2018-12-23 01:56:55 -06:00
|
|
|
#! /usr/bin/env bash
|
|
|
|
#
|
|
|
|
# i3-settree
|
|
|
|
# Restore an i3 layout using a list of saved trees
|
|
|
|
# Copyright (C) 2018 salt <salt@lap-th-e560-0>
|
|
|
|
#
|
|
|
|
# Distributed under terms of the MIT license.
|
|
|
|
#
|
|
|
|
|
|
|
|
# Pedantry
|
|
|
|
# shellcheck disable=1117
|
|
|
|
|
|
|
|
# Globals
|
|
|
|
name="$(basename -- "$0")"
|
|
|
|
treespath="${XDG_CONFIG_DIR:-$HOME/.config}/i3/trees"
|
|
|
|
|
|
|
|
# Helpers
|
|
|
|
log() {
|
|
|
|
[ -z "$1" ] && return 1
|
|
|
|
printf "%s: %s\n" \
|
|
|
|
"$name" \
|
|
|
|
"$1"
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
prepare_input() {
|
2018-12-23 14:53:23 -06:00
|
|
|
options="Save"
|
2018-12-23 01:56:55 -06:00
|
|
|
for file in "$treespath"/*; do
|
|
|
|
if ! [ -r "$file" ]; then
|
|
|
|
# Cannot read the file; skip it
|
|
|
|
continue
|
|
|
|
fi
|
2018-12-23 12:07:07 -06:00
|
|
|
if ! [[ "$file" = *".json" ]]; then
|
|
|
|
# File is not a json structure; skip it
|
2018-12-23 12:01:22 -06:00
|
|
|
continue
|
|
|
|
fi
|
2018-12-23 14:53:23 -06:00
|
|
|
if [[ "$file" = "$treespath/$name-layout-"* ]]; then
|
|
|
|
# File is an unmodified saved tree; skip it
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
if [ "$(basename -s ".json" -- "$file")" = "Save" ]; then
|
|
|
|
# File conflicts with reserved word "Save"; skip it
|
|
|
|
continue
|
|
|
|
fi
|
2018-12-23 01:56:55 -06:00
|
|
|
if ! [ -f "$file" ]; then
|
|
|
|
# Glob failed and no files exist; error
|
|
|
|
return 1
|
|
|
|
fi
|
2018-12-23 14:53:23 -06:00
|
|
|
options="$options
|
2018-12-23 12:07:07 -06:00
|
|
|
$(basename -s ".json" -- "$file")"
|
2018-12-23 01:56:55 -06:00
|
|
|
done
|
|
|
|
printf "%s" \
|
|
|
|
"$options"
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
# Steps
|
|
|
|
step_validate_deps() {
|
|
|
|
# Ensure we have all the programs we need to do this
|
2018-12-23 14:53:23 -06:00
|
|
|
for dep in mkdir date rofi i3-msg notify-send; do
|
2018-12-23 01:56:55 -06:00
|
|
|
if ! command -v "$dep" > /dev/null 2>&1; then
|
|
|
|
log "Could not validate dependency \"$dep\""
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
step_validate_files() {
|
|
|
|
# Ensure we actually have tree files to set
|
|
|
|
if ! [ -d "$treespath" ]; then
|
|
|
|
mkdir -p "$treespath" > /dev/null 2>&1
|
|
|
|
if [ -d "$treespath" ]; then
|
|
|
|
log "Trees path was created: $treespath"
|
|
|
|
log "Put your modified i3-save-tree trees there"
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
log "Failed to create trees path: $treespath"
|
|
|
|
log "Are permissions set correctly? Is the filesystem mounted read-write?"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
step_get_input() {
|
|
|
|
# Get the user input, store it in $choice
|
2018-12-23 12:01:22 -06:00
|
|
|
choice="$(rofi -dmenu -p "i3 tree" <<< "$(prepare_input)")"
|
2018-12-23 12:07:07 -06:00
|
|
|
choicefile="$treespath/$choice.json"
|
2018-12-23 01:56:55 -06:00
|
|
|
if [ -z "$choice" ]; then
|
|
|
|
log "User cancelled the operation"
|
|
|
|
return 1
|
2018-12-23 12:07:07 -06:00
|
|
|
else
|
|
|
|
log "Choice: $choice"
|
|
|
|
log "Choicefile: $choicefile"
|
2018-12-23 01:56:55 -06:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
step_act() {
|
|
|
|
# Do the do
|
|
|
|
case "$choice" in
|
2018-12-23 14:53:23 -06:00
|
|
|
Save)
|
|
|
|
file="$treespath/$name-layout-$(date +%s%N).json"
|
|
|
|
if [ -f "$file" ]; then
|
|
|
|
log "Not clobbering existing layout file: $file"
|
|
|
|
return 60
|
|
|
|
fi
|
|
|
|
i3-save-tree > "$file"
|
|
|
|
notify-send \
|
|
|
|
-u normal \
|
|
|
|
-a "$name" \
|
|
|
|
-t 3000 \
|
|
|
|
-i dialog-information \
|
|
|
|
"Saved Layout" \
|
|
|
|
"Saved current i3 tree to:\n$file"
|
|
|
|
;;
|
2018-12-23 01:56:55 -06:00
|
|
|
*)
|
2018-12-23 12:07:07 -06:00
|
|
|
i3-msg "append_layout $choicefile" > /dev/null 2>&1
|
|
|
|
if [ -r "$choicefile.rc" ]; then
|
|
|
|
log "Running $choicefile.rc"
|
|
|
|
"$choicefile".rc
|
2018-12-23 12:01:22 -06:00
|
|
|
fi
|
2018-12-23 01:56:55 -06:00
|
|
|
return $!
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
# Main
|
|
|
|
main() {
|
|
|
|
step_validate_deps || return 50
|
|
|
|
step_validate_files || return 51
|
|
|
|
step_get_input || return 52
|
|
|
|
step_act
|
|
|
|
return $!
|
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|
|
|
|
|