Skip to content

Commit aecd522

Browse files
committed
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()`.
1 parent 51514e9 commit aecd522

File tree

2 files changed

+62
-38
lines changed

2 files changed

+62
-38
lines changed

lib/preexec.bash

+62
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,65 @@ set +T
2323

2424
# Modify `$PROMPT_COMMAND` now
2525
__bp_install_after_session_init
26+
27+
## Helper functions
28+
function __check_precmd_conflict() {
29+
local f # TODO: trim whitespace like preexec does
30+
for f in "${precmd_functions[@]}"; do
31+
if [[ "${f}" == "${1}" ]]; then
32+
return 0
33+
fi
34+
done
35+
return 1
36+
}
37+
38+
function __check_preexec_conflict() {
39+
local f # TODO: trim whitespace like preexec does
40+
for f in "${preexec_functions[@]}"; do
41+
if [[ "${f}" == "${1}" ]]; then
42+
return 0
43+
fi
44+
done
45+
return 1
46+
}
47+
48+
function safe_append_prompt_command {
49+
local prompt_re
50+
51+
if [ "${__bp_imported:-missing}" == "defined" ]; then
52+
# We are using bash-preexec
53+
if ! __check_precmd_conflict "${1}"; then
54+
precmd_functions+=("${1}")
55+
fi
56+
else
57+
# Set OS dependent exact match regular expression
58+
if [[ ${OSTYPE} == darwin* ]]; then
59+
# macOS
60+
prompt_re="[[:<:]]${1}[[:>:]]"
61+
else
62+
# Linux, FreeBSD, etc.
63+
prompt_re="\<${1}\>"
64+
fi
65+
66+
if [[ ${PROMPT_COMMAND} =~ ${prompt_re} ]]; then
67+
return
68+
elif [[ -z ${PROMPT_COMMAND} ]]; then
69+
PROMPT_COMMAND="${1}"
70+
else
71+
PROMPT_COMMAND="${1};${PROMPT_COMMAND}"
72+
fi
73+
fi
74+
}
75+
76+
function safe_append_preexec {
77+
local prompt_re
78+
79+
if [ "${__bp_imported:-missing}" == "defined" ]; then
80+
# We are using bash-preexec
81+
if ! __check_preexec_conflict "${1}"; then
82+
preexec_functions+=("${1}")
83+
fi
84+
else
85+
: #can't...
86+
fi
87+
}

themes/base.theme.bash

-38
Original file line numberDiff line numberDiff line change
@@ -581,44 +581,6 @@ function aws_profile {
581581
fi
582582
}
583583

584-
function __check_precmd_conflict() {
585-
local f
586-
for f in "${precmd_functions[@]}"; do
587-
if [[ "${f}" == "${1}" ]]; then
588-
return 0
589-
fi
590-
done
591-
return 1
592-
}
593-
594-
function safe_append_prompt_command {
595-
local prompt_re
596-
597-
if [ "${__bp_imported:-missing}" == "defined" ]; then
598-
# We are using bash-preexec
599-
if ! __check_precmd_conflict "${1}"; then
600-
precmd_functions+=("${1}")
601-
fi
602-
else
603-
# Set OS dependent exact match regular expression
604-
if [[ ${OSTYPE} == darwin* ]]; then
605-
# macOS
606-
prompt_re="[[:<:]]${1}[[:>:]]"
607-
else
608-
# Linux, FreeBSD, etc.
609-
prompt_re="\<${1}\>"
610-
fi
611-
612-
if [[ ${PROMPT_COMMAND[*]:-} =~ ${prompt_re} ]]; then
613-
return
614-
elif [[ -z ${PROMPT_COMMAND} ]]; then
615-
PROMPT_COMMAND="${1}"
616-
else
617-
PROMPT_COMMAND="${1};${PROMPT_COMMAND}"
618-
fi
619-
fi
620-
}
621-
622584
function _save-and-reload-history() {
623585
local autosave=${1:-0}
624586
[[ $autosave -eq 1 ]] && history -a && history -c && history -r

0 commit comments

Comments
 (0)