wip(commands): re-implement core commands as bash functions

Re-implement all libexec executables as bash functions, with the goal of
being able build a single standalone execuable bash script for
tmuxifier.
This commit is contained in:
2025-12-01 23:17:41 +00:00
parent f268c12f3f
commit b2a67705b6
8 changed files with 240 additions and 174 deletions

View File

@@ -3,11 +3,12 @@ set -e
[ -n "$TMUXIFIER_DEBUG" ] && set -x
resolve_link() {
$(type -p greadlink readlink | head -1) $1
$(type -p greadlink readlink | head -1) "$1"
}
abs_dirname() {
local cwd="$(pwd)"
local cwd
cwd="$(pwd)"
local path="$1"
while [ -n "$path" ]; do
@@ -20,48 +21,63 @@ abs_dirname() {
cd "$cwd"
}
if [ -z "${TMUXIFIER}" ]; then
# Set TMUXIFIER relative to the "tmuxifier" executable.
export TMUXIFIER="$(dirname "$(abs_dirname "$0")")"
else
# Strip any trailing slash (/) characters from TMUXIFIER variable.
export TMUXIFIER="${TMUXIFIER%/}"
fi
main() {
if [ -z "${TMUXIFIER}" ]; then
# Set TMUXIFIER relative to the "tmuxifier" executable.
export TMUXIFIER="$(dirname "$(abs_dirname "$0")")"
else
# Strip any trailing slash (/) characters from TMUXIFIER variable.
export TMUXIFIER="${TMUXIFIER%/}"
fi
# Load tmuxifier environment variables.
source "$TMUXIFIER/lib/env.sh"
# Bootstrap tmuxifier.
source "$TMUXIFIER/lib/load.sh"
# Add tmuxifier's internal commands to PATH.
export PATH="$TMUXIFIER/libexec:$PATH"
# Check Tmux version.
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
exit 1
fi
# Check Tmux version.
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
exit 1
fi
# Get command and shift arguments.
local command="$1"
shift 1
# Parse given command
command="$1"
case "$command" in
"" | "-h" | "--help")
echo -e "tmuxifier $(tmuxifier-version)\n$(tmuxifier-help)" >&2
;;
# Resolve to full command name if an alias is given.
command="$(tmuxifier-alias "$command" || echo "$command")"
"-v" | "--version")
tmuxifier-version
;;
case "$command" in
"" | "-h" | "--help")
echo -e "tmuxifier $(tmuxifier-version)\n$(tmuxifier-help)" >&2
;;
*)
! command_path="$(tmuxifier-resolve-command-path "$command")"
"-v" | "--version")
tmuxifier-version
;;
if [ -z "$command_path" ]; then
echo "tmuxifier: no such command '$command'" >&2
exit 1
fi
*)
local command_path
local func_name
shift 1
exec "$command_path" "$@"
;;
esac
func_name="tmuxifier-$command"
# Check if command is available as a function.
if declare -f "$func_name" > /dev/null; then
"$func_name" "$@"
else
# Fall back to libexec executable.
command_path="$(tmuxifier-resolve-command-path "$command")" || true
if [ -z "$command_path" ]; then
echo "tmuxifier: no such command '$command'" >&2
exit 1
fi
exec "$command_path" "$@"
fi
;;
esac
}
main "$@"