Skip to content

Commit

Permalink
Avoid showing the 130 error code after hitting ^C
Browse files Browse the repository at this point in the history
When a user hits ^C on command line to cancel entering a lengthy command,
$! in __lp_set_prompt is set to 130 (128 + SIGINT). Displaying this error
on the input line (especially in warning colours) makes little sense.
Therefore, liquidprompt should check if the command submitted is a real
command or is it part of PROMPT_COMMAND in which case the error does
not come from a user's command and should not be presented.
  • Loading branch information
steelman committed Apr 1, 2024
1 parent ff44057 commit ed06664
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions liquidprompt
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ __lp_source_config() {
LP_ENABLE_OS_VERSION=${LP_ENABLE_OS_VERSION:-1}
LP_ENABLE_HYPERLINKS=${LP_ENABLE_HYPERLINKS:-0}
LP_ENABLE_PATH=${LP_ENABLE_PATH:-1}
LP_HIDE_EMPTY_ERROR=${LP_HIDE_EMPTY_ERROR:-1}

LP_MARK_DEFAULT="${LP_MARK_DEFAULT:-$_LP_MARK_SYMBOL}"
LP_MARK_BATTERY="${LP_MARK_BATTERY:-""}"
Expand Down Expand Up @@ -4797,11 +4798,17 @@ _lp_default_theme_prompt() {
# Construct the prompt #
########################

_LP_REAL_COMMAND=0
__lp_set_prompt() {
# Display the return value of the last command, if different from zero
# As this get the last returned code, it should be called first
local -i lp_error="$?"

if (( ! _LP_REAL_COMMAND && LP_HIDE_EMPTY_ERROR )); then
lp_error=0
fi
_LP_REAL_COMMAND=0

if (( LP_ENABLE_RUNTIME || LP_ENABLE_RUNTIME_BELL )); then
__lp_runtime_after
fi
Expand Down Expand Up @@ -4847,16 +4854,31 @@ __lp_before_command() {
# For debugging
#printf 'XXX %s\n' "$BASH_COMMAND"

local bash_command="$BASH_COMMAND"

# If this is the first time after the user submitted the command,
# execute the hooks.
if (( _LP_AT_PROMPT )); then
_LP_AT_PROMPT=0

# If this is the first command and it is in PROMPT_COMMAND, then probably the user has probably
# hit ^C and we should ignore $? in __lp_set_prompt and don't show the 130 error.
if (( ${BASH_VERSINFO[0]:-0} > 5 || ( ${BASH_VERSINFO[0]:-0} == 5 && ${BASH_VERSINFO[1]:-0} >= 1 ) )); then
# PROMPT_COMMAND is an array since bash 5.1
if ! ( __lp_array_contains "$bash_command" ${PROMPT_COMMAND[@]+"${PROMPT_COMMAND[@]}"} ); then
_LP_REAL_COMMAND=1
fi
else
if [[ "$PROMPT_COMMAND" != "$bash_command"* ]]; then
_LP_REAL_COMMAND=1
fi
fi

if (( LP_ENABLE_RUNTIME || LP_ENABLE_RUNTIME_BELL )); then
__lp_runtime_before
fi

if (( LP_ENABLE_TITLE_COMMAND )); then
if (( _LP_REAL_COMMAND && LP_ENABLE_TITLE_COMMAND )); then
__lp_print_title_command
fi
fi
Expand All @@ -4876,6 +4898,10 @@ __lp_before_command() {
fi
}

__lp_run_real_command() {
_LP_REAL_COMMAND=1
}

prompt_tag() {
if [[ -n "${1-}" ]]; then
export LP_PS1_PREFIX="$1 "
Expand Down Expand Up @@ -4928,8 +4954,10 @@ prompt_on() {
PROMPT_COMMAND="$set_prompt_command"
fi

if (( LP_ENABLE_RUNTIME || LP_ENABLE_RUNTIME_BELL || LP_ENABLE_TITLE_COMMAND )); then
if (( LP_ENABLE_RUNTIME || LP_ENABLE_RUNTIME_BELL || LP_ENABLE_TITLE_COMMAND \
|| LP_HIDE_EMPTY_ERROR )); then
_LP_AT_PROMPT=0
_LP_REAL_COMMAND=0
_LP_RUNTIME_LAST_SECONDS=$SECONDS
# __lp_before_command gets called just before bash executes a command,
# including $PROMPT_COMMAND
Expand Down Expand Up @@ -4999,6 +5027,9 @@ prompt_on() {
if (( LP_ENABLE_TITLE_COMMAND )); then
add-zsh-hook preexec __lp_print_title_command
fi
if (( LP_HIDE_EMPTY_ERROR )); then
add-zsh-hook preexec __lp_run_real_command
fi
fi
}

Expand Down Expand Up @@ -5037,7 +5068,8 @@ __lp_disable_hooks() {
fi

# Disable the DEBUG trap used by the RUNTIME or TITLE_COMMAND features
if (( ${LP_ENABLE_RUNTIME-0} || ${LP_ENABLE_RUNTIME_BELL-0} || ${LP_ENABLE_TITLE_COMMAND-0} )); then
if (( ${LP_ENABLE_RUNTIME-0} || ${LP_ENABLE_RUNTIME_BELL-0} || ${LP_ENABLE_TITLE_COMMAND-0} \
|| ${LP_HIDE_EMPTY_ERROR-0} )); then
trap - DEBUG
fi
fi
Expand All @@ -5048,6 +5080,7 @@ __lp_disable_hooks() {
add-zsh-hook -d precmd __lp_set_prompt
add-zsh-hook -d preexec __lp_runtime_before
add-zsh-hook -d preexec __lp_print_title_command
add-zsh-hook -d preexec __lp_run_real_command
} >/dev/null
fi
}
Expand Down

0 comments on commit ed06664

Please sign in to comment.