From adb008b30152c6c88c33b6fd80e3b4b100413a39 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Tue, 25 Nov 2025 02:52:36 +0000 Subject: [PATCH] style(shell): improve shell script formatting and quoting Apply shfmt formatting and fix some schellcheck complaints. --- bin/tmuxifier | 8 ++++---- completion/tmuxifier.bash | 4 ++-- lib/layout-helpers.sh | 23 +++++++++++----------- lib/util.sh | 4 ++-- libexec/tmuxifier-alias | 21 ++++++++++---------- libexec/tmuxifier-commands | 3 ++- libexec/tmuxifier-completions | 4 ++-- libexec/tmuxifier-current-session | 4 ++-- libexec/tmuxifier-edit-session | 2 +- libexec/tmuxifier-edit-window | 2 +- libexec/tmuxifier-help | 2 +- libexec/tmuxifier-init | 32 +++++++++++++++---------------- libexec/tmuxifier-load-session | 2 +- libexec/tmuxifier-load-window | 4 ++-- libexec/tmuxifier-new-session | 2 +- libexec/tmuxifier-new-window | 2 +- libexec/tmuxifier-tmux-version | 12 ++++++------ 17 files changed, 66 insertions(+), 65 deletions(-) diff --git a/bin/tmuxifier b/bin/tmuxifier index 3568f97..5539aa5 100755 --- a/bin/tmuxifier +++ b/bin/tmuxifier @@ -38,22 +38,22 @@ export PATH="$TMUXIFIER/libexec:$PATH" export TMUXIFIER_MIN_TMUX_VERSION="1.6" if [ "$(tmuxifier-tmux-version "$TMUXIFIER_MIN_TMUX_VERSION")" == "<" ]; then echo -e "ERROR: Tmuxifier requires Tmux v${TMUXIFIER_MIN_TMUX_VERSION}" \ - "or newer. You have v$(tmuxifier-tmux-version)." >&2 + "or newer. You have v$(tmuxifier-tmux-version)." >&2 exit 1 fi # Parse given command command="$1" case "$command" in - "" | "-h" | "--help" ) + "" | "-h" | "--help") echo -e "tmuxifier $(tmuxifier-version)\n$(tmuxifier-help)" >&2 ;; - "-v" | "--version" ) + "-v" | "--version") tmuxifier-version ;; - * ) + *) ! command_path="$(tmuxifier-resolve-command-path "$command")" if [ -z "$command_path" ]; then diff --git a/completion/tmuxifier.bash b/completion/tmuxifier.bash index 6d26e40..f87fc70 100644 --- a/completion/tmuxifier.bash +++ b/completion/tmuxifier.bash @@ -3,11 +3,11 @@ _tmuxifier() { local word="${COMP_WORDS[COMP_CWORD]}" if [ "$COMP_CWORD" -eq 1 ]; then - COMPREPLY=( $(compgen -W "$(tmuxifier commands)" -- "$word") ) + COMPREPLY=($(compgen -W "$(tmuxifier commands)" -- "$word")) else local command="${COMP_WORDS[1]}" local completions="$(tmuxifier completions "$command")" - COMPREPLY=( $(compgen -W "$completions" -- "$word") ) + COMPREPLY=($(compgen -W "$completions" -- "$word")) fi } diff --git a/lib/layout-helpers.sh b/lib/layout-helpers.sh index c9a91ab..1e0a226 100644 --- a/lib/layout-helpers.sh +++ b/lib/layout-helpers.sh @@ -140,7 +140,7 @@ balance_windows_horizontal() { # synchronize_on() { tmuxifier-tmux set-window-option -t "$session:${1:-$window}" \ - synchronize-panes on + synchronize-panes on } # Turn off synchronize-panes in a window. @@ -150,7 +150,7 @@ synchronize_on() { # synchronize_off() { tmuxifier-tmux set-window-option -t "$session:${1:-$window}" \ - synchronize-panes off + synchronize-panes off } # Send/paste keys to the currently active pane/window. @@ -214,8 +214,8 @@ load_window() { if [ $# -gt 1 ]; then window="$2" else - window="${1/%.window.sh}" - window="${window/%.sh}" + window="${1/%.window.sh/}" + window="${window/%.sh/}" fi source "$file" window= @@ -258,8 +258,8 @@ load_session() { if [ $# -gt 1 ]; then session="$2" else - session="${1/%.session.sh}" - session="${session/%.sh}" + session="${1/%.session.sh/}" + session="${session/%.sh/}" fi set_default_path=true @@ -308,7 +308,7 @@ initialize_session() { $set_default_path && tmuxifier-tmux \ set-option -t "$session:" \ - default-path "$session_root" 1>/dev/null + default-path "$session_root" 1> /dev/null fi # Tmux 1.9 and later. @@ -344,13 +344,12 @@ initialize_session() { # created, but already existed, then we'll need to specifically switch to it. # finalize_and_go_to_session() { - ! tmuxifier-tmux kill-window -t "$session:999" 2>/dev/null + ! tmuxifier-tmux kill-window -t "$session:999" 2> /dev/null if [[ "$(tmuxifier-current-session)" != "$session" ]]; then __go_to_session fi } - # # Internal functions # @@ -368,7 +367,7 @@ __expand_path() { __get_first_window_index() { local index=$(tmuxifier-tmux list-windows -t "$session:" \ - -F "#{window_index}" 2>/dev/null) + -F "#{window_index}" 2> /dev/null) if [ -n "$index" ]; then echo "$index" | head -1 @@ -379,10 +378,10 @@ __get_first_window_index() { __get_current_window_index() { local lookup=$(tmuxifier-tmux list-windows -t "$session:" \ - -F "#{window_active}:#{window_index}" 2>/dev/null | grep "^1:") + -F "#{window_active}:#{window_index}" 2> /dev/null | grep "^1:") if [ -n "$lookup" ]; then - echo "${lookup/1:}" + echo "${lookup/1:/}" fi } diff --git a/lib/util.sh b/lib/util.sh index eeaf9a2..02e4a2e 100644 --- a/lib/util.sh +++ b/lib/util.sh @@ -1,11 +1,11 @@ calling-help() { - if [[ " $@ " != *" --help "* ]] && [[ " $@ " != *" -h "* ]]; then + if [[ " $* " != *" --help "* ]] && [[ " $* " != *" -h "* ]]; then return 1 fi } calling-complete() { - if [[ " $@ " != *" --complete "* ]]; then + if [[ " $* " != *" --complete "* ]]; then return 1 fi } diff --git a/libexec/tmuxifier-alias b/libexec/tmuxifier-alias index 94e4f7e..60c63a2 100755 --- a/libexec/tmuxifier-alias +++ b/libexec/tmuxifier-alias @@ -14,33 +14,34 @@ Resolve a command alias to it's full name." fi case "$1" in - "session" | "ses" | "s" ) + "session" | "ses" | "s") echo "load-session" ;; - "window" | "win" | "w" ) + "window" | "win" | "w") echo "load-window" ;; - "new-ses" | "nses" | "ns" ) + "new-ses" | "nses" | "ns") echo "new-session" ;; - "new-win" | "nwin" | "nw" ) + "new-win" | "nwin" | "nw") echo "new-window" ;; - "edit-ses" | "eses" | "es" ) + "edit-ses" | "eses" | "es") echo "edit-session" ;; - "edit-win" | "ewin" | "ew" ) + "edit-win" | "ewin" | "ew") echo "edit-window" ;; - "l" ) + "l") echo "list" ;; - "list-ses" | "lses" | "ls" ) + "list-ses" | "lses" | "ls") echo "list-sessions" ;; - "list-win" | "lwin" | "lw" ) + "list-win" | "lwin" | "lw") echo "list-windows" ;; - * ) + *) exit 1 + ;; esac diff --git a/libexec/tmuxifier-commands b/libexec/tmuxifier-commands index 9de81aa..5f9fd2b 100755 --- a/libexec/tmuxifier-commands +++ b/libexec/tmuxifier-commands @@ -16,7 +16,8 @@ fi shopt -s nullglob -{ for path in ${PATH//:/$'\n'}; do +{ + for path in ${PATH//:/$'\n'}; do for command in "${path}/tmuxifier-"*; do command="${command##*tmuxifier-}" echo "$command" diff --git a/libexec/tmuxifier-completions b/libexec/tmuxifier-completions index 4408275..a5c041e 100755 --- a/libexec/tmuxifier-completions +++ b/libexec/tmuxifier-completions @@ -20,11 +20,11 @@ if calling-complete "$@"; then fi has-completions() { - grep -i "^# Provide tmuxifier completions" "$1" >/dev/null + grep -i "^# Provide tmuxifier completions" "$1" > /dev/null } if [ -z "$1" ]; then - echo "$(tmuxifier-help completions $@)" >&2 + tmuxifier-help completions "$@" >&2 exit 1 fi diff --git a/libexec/tmuxifier-current-session b/libexec/tmuxifier-current-session index 82e09ea..27dc96f 100755 --- a/libexec/tmuxifier-current-session +++ b/libexec/tmuxifier-current-session @@ -14,8 +14,8 @@ Outputs the name of the current Tmux session." fi if [ -n "$TMUX" ]; then - for item in $(tmuxifier-tmux list-pane -F "#{session_name}");do - echo $item + for item in $(tmuxifier-tmux list-pane -F "#{session_name}"); do + echo "$item" exit 0 done fi diff --git a/libexec/tmuxifier-edit-session b/libexec/tmuxifier-edit-session index e0722d1..9dad083 100755 --- a/libexec/tmuxifier-edit-session +++ b/libexec/tmuxifier-edit-session @@ -22,7 +22,7 @@ if calling-complete "$@"; then fi if [ -z "$1" ]; then - echo "$(tmuxifier-help edit-session $@)" >&2 + tmuxifier-help edit-session "$@" >&2 exit 1 fi diff --git a/libexec/tmuxifier-edit-window b/libexec/tmuxifier-edit-window index dee2986..f01420d 100755 --- a/libexec/tmuxifier-edit-window +++ b/libexec/tmuxifier-edit-window @@ -22,7 +22,7 @@ if calling-complete "$@"; then fi if [ -z "$1" ]; then - echo "$(tmuxifier-help edit-window $@)" >&2 + tmuxifier-help edit-window "$@" >&2 exit 1 fi diff --git a/libexec/tmuxifier-help b/libexec/tmuxifier-help index 5c27f1b..5f06efb 100755 --- a/libexec/tmuxifier-help +++ b/libexec/tmuxifier-help @@ -18,7 +18,7 @@ if calling-complete "$@"; then fi has-help() { - grep -i "^# Provide tmuxifier help" "$1" >/dev/null + grep -i "^# Provide tmuxifier help" "$1" > /dev/null } if [ -z "$1" ]; then diff --git a/libexec/tmuxifier-init b/libexec/tmuxifier-init index 0bad316..d57b8a6 100755 --- a/libexec/tmuxifier-init +++ b/libexec/tmuxifier-init @@ -14,25 +14,25 @@ if [ -z "$shell" ]; then fi case "$shell" in - bash ) + bash) profile='~/.bash_profile' ;; - zsh ) + zsh) profile='~/.zshrc' ;; - ksh ) + ksh) profile='~/.profile' ;; - csh ) + csh) profile='~/.cshrc' ;; - tcsh ) + tcsh) profile='~/.tcshrc' ;; - fish ) + fish) profile='~/.config/fish/config.fish' ;; - * ) + *) profile='shell init file' ;; esac @@ -45,15 +45,15 @@ Load Tmuxifier by adding the following to your ${profile}: " case "$shell" in - csh | tcsh ) + csh | tcsh) echo " eval \`tmuxifier init -\` " ;; - fish ) + fish) echo " eval (tmuxifier init -) " ;; - * ) + *) echo " eval \"\$(tmuxifier init -)\" " ;; @@ -64,26 +64,26 @@ Load Tmuxifier by adding the following to your ${profile}: fi # Print help if "-" argument is not given -if [[ " $@ " != *" - "* ]]; then - echo "$(tmuxifier-help init $@)" >&2 +if [[ " $* " != *" - "* ]]; then + tmuxifier-help init "$@" >&2 exit 1 fi case "$shell" in - csh | tcsh ) + csh | tcsh) echo "setenv TMUXIFIER \"$TMUXIFIER\";" echo "source \"\$TMUXIFIER/init.tcsh\";" ;; - fish ) + fish) echo "set -gx TMUXIFIER \"$TMUXIFIER\";" # fish shell 2.0.0 does not have the source alias if [[ $(fish --version 2>&1 | awk -F'version ' '{print $2}') = '2.0.0' ]]; then echo ". \"\$TMUXIFIER/init.fish\";" else echo "source \"\$TMUXIFIER/init.fish\";" - fi + fi ;; - * ) + *) echo "export TMUXIFIER=\"$TMUXIFIER\";" echo "source \"\$TMUXIFIER/init.sh\";" ;; diff --git a/libexec/tmuxifier-load-session b/libexec/tmuxifier-load-session index 5083b4f..318f875 100755 --- a/libexec/tmuxifier-load-session +++ b/libexec/tmuxifier-load-session @@ -30,7 +30,7 @@ if calling-complete "$@"; then fi if [ -z "$1" ]; then - echo "$(tmuxifier-help load-session $@)" >&2 + tmuxifier-help load-session "$@" >&2 exit 1 fi diff --git a/libexec/tmuxifier-load-window b/libexec/tmuxifier-load-window index 0a6d0f9..02a3e1d 100755 --- a/libexec/tmuxifier-load-window +++ b/libexec/tmuxifier-load-window @@ -26,14 +26,14 @@ if calling-complete "$@"; then fi if [ -z "$1" ]; then - echo "$(tmuxifier-help load-window $@)" >&2 + tmuxifier-help load-window "$@" >&2 exit 1 fi # Load runtime functions. source "$TMUXIFIER/lib/runtime.sh" -if [ ! -z $TMUX ]; then +if [ ! -z "$TMUX" ]; then session="$(tmuxifier-current-session)" load_window "$1" else diff --git a/libexec/tmuxifier-new-session b/libexec/tmuxifier-new-session index c3ec25f..4de01d2 100755 --- a/libexec/tmuxifier-new-session +++ b/libexec/tmuxifier-new-session @@ -22,7 +22,7 @@ if calling-complete "$@"; then fi if [ -z "$1" ]; then - echo "$(tmuxifier-help new-session $@)" >&2 + tmuxifier-help new-session "$@" >&2 exit 1 fi diff --git a/libexec/tmuxifier-new-window b/libexec/tmuxifier-new-window index a9e35ef..f4be1ae 100755 --- a/libexec/tmuxifier-new-window +++ b/libexec/tmuxifier-new-window @@ -22,7 +22,7 @@ if calling-complete "$@"; then fi if [ -z "$1" ]; then - echo "$(tmuxifier-help new-window $@)" >&2 + tmuxifier-help new-window "$@" >&2 exit 1 fi diff --git a/libexec/tmuxifier-tmux-version b/libexec/tmuxifier-tmux-version index f48b17e..8dedff5 100755 --- a/libexec/tmuxifier-tmux-version +++ b/libexec/tmuxifier-tmux-version @@ -19,16 +19,16 @@ fi # The vercomp() function is shamelessly ripped/borrowed from the following # StackOverflow answer: http://stackoverflow.com/a/4025065/42146 -vercomp () { +vercomp() { if [[ $1 == $2 ]]; then return 0; fi local IFS=. local i ver1=($1) ver2=($2) # fill empty fields in ver1 with zeros - for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do ver1[i]=0; done + for ((i = ${#ver1[@]}; i < ${#ver2[@]}; i++)); do ver1[i]=0; done - for ((i=0; i<${#ver1[@]}; i++)); do + for ((i = 0; i < ${#ver1[@]}; i++)); do # fill empty fields in ver2 with zeros if [[ -z ${ver2[i]} ]]; then ver2[i]=0; fi @@ -63,8 +63,8 @@ else vercomp "$version" "$1" case $? in - 0) echo '=';; - 1) echo '>';; - 2) echo '<';; + 0) echo '=' ;; + 1) echo '>' ;; + 2) echo '<' ;; esac fi