Skip to content

Commit

Permalink
Better handle edge-cases in $PROMPT_COMMAND (#128)
Browse files Browse the repository at this point in the history
Some libraries do things like `PROMPT_COMMAND="$PROMPT_COMMAND ; _new_addition"`, that's a space-semicolon-space. It would be counter-productive to try to account for all possibilities, so just swap the install string for a no-op.

Clean up no-op, it it's all that's left

Co-authored-by: Ryan Caloras <[email protected]>
  • Loading branch information
gaelicWizard and rcaloras authored Mar 7, 2023
1 parent a44754f commit 0c8fe46
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions bash-preexec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,16 @@ __bp_install() {
local existing_prompt_command
# Remove setting our trap install string and sanitize the existing prompt command string
existing_prompt_command="${PROMPT_COMMAND:-}"
existing_prompt_command="${existing_prompt_command//${__bp_install_string}[;$'\n']}" # Edge case of appending to PROMPT_COMMAND
existing_prompt_command="${existing_prompt_command//$__bp_install_string}"
# Edge case of appending to PROMPT_COMMAND
existing_prompt_command="${existing_prompt_command//$__bp_install_string/:}" # no-op
existing_prompt_command="${existing_prompt_command//$'\n':$'\n'/$'\n'}" # remove known-token only
existing_prompt_command="${existing_prompt_command//$'\n':;/$'\n'}" # remove known-token only
__bp_sanitize_string existing_prompt_command "$existing_prompt_command"

# Install our hooks in PROMPT_COMMAND to allow our trap to know when we've
# actually entered something.
PROMPT_COMMAND=$'__bp_precmd_invoke_cmd\n'
if [[ -n "$existing_prompt_command" ]]; then
if [[ "${existing_prompt_command:-:}" != ":" ]]; then
PROMPT_COMMAND+=${existing_prompt_command}$'\n'
fi;
PROMPT_COMMAND+='__bp_interactive_mode'
Expand Down

0 comments on commit 0c8fe46

Please sign in to comment.