Skip to content

Commit

Permalink
Fix command timing on Bash 5.1+ with LP not last
Browse files Browse the repository at this point in the history
If __lp_set_prompt() was not the last in the PROMPT_COMMAND array, the
command that ran right after it would get marked as the start of the
command, and the timing would begin then. Obviously, timing the time
spent typing a command is not very useful.

The fix is to set the AT_PROMPT when the very last PROMPT_COMMAND is
ran. This can be checked by looking at the last item in the array.

Unfortunately, this will not always work. It is possible to set an entry
in the PROMPT_COMMAND array to a compound command, like:
`my first command; my second command`

In this case, Bash will run the DEBUG trap twice, once with BASH_COMMAND
set to "my first command", and the second to "my second command". This
will fail our check, meaning the start time is never set.

The fix is to check for a BASH_COMMAND with the same ending as the last
PROMPT_COMMAND. It is possible this might match early in very special
circumstances, but even in that case, the last command will reset
everything without problems.
  • Loading branch information
Rycieos committed Mar 13, 2024
1 parent fa1fa98 commit 6f2ca7e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions liquidprompt
Expand Up @@ -4859,13 +4859,19 @@ __lp_before_command() {
fi
fi

# If this is when the prompt is being drawn, the command is done,
# If this is the last thing before prompt is being drawn, the command is done,
# so mark the next trap. Note these two events could be at the same
# time, so no elif is used.
if [[ "$BASH_COMMAND" == __lp_set_prompt ]]; then
_LP_AT_PROMPT=1
local command
if (( ${BASH_VERSINFO[0]:-0} > 5 || ( ${BASH_VERSINFO[0]:-0} == 5 && ${BASH_VERSINFO[1]:-0} >= 1 ) )); then
command=${PROMPT_COMMAND[-1]}
else
command=$PROMPT_COMMAND
fi

if [[ "$command" == *"$BASH_COMMAND" ]]; then
_LP_AT_PROMPT=1
fi
}

prompt_tag() {
Expand Down

0 comments on commit 6f2ca7e

Please sign in to comment.