Created
November 27, 2018 15:15
-
-
Save dougpagani/0484c8f6ba568d66d54b5fa0c90f8a05 to your computer and use it in GitHub Desktop.
Facilitated Teamocil.yml file creation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# FILE: timo-make.sh | |
# readline for facilitating | |
set -euo pipefail | |
############################################################ | |
#{{{ Helpers | |
tmuxdims () | |
{ | |
local WINDEX=${1:-$(tmux display-message -p '#I')}; | |
tmux list-windows | \grep -i "^${WINDEX}" | cut -f 3- -d'[' | sed 's/layout //' | sed 's/ @.*//' | sed 's/.$//' | |
} | |
ask() { | |
# https://djm.me/ask | |
local prompt default reply | |
while true; do | |
if [ "${2:-}" = "Y" ]; then | |
prompt="Y/n" | |
default=Y | |
elif [ "${2:-}" = "N" ]; then | |
prompt="y/N" | |
default=N | |
else | |
prompt="y/n" | |
default= | |
fi | |
# Ask the question (not using "read -p" as it uses stderr not stdout) | |
echo -n "$1 [$prompt] " | |
# Read the answer (use /dev/tty in case stdin is redirected from somewhere else) | |
read reply </dev/tty | |
# Default? | |
if [ -z "$reply" ]; then | |
reply=$default | |
fi | |
# Check if the reply is valid | |
case "$reply" in | |
"") return 0 ;; | |
Y*|y*) return 0 ;; | |
N*|n*) return 1 ;; | |
esac | |
done | |
} | |
quote () | |
{ | |
local quoted=${1//\'/\'\\\'\'}; | |
printf "'%s'" "$quoted" | |
} | |
#}}} | |
this_pane_ref=$(tmux display -p -t "$TMUX_PANE" "#{pane_index}") | |
tmux_window_dims=$(tmuxdims) | |
num_of_panes=$(tmux list-panes | wc -l | tr -d '[[:space:]]' ) | |
thisdirname="${PWD##*/}" | |
read -ep "What name for this template? [default: $thisdirname] " name | |
if [[ -z "$name" ]]; then | |
name="$thisdirname" | |
fi | |
# Populate template: | |
filename="./teamocil.yml" | |
cat > "$filename" <<HERE | |
name: XXXX | |
windows: | |
- name: "$name" | |
root: ./ | |
layout: "$tmux_window_dims" | |
panes: | |
HERE | |
# Add panes to template | |
for pn in $(seq 1 "$num_of_panes"); | |
do | |
# TODO: | |
#if this_pane_ref == pn, then make it the default-focused pane, w/ a "hello" message | |
read -e -p "Pane $pn > " pane_cmd | |
echo " - $(quote "$pane_cmd")" >> "$filename" | |
done | |
function confirm_yaml() { | |
cat "$filename" | |
if ask "^^^ Look good?"; then | |
echo "Ok, let's append the pane-cmds" | |
else | |
rm "$filename" | |
echo "TRY AGAIN" | |
exit 1 | |
fi | |
} | |
confirm_yaml | |
# TEST: for robust quote-handling | |
# echo "some * $PWD" 'now no $resolution' END-echo | |
# ... here: (for info on YAML, escaping, "shell" module etc.) | |
# https://stackoverflow.com/questions/37427498/how-to-escape-double-and-single-quotes-in-yaml-within-the-same-string | |
# ... here: (for whitespace trimming) | |
# https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-a-bash-variable | |
# bash, and ideas for templating | |
# https://stackoverflow.com/questions/6214743/create-new-file-from-templates-with-bash-script |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment