diff options
author | Koichi Murase <myoga.murase@gmail.com> | 2024-04-16 15:16:00 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-16 08:16:00 +0200 |
commit | fe1a8df6fa5121cc83b288cf054a8ed16631d335 (patch) | |
tree | ad2e5cc16aee66ce7167d7723312d214c0b2f7b9 | |
parent | 2716db6d32a704aac9eacfd2ca0a9297201c65f7 (diff) |
fix(bash): fix handling of the preserved DEBUG trap (used in Bash <= 4.3) (#5908)
* refactor(bash): use `STARSHIP_*` for the internal variable names
The current codebase uses `dbg_trap` to save the original DEBUG trap
in bash <= 4.3. However, the variable name possibly conflicts a user
variable since it is not prefixed by `_starship` or `starship_` or
`STARSHIP_`. In this patch, we rename `dbg_trap` to
`STARSHIP_DEBUG_TRAP` following other variables of
`STARSHIP_EXIT_STATUS` and `STARSHIP_PIPE_STATUS`. We also rename the
variable `_PRESERVED_PROMPT_COMMAND` to `STARSHIP_PROMPT_COMMAND`.
* fix(bash): correctly extract DEBUG trap with spaces
* fix(bash): evaluate DEBUG trap by eval
The current code executes the saved DEBUG trap just by
$_starship_dbg_trap. This causes various problems. The content of
the variable `_starship_dbg_trap` is unexpectedly subject to the word
splitting with the pathname expansions. Also, the needed shell
expansions and quote removal are not performed. With a custom IFS,
the content of the variable will be split in an unexpected way. The
saved DEBUG trap needs to be executed by eval "$_starship_dbg_trap".
-rw-r--r-- | src/init/starship.bash | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/src/init/starship.bash b/src/init/starship.bash index 1e6a972b0..e08a9fbd0 100644 --- a/src/init/starship.bash +++ b/src/init/starship.bash @@ -65,8 +65,8 @@ starship_precmd() { # command pipeline, which may rely on it. _starship_set_return "$STARSHIP_CMD_STATUS" - if [[ -n "${_PRESERVED_PROMPT_COMMAND-}" ]]; then - eval "$_PRESERVED_PROMPT_COMMAND" + if [[ -n "${STARSHIP_PROMPT_COMMAND-}" ]]; then + eval "$STARSHIP_PROMPT_COMMAND" fi local -a ARGS=(--terminal-width="${COLUMNS}" --status="${STARSHIP_CMD_STATUS}" --pipestatus="${STARSHIP_PIPE_STATUS[*]}" --jobs="${NUM_JOBS}") @@ -110,12 +110,13 @@ else # We want to avoid destroying an existing DEBUG hook. If we detect one, create # a new function that runs both the existing function AND our function, then # re-trap DEBUG to use this new function. This prevents a trap clobber. - dbg_trap="$(trap -p DEBUG | cut -d' ' -f3 | tr -d \')" - if [[ -z "$dbg_trap" ]]; then + eval "STARSHIP_DEBUG_TRAP=($(trap -p DEBUG))" + STARSHIP_DEBUG_TRAP=("${STARSHIP_DEBUG_TRAP[2]}") + if [[ -z "$STARSHIP_DEBUG_TRAP" ]]; then trap 'starship_preexec "$_"' DEBUG - elif [[ "$dbg_trap" != 'starship_preexec "$_"' && "$dbg_trap" != 'starship_preexec_all "$_"' ]]; then + elif [[ "$STARSHIP_DEBUG_TRAP" != 'starship_preexec "$_"' && "$STARSHIP_DEBUG_TRAP" != 'starship_preexec_all "$_"' ]]; then starship_preexec_all() { - local PREV_LAST_ARG=$1 ; $dbg_trap; starship_preexec; : "$PREV_LAST_ARG"; + local PREV_LAST_ARG=$1 ; eval -- "$STARSHIP_DEBUG_TRAP"; starship_preexec; : "$PREV_LAST_ARG"; } trap 'starship_preexec_all "$_"' DEBUG fi @@ -130,7 +131,7 @@ else # Prepending to PROMPT_COMMAND breaks "command duration" module. # So, we are preserving the existing PROMPT_COMMAND # which will be executed later in the starship_precmd function - _PRESERVED_PROMPT_COMMAND="$PROMPT_COMMAND" + STARSHIP_PROMPT_COMMAND="$PROMPT_COMMAND" PROMPT_COMMAND="starship_precmd" fi fi |