diff options
author | Koichi Murase <myoga.murase@gmail.com> | 2024-01-02 21:19:16 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-02 12:19:16 +0000 |
commit | 16309ca198e6b821aefee98e90587fabaf192796 (patch) | |
tree | c5d03d1d750915a82df565a6918b435036732c72 | |
parent | f44db9d7f90a268f245e705701c2968fa6092030 (diff) |
fix(bash): fix error by the use of ${PS1@P} in bash < 4.4 (#1488)
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.
-rw-r--r-- | atuin/src/shell/atuin.bash | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/atuin/src/shell/atuin.bash b/atuin/src/shell/atuin.bash index 1a5a6383..cf67034e 100644 --- a/atuin/src/shell/atuin.bash +++ b/atuin/src/shell/atuin.bash @@ -30,17 +30,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" @@ -87,8 +98,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() { |