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 19, 2022
1 parent fe46a97 commit 01bfc4e
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 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,10 +150,10 @@ __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"
}

__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 @@ -252,7 +256,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 @@ -266,7 +270,7 @@ __bp_preexec_invoke_exec() {
# This function invokes every function defined in our function array. This
# function assigns the last error exit status 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 @@ -293,7 +297,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 @@ -322,8 +326,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 @@ -360,14 +364,14 @@ __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}
}

__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 @@ -377,7 +381,7 @@ __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
Expand Down

0 comments on commit 01bfc4e

Please sign in to comment.