Skip to content

Commit 019f1d0

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 af9a377 commit 019f1d0

File tree

2 files changed

+63
-38
lines changed

2 files changed

+63
-38
lines changed

lib/preexec.bash

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Load the `bash-preexec.sh` library, and define helper functions
55

66
## Prepare, load, fix, and install `bash-preexec.sh`
7+
: "${PROMPT_COMMAND:=}"
78

89
# Disable immediate `$PROMPT_COMMAND` modification
910
__bp_delay_install="delayed"
@@ -23,3 +24,65 @@ set +T
2324

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

themes/base.theme.bash

Lines changed: 0 additions & 38 deletions
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)