fix(bash): Handle Unbound Variables Errors in Bash (#4972)

* fix: unbound bp pipestatus variable

* fix: unbound preserved prompt command variable

* fix: unbound starship start time variable

* fix: unbound preexec_functions, precmd_functions

and PROMPT_COMMAND variables.
This commit is contained in:
tricktron 2024-01-27 22:08:19 +01:00 committed by GitHub
parent da4af64dc5
commit 7093d5cd84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 5 deletions

View File

@ -36,7 +36,7 @@ starship_precmd() {
if [[ ${BLE_ATTACHED-} && ${#BLE_PIPESTATUS[@]} -gt 0 ]]; then if [[ ${BLE_ATTACHED-} && ${#BLE_PIPESTATUS[@]} -gt 0 ]]; then
STARSHIP_PIPE_STATUS=("${BLE_PIPESTATUS[@]}") STARSHIP_PIPE_STATUS=("${BLE_PIPESTATUS[@]}")
fi fi
if [[ "${#BP_PIPESTATUS[@]}" -gt "${#STARSHIP_PIPE_STATUS[@]}" ]]; then if [[ -n "${BP_PIPESTATUS-}" ]] && [[ "${#BP_PIPESTATUS[@]}" -gt "${#STARSHIP_PIPE_STATUS[@]}" ]]; then
STARSHIP_PIPE_STATUS=(${BP_PIPESTATUS[@]}) STARSHIP_PIPE_STATUS=(${BP_PIPESTATUS[@]})
fi fi
@ -65,11 +65,13 @@ starship_precmd() {
# command pipeline, which may rely on it. # command pipeline, which may rely on it.
_starship_set_return "$STARSHIP_CMD_STATUS" _starship_set_return "$STARSHIP_CMD_STATUS"
if [[ -n "${_PRESERVED_PROMPT_COMMAND-}" ]]; then
eval "$_PRESERVED_PROMPT_COMMAND" eval "$_PRESERVED_PROMPT_COMMAND"
fi
local -a ARGS=(--terminal-width="${COLUMNS}" --status="${STARSHIP_CMD_STATUS}" --pipestatus="${STARSHIP_PIPE_STATUS[*]}" --jobs="${NUM_JOBS}") local -a ARGS=(--terminal-width="${COLUMNS}" --status="${STARSHIP_CMD_STATUS}" --pipestatus="${STARSHIP_PIPE_STATUS[*]}" --jobs="${NUM_JOBS}")
# Prepare the timer data, if needed. # Prepare the timer data, if needed.
if [[ $STARSHIP_START_TIME ]]; then if [[ -n "${STARSHIP_START_TIME-}" ]]; then
STARSHIP_END_TIME=$(::STARSHIP:: time) STARSHIP_END_TIME=$(::STARSHIP:: time)
STARSHIP_DURATION=$((STARSHIP_END_TIME - STARSHIP_START_TIME)) STARSHIP_DURATION=$((STARSHIP_END_TIME - STARSHIP_START_TIME))
ARGS+=( --cmd-duration="${STARSHIP_DURATION}") ARGS+=( --cmd-duration="${STARSHIP_DURATION}")
@ -90,7 +92,7 @@ if [[ ${BLE_VERSION-} && _ble_version -ge 400 ]]; then
blehook PRECMD!='starship_precmd' blehook PRECMD!='starship_precmd'
# If the user appears to be using https://github.com/rcaloras/bash-preexec, # If the user appears to be using https://github.com/rcaloras/bash-preexec,
# then hook our functions into their framework. # then hook our functions into their framework.
elif [[ "${__bp_imported:-}" == "defined" || $preexec_functions || $precmd_functions ]]; then elif [[ "${__bp_imported:-}" == "defined" || -n "${preexec_functions-}" || -n "${precmd_functions-}" ]]; then
# bash-preexec needs a single function--wrap the args into a closure and pass # bash-preexec needs a single function--wrap the args into a closure and pass
starship_preexec_all(){ starship_preexec "$_"; } starship_preexec_all(){ starship_preexec "$_"; }
preexec_functions+=(starship_preexec_all) preexec_functions+=(starship_preexec_all)
@ -111,7 +113,7 @@ else
# Finally, prepare the precmd function and set up the start time. We will avoid to # Finally, prepare the precmd function and set up the start time. We will avoid to
# add multiple instances of the starship function and keep other user functions if any. # add multiple instances of the starship function and keep other user functions if any.
if [[ -z "$PROMPT_COMMAND" ]]; then if [[ -z "${PROMPT_COMMAND-}" ]]; then
PROMPT_COMMAND="starship_precmd" PROMPT_COMMAND="starship_precmd"
elif [[ "$PROMPT_COMMAND" != *"starship_precmd"* ]]; then elif [[ "$PROMPT_COMMAND" != *"starship_precmd"* ]]; then
# Appending to PROMPT_COMMAND breaks exit status ($?) checking. # Appending to PROMPT_COMMAND breaks exit status ($?) checking.