Skip to content

Commit

Permalink
fix(bash): fix error by the use of ${PS1@P} in bash < 4.4
Browse files Browse the repository at this point in the history
The parameter expansions for the prompt strings, `${PS1@P}`, is only
available in bash >= 4.4.  In Bash 4.3 or below w/ bash-preexec, the
current implementation produces error messages.  There is no way to
evaluate PS1 with bash < 4.4, so we give up the adjustments for
multiline prompts in bash < 4.4 in this patch.
  • Loading branch information
akinomyoga committed Jan 2, 2024
1 parent 7f44358 commit 794fde4
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions atuin/src/shell/atuin.bash
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,28 @@ __atuin_set_ret_value() {
return ${1:+"$1"}
}

# The expansion ${PS1@P} is available in bash >= 4.4.
if ((BASH_VERSINFO[0] >= 5 || BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 4)); then
__atuin_use_prompt_expansion=true
else
__atuin_use_prompt_expansion=false
fi

__atuin_accept_line() {
local __atuin_command=$1

# Reprint the prompt, accounting for multiple lines
local __atuin_prompt=${PS1@P}
local __atuin_prompt_offset
__atuin_prompt_offset=$(printf '%s' "$__atuin_prompt" | wc -l)
if ((__atuin_prompt_offset > 0)); then
tput cuu "$__atuin_prompt_offset"
if [[ $__atuin_use_prompt_expansion == true ]]; then
local __atuin_prompt=${PS1@P}
local __atuin_prompt_offset
__atuin_prompt_offset=$(printf '%s' "$__atuin_prompt" | wc -l)
if ((__atuin_prompt_offset > 0)); then
tput cuu "$__atuin_prompt_offset"
fi
printf '%s\n' "$__atuin_prompt$__atuin_command"
else
printf '%s\n' "\$ $__atuin_command"
fi
printf '%s\n' "$__atuin_prompt$__atuin_command"

# Add it to the bash history
history -s "$__atuin_command"
Expand Down Expand Up @@ -78,8 +89,10 @@ __atuin_accept_line() {
# Bash will redraw only the line with the prompt after we finish,
# so to work for a multiline prompt we need to print it ourselves,
# then go to the beginning of the last line.
__atuin_set_ret_value "${__bp_last_ret_value-}" "${__bp_last_argument_prev_command-}"
printf '%s\r' "${PS1@P}"
if [[ $__atuin_use_prompt_expansion == true ]]; then
__atuin_set_ret_value "${__bp_last_ret_value-}" "${__bp_last_argument_prev_command-}"
printf '%s\r' "${PS1@P}"
fi
}

__atuin_history() {
Expand Down

0 comments on commit 794fde4

Please sign in to comment.