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() {
|
|
|
|
options=""
|
|
|
|
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 01:56:55 -06:00
|
|
|
if ! [ -f "$file" ]; then
|
|
|
|
# Glob failed and no files exist; error
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ -z "$options" ]; then
|
2018-12-23 12:07:07 -06:00
|
|
|
options="$(basename -s ".json" -- "$file")"
|
2018-12-23 01:56:55 -06:00
|
|
|
else
|
|
|
|
options="$options
|
2018-12-23 12:07:07 -06:00
|
|
|
$(basename -s ".json" -- "$file")"
|
2018-12-23 01:56:55 -06:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
printf "%s" \
|
|
|
|
"$options"
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
# Steps
|
|
|
|
step_validate_deps() {
|
|
|
|
# Ensure we have all the programs we need to do this
|
|
|
|
for dep in rofi i3-msg; do
|
|
|
|
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
|
|
|
|
# Using a case statement here in case I want to implement saving
|
|
|
|
# functionality later
|
|
|
|
case "$choice" in
|
|
|
|
*)
|
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 "$@"
|
|
|
|
|