Skip to content

Commit

Permalink
Rename public functions and variables as "bash_preexec_*"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Feb 20, 2022
1 parent dcef4ad commit 1c82488
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions bash-preexec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ __bp_inside_precmd=0
__bp_inside_preexec=0

# Initial PROMPT_COMMAND string that is removed from PROMPT_COMMAND post __bp_install
__bp_install_string=$'__bp_trap_string="$(trap -p DEBUG)"\ntrap - DEBUG\n__bp_install'
bash_preexec_install_string=$'__bp_trap_string="$(trap -p DEBUG)"\ntrap - DEBUG\n__bp_install'

# WARNING: This variable is no longer used and should not be relied upon.
# Use ${bash_preexec_install_string} instead.
__bp_install_string="${bash_preexec_install_string}"

# The command string that is registered to the DEBUG trap.
__bp_trapdebug_string='__bp_preexec_invoke_exec "$_"'
bash_preexec_trapdebug_string='__bp_preexec_invoke_exec "$_"'

# Fails if any of the given variables are readonly
# Reference https://stackoverflow.com/a/4441178
Expand Down Expand Up @@ -146,13 +150,13 @@ __bp_precmd_invoke_cmd() {
return
fi
local __bp_inside_precmd=1
__bp_invoke_precmd_functions "$__bp_last_ret_value" "$__bp_last_argument_prev_command"
bash_preexec_invoke_precmd_functions "$__bp_last_ret_value" "$__bp_last_argument_prev_command"
}

# This function invokes every function defined in our function array
# "precmd_function". This function receives the arguments $1 and $2 for $? and
# $_, respectively, that will be set for the precmd functions.
__bp_invoke_precmd_functions() {
bash_preexec_invoke_precmd_functions() {
local __lastexit=$1 __lastarg=$2
# Invoke every function defined in our function array.
local precmd_function
Expand Down Expand Up @@ -255,7 +259,7 @@ __bp_preexec_invoke_exec() {
fi

local preexec_ret_value
__bp_invoke_preexec_functions "${__bp_last_ret_value:-}" "$__bp_last_argument_prev_command" "$this_command"
bash_preexec_invoke_preexec_functions "${__bp_last_ret_value:-}" "$__bp_last_argument_prev_command" "$this_command"

# Restore the last argument of the last executed command, and set the return
# value of the DEBUG trap to be the return code of the last preexec function
Expand All @@ -273,7 +277,7 @@ __bp_preexec_invoke_exec() {
# (corresponding to BASH_COMMAND in the DEBUG trap). This function assigns the
# last non-zero exit status from the preexec functions to the variable
# `preexec_ret_value`. If there is no error, preexec_ret_value is set to `0`.
__bp_invoke_preexec_functions() {
bash_preexec_invoke_preexec_functions() {
local __lastexit=$1 __lastarg=$2 __this_command=$3
local preexec_function
local preexec_function_ret_value
Expand All @@ -300,7 +304,7 @@ __bp_install() {
return 1;
fi

trap "$__bp_trapdebug_string" DEBUG
trap "$bash_preexec_trapdebug_string" DEBUG

# Preserve any prior DEBUG trap as a preexec function
local prior_trap=$(sed "s/[^']*'\(.*\)'[^']*/\1/" <<<"${__bp_trap_string:-}")
Expand Down Expand Up @@ -329,8 +333,8 @@ __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}"
existing_prompt_command="${existing_prompt_command//$bash_preexec_install_string[;$'\n']}" # Edge case of appending to PROMPT_COMMAND
existing_prompt_command="${existing_prompt_command//$bash_preexec_install_string}"
__bp_sanitize_string existing_prompt_command "$existing_prompt_command"

# Install our hooks in PROMPT_COMMAND to allow our trap to know when we've
Expand Down Expand Up @@ -367,15 +371,15 @@ __bp_install_after_session_init() {
if [[ -n "$sanitized_prompt_command" ]]; then
PROMPT_COMMAND=${sanitized_prompt_command}$'\n'
fi;
PROMPT_COMMAND+=${__bp_install_string}
PROMPT_COMMAND+=${bash_preexec_install_string}
}

# Remove hooks installed in the DEBUG trap and PROMPT_COMMAND.
__bp_uninstall() {
bash_preexec_uninstall() {
# Remove __bp_install hook from PROMPT_COMMAND
if [[ ${PROMPT_COMMAND-} == *"$__bp_install_string"* ]]; then
PROMPT_COMMAND="${PROMPT_COMMAND//$__bp_install_string[;$'\n']}" # Edge case of appending to PROMPT_COMMAND
PROMPT_COMMAND="${PROMPT_COMMAND//$__bp_install_string}"
if [[ ${PROMPT_COMMAND-} == *"$bash_preexec_install_string"* ]]; then
PROMPT_COMMAND="${PROMPT_COMMAND//$bash_preexec_install_string[;$'\n']}" # Edge case of appending to PROMPT_COMMAND
PROMPT_COMMAND="${PROMPT_COMMAND//$bash_preexec_install_string}"
fi

# Remove precmd hook from PROMPT_COMMAND
Expand All @@ -385,15 +389,15 @@ __bp_uninstall() {

# Remove preexec hook in the DEBUG trap
local q="'" Q="'\''"
if [[ $(trap -p DEBUG) == "trap -- '${__bp_trapdebug_string//$q/$Q}' DEBUG" ]]; then
if [[ $(trap -p DEBUG) == "trap -- '${bash_preexec_trapdebug_string//$q/$Q}' DEBUG" ]]; then
if [[ ${__bp_trap_string-} ]]; then
eval -- "$__bp_trap_string"
else
trap - DEBUG
fi
fi
}
declare -ft __bp_uninstall
declare -ft bash_preexec_uninstall

# Run our install so long as we're not delaying it.
if [[ -z "${__bp_delay_install:-}" ]]; then
Expand Down

0 comments on commit 1c82488

Please sign in to comment.