lib/preexec: consolidate helper functions

Define the helper functions for `bash-preexec.sh` immediately after importing it, rather than in `lib/theme`.
- `__check_precmd_conflict()` and `save_append_prompt_command()` are generally useful and not theme-specific.
- Add matching `__check_preexec_conflict()` and `safe_append_preexec()`.
pull/2003/head
John D Pell 2021-12-28 23:58:34 -08:00 committed by John D Pell
parent 7a0b353cea
commit 9f146f937a
2 changed files with 63 additions and 38 deletions

View File

@ -4,6 +4,7 @@
# Load the `bash-preexec.sh` library, and define helper functions # Load the `bash-preexec.sh` library, and define helper functions
## Prepare, load, fix, and install `bash-preexec.sh` ## Prepare, load, fix, and install `bash-preexec.sh`
: "${PROMPT_COMMAND:=}"
# Disable immediate `$PROMPT_COMMAND` modification # Disable immediate `$PROMPT_COMMAND` modification
__bp_delay_install="delayed" __bp_delay_install="delayed"
@ -23,3 +24,65 @@ set +T
# Modify `$PROMPT_COMMAND` now # Modify `$PROMPT_COMMAND` now
__bp_install_after_session_init __bp_install_after_session_init
## Helper functions
function __check_precmd_conflict() {
local f # TODO: trim whitespace like preexec does
for f in "${precmd_functions[@]}"; do
if [[ "${f}" == "${1}" ]]; then
return 0
fi
done
return 1
}
function __check_preexec_conflict() {
local f # TODO: trim whitespace like preexec does
for f in "${preexec_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 safe_append_preexec {
local prompt_re
if [ "${__bp_imported:-missing}" == "defined" ]; then
# We are using bash-preexec
if ! __check_preexec_conflict "${1}"; then
preexec_functions+=("${1}")
fi
else
: #can't...
fi
}

View File

@ -583,44 +583,6 @@ function aws_profile {
fi 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() { function _save-and-reload-history() {
local autosave=${1:-0} local autosave=${1:-0}
[[ $autosave -eq 1 ]] && history -a && history -c && history -r [[ $autosave -eq 1 ]] && history -a && history -c && history -r