Merge pull request #2003 from gaelicWizard/preexec

Consolidate `preexec`-related functions; Eliminate `vendor/init.d`
pull/2066/head
Noah Gorny 2022-01-26 17:34:46 +02:00 committed by GitHub
commit 3c2bc3d196
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 84 additions and 74 deletions

View File

@ -51,15 +51,6 @@ for _bash_it_config_file in $LIB; do
fi
done
# Load vendors
BASH_IT_LOG_PREFIX="vendor: "
for _bash_it_vendor_init in "${BASH_IT}"/vendor/init.d/*.bash; do
_log_debug "Loading \"$(basename "${_bash_it_vendor_init}" .bash)\"..."
# shellcheck disable=SC1090
source "${_bash_it_vendor_init}"
done
unset _bash_it_vendor_init
BASH_IT_LOG_PREFIX="core: main: "
# Load the global "enabled" directory
# "family" param is empty so that files get sources in glob order

View File

@ -82,6 +82,7 @@ completion/available/wpscan.completion.bash
# libraries
lib/helpers.bash
lib/log.bash
lib/preexec.bash
lib/utilities.bash
# plugins
@ -165,4 +166,3 @@ themes/purity
# vendor init files
#
vendor/.gitattributes
vendor/init.d

82
lib/preexec.bash 100644
View File

@ -0,0 +1,82 @@
# shellcheck shell=bash
# shellcheck disable=SC2034
#
# 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
__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`
function __bp_adjust_histcontrol() { :; }
# Don't fail on readonly variables
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
## Helper functions
function __check_precmd_conflict() {
local f
__bp_trim_whitespace f "${1?}"
! _bash-it-array-contains-element "${f}" "${precmd_functions[@]}"
}
function __check_preexec_conflict() {
local f
__bp_trim_whitespace f "${1?}"
! _bash-it-array-contains-element "${f}" "${preexec_functions[@]}"
}
function safe_append_prompt_command {
local prompt_re f
__bp_trim_whitespace f "${1?}"
if [ "${__bp_imported:-missing}" == "defined" ]; then
# We are using bash-preexec
if ! __check_precmd_conflict "${f}"; then
precmd_functions+=("${f}")
fi
else
# Set OS dependent exact match regular expression
if [[ ${OSTYPE} == darwin* ]]; then
# macOS
prompt_re="[[:<:]]${1}[[:>:]]"
else
# Linux, FreeBSD, etc.
prompt_re="\<${1}\>"
fi
if [[ ${PROMPT_COMMAND} =~ ${prompt_re} ]]; then
return
elif [[ -z ${PROMPT_COMMAND} ]]; then
PROMPT_COMMAND="${1}"
else
PROMPT_COMMAND="${1};${PROMPT_COMMAND}"
fi
fi
}
function safe_append_preexec {
local prompt_re f
__bp_trim_whitespace f "${1?}"
if [ "${__bp_imported:-missing}" == "defined" ]; then
# We are using bash-preexec
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
}

View File

@ -2,7 +2,6 @@
load ../test_helper
load ../test_helper_libs
load ../../vendor/init.d/preexec
load ../../plugins/available/cmd-returned-notify.plugin

View File

@ -4,3 +4,4 @@ load "${BASH_IT}/lib/log.bash"
load "${BASH_IT}/lib/utilities.bash"
load "${BASH_IT}/lib/helpers.bash"
load "${BASH_IT}/lib/search.bash"
load "${BASH_IT}/lib/preexec.bash"

View File

@ -583,44 +583,6 @@ function aws_profile {
fi
}
function __check_precmd_conflict() {
local f
for f in "${precmd_functions[@]}"; do
if [[ "${f}" == "${1}" ]]; then
return 0
fi
done
return 1
}
function safe_append_prompt_command {
local prompt_re
if [ "${__bp_imported:-missing}" == "defined" ]; then
# We are using bash-preexec
if ! __check_precmd_conflict "${1}"; then
precmd_functions+=("${1}")
fi
else
# Set OS dependent exact match regular expression
if [[ ${OSTYPE} == darwin* ]]; then
# macOS
prompt_re="[[:<:]]${1}[[:>:]]"
else
# Linux, FreeBSD, etc.
prompt_re="\<${1}\>"
fi
if [[ ${PROMPT_COMMAND[*]:-} =~ ${prompt_re} ]]; then
return
elif [[ -z ${PROMPT_COMMAND} ]]; then
PROMPT_COMMAND="${1}"
else
PROMPT_COMMAND="${1};${PROMPT_COMMAND}"
fi
fi
}
function _save-and-reload-history() {
local autosave=${1:-0}
[[ $autosave -eq 1 ]] && history -a && history -c && history -r

View File

@ -1,25 +0,0 @@
# 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 immediate `$PROMPT_COMMAND` modification
__bp_delay_install="delayed"
# shellcheck source-path=SCRIPTDIR/../github.com/rcaloras/bash-preexec
source "${BASH_IT?}/vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh"
# Block damanaging user's `$HISTCONTROL`
function __bp_adjust_histcontrol() { :; }
# Don't fail on readonly variables
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