bash-it/lib/preexec.bash

116 lines
3.5 KiB
Bash

# shellcheck shell=bash
# shellcheck disable=SC2034
#
# Load the `bash-preexec.sh` library, and define helper functions
## Prepare, load, fix, and install `bash-preexec.sh`
# Disable `$PROMPT_COMMAND` modification for now.
__bp_delay_install="delayed"
# shellcheck source-path=SCRIPTDIR/../vendor/github.com/rcaloras/bash-preexec
source "${BASH_IT?}/vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh"
# Block damanaging user's `$HISTCONTROL`
__bp_adjust_histcontrol()
# Don't fail on readonly variables
__bp_require_not_readonly()
# For performance, testing, and to avoid unexpected behavior: disable DEBUG traps in subshells.
# See bash-it/bash-it#1040 and rcaloras/bash-preexec#26
: "${__bp_enable_subshells:=}" # blank
# Modify `$PROMPT_COMMAND` in finalize hook
_bash_it_library_finalize_hook+=('__bp_install_after_session_init')
## Helper functions
function __check_precmd_conflict()
{
############ STACK_TRACE_BUILDER #####################
Function_Name="${FUNCNAME[0]}"
Function_PATH="${Function_PATH}/${Function_Name}"
######################################################
local f
__bp_trim_whitespace f "${1?}"
_bash-it-array-contains-element "${f}" "${precmd_functions[@]}"
############### Stack_TRACE_BUILDER ################
Function_PATH="$( dirname ${Function_PATH} )"
####################################################
}
function __check_preexec_conflict()
{
############ STACK_TRACE_BUILDER #####################
Function_Name="${FUNCNAME[0]}"
Function_PATH="${Function_PATH}/${Function_Name}"
######################################################
local f
__bp_trim_whitespace f "${1?}"
_bash-it-array-contains-element "${f}" "${preexec_functions[@]}"
############### Stack_TRACE_BUILDER ################
Function_PATH="$( dirname ${Function_PATH} )"
####################################################
}
function safe_append_prompt_command()
{
############ STACK_TRACE_BUILDER #####################
Function_Name="${FUNCNAME[0]}"
Function_PATH="${Function_PATH}/${Function_Name}"
######################################################
local prompt_re prompt_er f
if [[ "${bash_preexec_imported:-${__bp_imported:-missing}}" == "defined" ]]
then
# We are using bash-preexec
__bp_trim_whitespace f "${1?}"
if ! __check_precmd_conflict "${f}"
then
precmd_functions+=("${f}")
fi
else
# Match on word-boundaries
prompt_re='(^|[^[:alnum:]_])'
prompt_er='([^[:alnum:]_]|$)'
if [[ ${PROMPT_COMMAND} =~ ${prompt_re}"${1}"${prompt_er} ]]
then
return
elif [[ -z ${PROMPT_COMMAND} ]]
then
PROMPT_COMMAND="${1}"
else
PROMPT_COMMAND="${1};${PROMPT_COMMAND}"
fi
fi
############### Stack_TRACE_BUILDER ################
Function_PATH="$( dirname ${Function_PATH} )"
####################################################
}
function safe_append_preexec()
{
############ STACK_TRACE_BUILDER #####################
Function_Name="${FUNCNAME[0]}"
Function_PATH="${Function_PATH}/${Function_Name}"
######################################################
local prompt_re f
if [[ "${bash_preexec_imported:-${__bp_imported:-missing}}" == "defined" ]]
then
# We are using bash-preexec
__bp_trim_whitespace f "${1?}"
if ! __check_preexec_conflict "${f}"
then
preexec_functions+=("${f}")
fi
else
_log_error "${FUNCNAME[0]}: can't append to preexec hook because _bash-preexec.sh_ hasn't been loaded"
fi
############### Stack_TRACE_BUILDER ################
Function_PATH="$( dirname ${Function_PATH} )"
####################################################
}