sh: Refactor argument construction

This centralizes the global array mutation and maintains the strict
quoting discipline.
This commit is contained in:
Robert Estelle 2020-07-29 12:10:09 -07:00 committed by Ferran Basora
parent 27b0214408
commit eced2d558e

View File

@ -2,41 +2,36 @@
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PARAMS=() function get-opt-value() {
function get-option() {
tmux show -vg "@thumbs-${1}" 2> /dev/null tmux show -vg "@thumbs-${1}" 2> /dev/null
} }
function add-boolean-param { function get-opt-arg() {
local opt local opt type value
opt="${1}" opt="${1}"; type="${2}"
if [ "$(get-option "${opt}")" = 1 ]; then value="$(get-opt-value "${opt}")" || true
PARAMS+=("--${opt}")
if [ "${type}" = string ]; then
[ -n "${value}" ] && echo "--${opt}=${value}"
elif [ "${type}" = boolean ]; then
[ "${value}" = 1 ] && echo "--${opt}"
else
return 1
fi fi
} }
function add-option-param { PARAMS=(--dir "${CURRENT_DIR}")
local opt value
opt="${1}" function add-param() {
value="$(get-option "${opt}")" local type opt arg
if [ -n "${value}" ]; then opt="${1}"; type="${2}"
PARAMS+=("--${opt}=${value}") if arg="$(get-opt-arg "${opt}" "${type}")"; then
PARAMS+=("${arg}")
fi fi
} }
add-option-param command add-param command string
add-option-param upcase-command add-param upcase-command string
add-boolean-param osc52 add-param osc52 boolean
# Remove empty arguments from PARAMS. "${CURRENT_DIR}/target/release/tmux-thumbs" "${PARAMS[@]}" || true
# Otherwise, they would choke up tmux-thumbs when passed to it.
for i in "${!PARAMS[@]}"; do
[ -n "${PARAMS[$i]}" ] || unset "PARAMS[$i]"
done
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
"${CURRENT_DIR}/target/release/tmux-thumbs" --dir "${CURRENT_DIR}" "${PARAMS[@]}"
true