style(shell): improve shell script formatting and quoting

Apply shfmt formatting and fix some schellcheck complaints.
This commit is contained in:
2025-11-25 02:52:36 +00:00
parent 9941b28063
commit adb008b301
17 changed files with 66 additions and 65 deletions

View File

@@ -38,22 +38,22 @@ export PATH="$TMUXIFIER/libexec:$PATH"
export TMUXIFIER_MIN_TMUX_VERSION="1.6" export TMUXIFIER_MIN_TMUX_VERSION="1.6"
if [ "$(tmuxifier-tmux-version "$TMUXIFIER_MIN_TMUX_VERSION")" == "<" ]; then if [ "$(tmuxifier-tmux-version "$TMUXIFIER_MIN_TMUX_VERSION")" == "<" ]; then
echo -e "ERROR: Tmuxifier requires Tmux v${TMUXIFIER_MIN_TMUX_VERSION}" \ 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 exit 1
fi fi
# Parse given command # Parse given command
command="$1" command="$1"
case "$command" in case "$command" in
"" | "-h" | "--help" ) "" | "-h" | "--help")
echo -e "tmuxifier $(tmuxifier-version)\n$(tmuxifier-help)" >&2 echo -e "tmuxifier $(tmuxifier-version)\n$(tmuxifier-help)" >&2
;; ;;
"-v" | "--version" ) "-v" | "--version")
tmuxifier-version tmuxifier-version
;; ;;
* ) *)
! command_path="$(tmuxifier-resolve-command-path "$command")" ! command_path="$(tmuxifier-resolve-command-path "$command")"
if [ -z "$command_path" ]; then if [ -z "$command_path" ]; then

View File

@@ -3,11 +3,11 @@ _tmuxifier() {
local word="${COMP_WORDS[COMP_CWORD]}" local word="${COMP_WORDS[COMP_CWORD]}"
if [ "$COMP_CWORD" -eq 1 ]; then if [ "$COMP_CWORD" -eq 1 ]; then
COMPREPLY=( $(compgen -W "$(tmuxifier commands)" -- "$word") ) COMPREPLY=($(compgen -W "$(tmuxifier commands)" -- "$word"))
else else
local command="${COMP_WORDS[1]}" local command="${COMP_WORDS[1]}"
local completions="$(tmuxifier completions "$command")" local completions="$(tmuxifier completions "$command")"
COMPREPLY=( $(compgen -W "$completions" -- "$word") ) COMPREPLY=($(compgen -W "$completions" -- "$word"))
fi fi
} }

View File

@@ -140,7 +140,7 @@ balance_windows_horizontal() {
# #
synchronize_on() { synchronize_on() {
tmuxifier-tmux set-window-option -t "$session:${1:-$window}" \ tmuxifier-tmux set-window-option -t "$session:${1:-$window}" \
synchronize-panes on synchronize-panes on
} }
# Turn off synchronize-panes in a window. # Turn off synchronize-panes in a window.
@@ -150,7 +150,7 @@ synchronize_on() {
# #
synchronize_off() { synchronize_off() {
tmuxifier-tmux set-window-option -t "$session:${1:-$window}" \ tmuxifier-tmux set-window-option -t "$session:${1:-$window}" \
synchronize-panes off synchronize-panes off
} }
# Send/paste keys to the currently active pane/window. # Send/paste keys to the currently active pane/window.
@@ -214,8 +214,8 @@ load_window() {
if [ $# -gt 1 ]; then if [ $# -gt 1 ]; then
window="$2" window="$2"
else else
window="${1/%.window.sh}" window="${1/%.window.sh/}"
window="${window/%.sh}" window="${window/%.sh/}"
fi fi
source "$file" source "$file"
window= window=
@@ -258,8 +258,8 @@ load_session() {
if [ $# -gt 1 ]; then if [ $# -gt 1 ]; then
session="$2" session="$2"
else else
session="${1/%.session.sh}" session="${1/%.session.sh/}"
session="${session/%.sh}" session="${session/%.sh/}"
fi fi
set_default_path=true set_default_path=true
@@ -308,7 +308,7 @@ initialize_session() {
$set_default_path && tmuxifier-tmux \ $set_default_path && tmuxifier-tmux \
set-option -t "$session:" \ set-option -t "$session:" \
default-path "$session_root" 1>/dev/null default-path "$session_root" 1> /dev/null
fi fi
# Tmux 1.9 and later. # Tmux 1.9 and later.
@@ -344,13 +344,12 @@ initialize_session() {
# created, but already existed, then we'll need to specifically switch to it. # created, but already existed, then we'll need to specifically switch to it.
# #
finalize_and_go_to_session() { 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 if [[ "$(tmuxifier-current-session)" != "$session" ]]; then
__go_to_session __go_to_session
fi fi
} }
# #
# Internal functions # Internal functions
# #
@@ -368,7 +367,7 @@ __expand_path() {
__get_first_window_index() { __get_first_window_index() {
local index=$(tmuxifier-tmux list-windows -t "$session:" \ local index=$(tmuxifier-tmux list-windows -t "$session:" \
-F "#{window_index}" 2>/dev/null) -F "#{window_index}" 2> /dev/null)
if [ -n "$index" ]; then if [ -n "$index" ]; then
echo "$index" | head -1 echo "$index" | head -1
@@ -379,10 +378,10 @@ __get_first_window_index() {
__get_current_window_index() { __get_current_window_index() {
local lookup=$(tmuxifier-tmux list-windows -t "$session:" \ 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 if [ -n "$lookup" ]; then
echo "${lookup/1:}" echo "${lookup/1:/}"
fi fi
} }

View File

@@ -1,11 +1,11 @@
calling-help() { calling-help() {
if [[ " $@ " != *" --help "* ]] && [[ " $@ " != *" -h "* ]]; then if [[ " $* " != *" --help "* ]] && [[ " $* " != *" -h "* ]]; then
return 1 return 1
fi fi
} }
calling-complete() { calling-complete() {
if [[ " $@ " != *" --complete "* ]]; then if [[ " $* " != *" --complete "* ]]; then
return 1 return 1
fi fi
} }

View File

@@ -14,33 +14,34 @@ Resolve a command alias to it's full name."
fi fi
case "$1" in case "$1" in
"session" | "ses" | "s" ) "session" | "ses" | "s")
echo "load-session" echo "load-session"
;; ;;
"window" | "win" | "w" ) "window" | "win" | "w")
echo "load-window" echo "load-window"
;; ;;
"new-ses" | "nses" | "ns" ) "new-ses" | "nses" | "ns")
echo "new-session" echo "new-session"
;; ;;
"new-win" | "nwin" | "nw" ) "new-win" | "nwin" | "nw")
echo "new-window" echo "new-window"
;; ;;
"edit-ses" | "eses" | "es" ) "edit-ses" | "eses" | "es")
echo "edit-session" echo "edit-session"
;; ;;
"edit-win" | "ewin" | "ew" ) "edit-win" | "ewin" | "ew")
echo "edit-window" echo "edit-window"
;; ;;
"l" ) "l")
echo "list" echo "list"
;; ;;
"list-ses" | "lses" | "ls" ) "list-ses" | "lses" | "ls")
echo "list-sessions" echo "list-sessions"
;; ;;
"list-win" | "lwin" | "lw" ) "list-win" | "lwin" | "lw")
echo "list-windows" echo "list-windows"
;; ;;
* ) *)
exit 1 exit 1
;;
esac esac

View File

@@ -16,7 +16,8 @@ fi
shopt -s nullglob shopt -s nullglob
{ for path in ${PATH//:/$'\n'}; do {
for path in ${PATH//:/$'\n'}; do
for command in "${path}/tmuxifier-"*; do for command in "${path}/tmuxifier-"*; do
command="${command##*tmuxifier-}" command="${command##*tmuxifier-}"
echo "$command" echo "$command"

View File

@@ -20,11 +20,11 @@ if calling-complete "$@"; then
fi fi
has-completions() { has-completions() {
grep -i "^# Provide tmuxifier completions" "$1" >/dev/null grep -i "^# Provide tmuxifier completions" "$1" > /dev/null
} }
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo "$(tmuxifier-help completions $@)" >&2 tmuxifier-help completions "$@" >&2
exit 1 exit 1
fi fi

View File

@@ -14,8 +14,8 @@ Outputs the name of the current Tmux session."
fi fi
if [ -n "$TMUX" ]; then if [ -n "$TMUX" ]; then
for item in $(tmuxifier-tmux list-pane -F "#{session_name}");do for item in $(tmuxifier-tmux list-pane -F "#{session_name}"); do
echo $item echo "$item"
exit 0 exit 0
done done
fi fi

View File

@@ -22,7 +22,7 @@ if calling-complete "$@"; then
fi fi
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo "$(tmuxifier-help edit-session $@)" >&2 tmuxifier-help edit-session "$@" >&2
exit 1 exit 1
fi fi

View File

@@ -22,7 +22,7 @@ if calling-complete "$@"; then
fi fi
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo "$(tmuxifier-help edit-window $@)" >&2 tmuxifier-help edit-window "$@" >&2
exit 1 exit 1
fi fi

View File

@@ -18,7 +18,7 @@ if calling-complete "$@"; then
fi fi
has-help() { has-help() {
grep -i "^# Provide tmuxifier help" "$1" >/dev/null grep -i "^# Provide tmuxifier help" "$1" > /dev/null
} }
if [ -z "$1" ]; then if [ -z "$1" ]; then

View File

@@ -14,25 +14,25 @@ if [ -z "$shell" ]; then
fi fi
case "$shell" in case "$shell" in
bash ) bash)
profile='~/.bash_profile' profile='~/.bash_profile'
;; ;;
zsh ) zsh)
profile='~/.zshrc' profile='~/.zshrc'
;; ;;
ksh ) ksh)
profile='~/.profile' profile='~/.profile'
;; ;;
csh ) csh)
profile='~/.cshrc' profile='~/.cshrc'
;; ;;
tcsh ) tcsh)
profile='~/.tcshrc' profile='~/.tcshrc'
;; ;;
fish ) fish)
profile='~/.config/fish/config.fish' profile='~/.config/fish/config.fish'
;; ;;
* ) *)
profile='shell init file' profile='shell init file'
;; ;;
esac esac
@@ -45,15 +45,15 @@ Load Tmuxifier by adding the following to your ${profile}:
" "
case "$shell" in case "$shell" in
csh | tcsh ) csh | tcsh)
echo " eval \`tmuxifier init -\` echo " eval \`tmuxifier init -\`
" "
;; ;;
fish ) fish)
echo " eval (tmuxifier init -) echo " eval (tmuxifier init -)
" "
;; ;;
* ) *)
echo " eval \"\$(tmuxifier init -)\" echo " eval \"\$(tmuxifier init -)\"
" "
;; ;;
@@ -64,26 +64,26 @@ Load Tmuxifier by adding the following to your ${profile}:
fi fi
# Print help if "-" argument is not given # Print help if "-" argument is not given
if [[ " $@ " != *" - "* ]]; then if [[ " $* " != *" - "* ]]; then
echo "$(tmuxifier-help init $@)" >&2 tmuxifier-help init "$@" >&2
exit 1 exit 1
fi fi
case "$shell" in case "$shell" in
csh | tcsh ) csh | tcsh)
echo "setenv TMUXIFIER \"$TMUXIFIER\";" echo "setenv TMUXIFIER \"$TMUXIFIER\";"
echo "source \"\$TMUXIFIER/init.tcsh\";" echo "source \"\$TMUXIFIER/init.tcsh\";"
;; ;;
fish ) fish)
echo "set -gx TMUXIFIER \"$TMUXIFIER\";" echo "set -gx TMUXIFIER \"$TMUXIFIER\";"
# fish shell 2.0.0 does not have the source alias # 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 if [[ $(fish --version 2>&1 | awk -F'version ' '{print $2}') = '2.0.0' ]]; then
echo ". \"\$TMUXIFIER/init.fish\";" echo ". \"\$TMUXIFIER/init.fish\";"
else else
echo "source \"\$TMUXIFIER/init.fish\";" echo "source \"\$TMUXIFIER/init.fish\";"
fi fi
;; ;;
* ) *)
echo "export TMUXIFIER=\"$TMUXIFIER\";" echo "export TMUXIFIER=\"$TMUXIFIER\";"
echo "source \"\$TMUXIFIER/init.sh\";" echo "source \"\$TMUXIFIER/init.sh\";"
;; ;;

View File

@@ -30,7 +30,7 @@ if calling-complete "$@"; then
fi fi
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo "$(tmuxifier-help load-session $@)" >&2 tmuxifier-help load-session "$@" >&2
exit 1 exit 1
fi fi

View File

@@ -26,14 +26,14 @@ if calling-complete "$@"; then
fi fi
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo "$(tmuxifier-help load-window $@)" >&2 tmuxifier-help load-window "$@" >&2
exit 1 exit 1
fi fi
# Load runtime functions. # Load runtime functions.
source "$TMUXIFIER/lib/runtime.sh" source "$TMUXIFIER/lib/runtime.sh"
if [ ! -z $TMUX ]; then if [ ! -z "$TMUX" ]; then
session="$(tmuxifier-current-session)" session="$(tmuxifier-current-session)"
load_window "$1" load_window "$1"
else else

View File

@@ -22,7 +22,7 @@ if calling-complete "$@"; then
fi fi
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo "$(tmuxifier-help new-session $@)" >&2 tmuxifier-help new-session "$@" >&2
exit 1 exit 1
fi fi

View File

@@ -22,7 +22,7 @@ if calling-complete "$@"; then
fi fi
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo "$(tmuxifier-help new-window $@)" >&2 tmuxifier-help new-window "$@" >&2
exit 1 exit 1
fi fi

View File

@@ -19,16 +19,16 @@ fi
# The vercomp() function is shamelessly ripped/borrowed from the following # The vercomp() function is shamelessly ripped/borrowed from the following
# StackOverflow answer: http://stackoverflow.com/a/4025065/42146 # StackOverflow answer: http://stackoverflow.com/a/4025065/42146
vercomp () { vercomp() {
if [[ $1 == $2 ]]; then return 0; fi if [[ $1 == $2 ]]; then return 0; fi
local IFS=. local IFS=.
local i ver1=($1) ver2=($2) local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros # 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 # fill empty fields in ver2 with zeros
if [[ -z ${ver2[i]} ]]; then ver2[i]=0; fi if [[ -z ${ver2[i]} ]]; then ver2[i]=0; fi
@@ -63,8 +63,8 @@ else
vercomp "$version" "$1" vercomp "$version" "$1"
case $? in case $? in
0) echo '=';; 0) echo '=' ;;
1) echo '>';; 1) echo '>' ;;
2) echo '<';; 2) echo '<' ;;
esac esac
fi fi