Skip to content

Commit ae711c0

Browse files
authored
feat(bash): use PS0 for preexec hook (starship#5735)
1 parent 7290c5e commit ae711c0

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/init/starship.bash

+21-11
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ starship_precmd() {
7575
STARSHIP_END_TIME=$(::STARSHIP:: time)
7676
STARSHIP_DURATION=$((STARSHIP_END_TIME - STARSHIP_START_TIME))
7777
ARGS+=( --cmd-duration="${STARSHIP_DURATION}")
78-
unset STARSHIP_START_TIME
78+
STARSHIP_START_TIME=""
7979
fi
8080
PS1="$(::STARSHIP:: prompt "${ARGS[@]}")"
8181
if [[ ${BLE_ATTACHED-} ]]; then
@@ -98,17 +98,27 @@ elif [[ -n "${bash_preexec_imported:-}" || -n "${__bp_imported:-}" || -n "${pree
9898
preexec_functions+=(starship_preexec_all)
9999
precmd_functions+=(starship_precmd)
100100
else
101-
# We want to avoid destroying an existing DEBUG hook. If we detect one, create
102-
# a new function that runs both the existing function AND our function, then
103-
# re-trap DEBUG to use this new function. This prevents a trap clobber.
104-
dbg_trap="$(trap -p DEBUG | cut -d' ' -f3 | tr -d \')"
105-
if [[ -z "$dbg_trap" ]]; then
106-
trap 'starship_preexec "$_"' DEBUG
107-
elif [[ "$dbg_trap" != 'starship_preexec "$_"' && "$dbg_trap" != 'starship_preexec_all "$_"' ]]; then
108-
starship_preexec_all() {
109-
local PREV_LAST_ARG=$1 ; $dbg_trap; starship_preexec; : "$PREV_LAST_ARG";
101+
if [[ -n "${BASH_VERSION-}" ]] && [[ "${BASH_VERSINFO[0]}" -gt 4 || ( "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 ) ]]; then
102+
starship_preexec_ps0() {
103+
::STARSHIP:: time
110104
}
111-
trap 'starship_preexec_all "$_"' DEBUG
105+
# In order to set STARSHIP_START_TIME use an arithmetic expansion that evaluates to 0
106+
# To avoid printing anything, use the return value in an ${var:offset:length} substring expansion
107+
# with offset and length evaluating to 0.
108+
PS0='${STARSHIP_START_TIME:$((STARSHIP_START_TIME="$(starship_preexec_ps0)",STARSHIP_PREEXEC_READY=0,0)):0}'"${PS0-}"
109+
else
110+
# We want to avoid destroying an existing DEBUG hook. If we detect one, create
111+
# a new function that runs both the existing function AND our function, then
112+
# re-trap DEBUG to use this new function. This prevents a trap clobber.
113+
dbg_trap="$(trap -p DEBUG | cut -d' ' -f3 | tr -d \')"
114+
if [[ -z "$dbg_trap" ]]; then
115+
trap 'starship_preexec "$_"' DEBUG
116+
elif [[ "$dbg_trap" != 'starship_preexec "$_"' && "$dbg_trap" != 'starship_preexec_all "$_"' ]]; then
117+
starship_preexec_all() {
118+
local PREV_LAST_ARG=$1 ; $dbg_trap; starship_preexec; : "$PREV_LAST_ARG";
119+
}
120+
trap 'starship_preexec_all "$_"' DEBUG
121+
fi
112122
fi
113123

114124
# Finally, prepare the precmd function and set up the start time. We will avoid to

0 commit comments

Comments
 (0)