From 4f6540b0d2ddf6ae5527b272cb17dd65a92fe98f Mon Sep 17 00:00:00 2001 From: Salt Date: Sun, 23 Dec 2018 01:56:55 -0600 Subject: [PATCH] i3-settree: Add first implementation of an i3 tree restorer --- i3-settree | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100755 i3-settree diff --git a/i3-settree b/i3-settree new file mode 100755 index 0000000..b0abea5 --- /dev/null +++ b/i3-settree @@ -0,0 +1,105 @@ +#! /usr/bin/env bash +# +# i3-settree +# Restore an i3 layout using a list of saved trees +# Copyright (C) 2018 salt +# +# 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 + if ! [ -f "$file" ]; then + # Glob failed and no files exist; error + return 1 + fi + if [ -z "$options" ]; then + options="$file" + else + options="$options +$file" + 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 + choice="$(rofi -dmenu -p "append tree" <<< "$(prepare_input)")" + if [ -z "$choice" ]; then + log "User cancelled the operation" + return 1 + fi +} +step_act() { + # Do the do + # Using a case statement here in case I want to implement saving + # functionality later + case "$choice" in + *) + i3-msg "append_layout $choice" > /dev/null 2>&1 + return $! + ;; + esac +} + +# Main +main() { + step_validate_deps || return 50 + step_validate_files || return 51 + step_get_input || return 52 + step_act + return $! +} + +main "$@" +