Skip to content

Commit 8246794

Browse files
committed
lib/preexec: the last remnants of the $OSTYPE have been swept away
- Use a POSIX-compliant/portable extended regular expression to match on word-boundaries, rather than guessing which regex library `bash` was linked against. See https://stackoverflow.com/a/12696899/555333 for explanation and code suggestion.
1 parent c194319 commit 8246794

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

lib/preexec.bash

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,20 @@ function __check_preexec_conflict() {
3737
_bash-it-array-contains-element "${f}" "${preexec_functions[@]}"
3838
}
3939

40-
function safe_append_prompt_command {
41-
local prompt_re f
42-
__bp_trim_whitespace f "${1?}"
40+
function safe_append_prompt_command() {
41+
local prompt_re prompt_er f
4342

44-
if [ "${bash_preexec_imported:-${__bp_imported:-missing}}" == "defined" ]; then
43+
if [[ "${bash_preexec_imported:-${__bp_imported:-missing}}" == "defined" ]]; then
4544
# We are using bash-preexec
45+
__bp_trim_whitespace f "${1?}"
4646
if ! __check_precmd_conflict "${f}"; then
4747
precmd_functions+=("${f}")
4848
fi
4949
else
50-
# Set OS dependent exact match regular expression
51-
if [[ ${OSTYPE} == darwin* ]]; then
52-
# macOS
53-
prompt_re="[[:<:]]${1}[[:>:]]"
54-
else
55-
# Linux, FreeBSD, etc.
56-
prompt_re="\<${1}\>"
57-
fi
58-
59-
if [[ ${PROMPT_COMMAND} =~ ${prompt_re} ]]; then
50+
# Match on word-boundaries
51+
prompt_re='(^|[^[:alnum:]_])'
52+
prompt_er='([^[:alnum:]_]|$)'
53+
if [[ ${PROMPT_COMMAND} =~ ${prompt_re}"${1}"${prompt_er} ]]; then
6054
return
6155
elif [[ -z ${PROMPT_COMMAND} ]]; then
6256
PROMPT_COMMAND="${1}"
@@ -66,12 +60,12 @@ function safe_append_prompt_command {
6660
fi
6761
}
6862

69-
function safe_append_preexec {
63+
function safe_append_preexec() {
7064
local prompt_re f
71-
__bp_trim_whitespace f "${1?}"
7265

73-
if [ "${bash_preexec_imported:-${__bp_imported:-missing}}" == "defined" ]; then
66+
if [[ "${bash_preexec_imported:-${__bp_imported:-missing}}" == "defined" ]]; then
7467
# We are using bash-preexec
68+
__bp_trim_whitespace f "${1?}"
7569
if ! __check_preexec_conflict "${f}"; then
7670
preexec_functions+=("${f}")
7771
fi

0 commit comments

Comments
 (0)