git-vendor-name: preexec
git-vendor-dir: vendor/github.com/rcaloras/bash-preexec
git-vendor-repository: https://github.com/rcaloras/bash-preexec
git-vendor-ref: fd2ffa8876d3940c97ffdc3cc807e43277cf72da
pull/2073/head
John D Pell 2022-01-28 12:13:43 -08:00
parent 187916d903
commit 6b08284928
2 changed files with 24 additions and 16 deletions

View File

@ -4,9 +4,8 @@
# Load the `bash-preexec.sh` library, and define helper functions
## Prepare, load, fix, and install `bash-preexec.sh`
: "${PROMPT_COMMAND:=}"
# Disable immediate `$PROMPT_COMMAND` modification
# Disable `$PROMPT_COMMAND` modification for now.
__bp_delay_install="delayed"
# shellcheck source-path=SCRIPTDIR/../vendor/github.com/rcaloras/bash-preexec
@ -20,7 +19,6 @@ function __bp_require_not_readonly() { :; }
# Disable trap DEBUG on subshells - https://github.com/Bash-it/bash-it/pull/1040
__bp_enable_subshells= # blank
set +T
# Modify `$PROMPT_COMMAND` now
__bp_install_after_session_init
@ -42,7 +40,7 @@ function safe_append_prompt_command {
local prompt_re f
__bp_trim_whitespace f "${1?}"
if [ "${__bp_imported:-missing}" == "defined" ]; then
if [ "${bash_preexec_imported:-${__bp_imported:-missing}}" == "defined" ]; then
# We are using bash-preexec
if ! __check_precmd_conflict "${f}"; then
precmd_functions+=("${f}")
@ -71,7 +69,7 @@ function safe_append_preexec {
local prompt_re f
__bp_trim_whitespace f "${1?}"
if [ "${__bp_imported:-missing}" == "defined" ]; then
if [ "${bash_preexec_imported:-${__bp_imported:-missing}}" == "defined" ]; then
# We are using bash-preexec
if ! __check_preexec_conflict "${f}"; then
preexec_functions+=("${f}")

View File

@ -32,11 +32,20 @@
# using: the "DEBUG" trap, and the "PROMPT_COMMAND" variable. If you override
# either of these after bash-preexec has been installed it will most likely break.
# Make sure this is bash that's running and return otherwise.
if [[ -z "${BASH_VERSION:-}" ]]; then
return 1;
fi
# Avoid duplicate inclusion
if [[ "${__bp_imported:-}" == "defined" ]]; then
if [[ -n "${bash_preexec_imported:-}" ]]; then
return 0
fi
__bp_imported="defined"
bash_preexec_imported="defined"
# WARNING: This variable is no longer used and should not be relied upon.
# Use ${bash_preexec_imported} instead.
__bp_imported="${bash_preexec_imported}"
# Should be available to each precmd and preexec
# functions, should they want it. $? and $_ are available as $? and $_, but
@ -70,7 +79,8 @@ __bp_require_not_readonly() {
# history even if it starts with a space.
__bp_adjust_histcontrol() {
local histcontrol
histcontrol="${HISTCONTROL//ignorespace}"
histcontrol="${HISTCONTROL:-}"
histcontrol="${histcontrol//ignorespace}"
# Replace ignoreboth with ignoredups
if [[ "$histcontrol" == *"ignoreboth"* ]]; then
histcontrol="ignoredups:${histcontrol//ignoreboth}"
@ -85,6 +95,10 @@ __bp_adjust_histcontrol() {
# and unset as soon as the trace hook is run.
__bp_preexec_interactive_mode=""
# These arrays are used to add functions to be run before, or after, prompts.
declare -a precmd_functions
declare -a preexec_functions
# Trims leading and trailing whitespace from $2 and writes it to the variable
# name passed as $1
__bp_trim_whitespace() {
@ -154,7 +168,7 @@ __bp_set_ret_value() {
__bp_in_prompt_command() {
local prompt_command_array
IFS=$'\n;' read -rd '' -a prompt_command_array <<< "$PROMPT_COMMAND"
IFS=$'\n;' read -rd '' -a prompt_command_array <<< "${PROMPT_COMMAND:-}"
local trimmed_arg
__bp_trim_whitespace trimmed_arg "${1:-}"
@ -292,7 +306,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//$__bp_install_string[;$'\n']}" # Edge case of appending to PROMPT_COMMAND
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}"
__bp_sanitize_string existing_prompt_command "$existing_prompt_command"
@ -318,17 +333,12 @@ __bp_install() {
# after our session has started. This allows bash-preexec to be included
# at any point in our bash profile.
__bp_install_after_session_init() {
# Make sure this is bash that's running this and return otherwise.
if [[ -z "${BASH_VERSION:-}" ]]; then
return 1;
fi
# bash-preexec needs to modify these variables in order to work correctly
# if it can't, just stop the installation
__bp_require_not_readonly PROMPT_COMMAND HISTCONTROL HISTTIMEFORMAT || return
local sanitized_prompt_command
__bp_sanitize_string sanitized_prompt_command "$PROMPT_COMMAND"
__bp_sanitize_string sanitized_prompt_command "${PROMPT_COMMAND:-}"
if [[ -n "$sanitized_prompt_command" ]]; then
PROMPT_COMMAND=${sanitized_prompt_command}$'\n'
fi;