Skip to content

Commit

Permalink
lib/preexec: the last remnants of the $OSTYPE have been swept away
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
gaelicWizard committed Feb 17, 2022
1 parent c194319 commit 8246794
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions lib/preexec.bash
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,20 @@ function __check_preexec_conflict() {
_bash-it-array-contains-element "${f}" "${preexec_functions[@]}"
}

function safe_append_prompt_command {
local prompt_re f
__bp_trim_whitespace f "${1?}"
function safe_append_prompt_command() {
local prompt_re prompt_er f

if [ "${bash_preexec_imported:-${__bp_imported:-missing}}" == "defined" ]; then
if [[ "${bash_preexec_imported:-${__bp_imported:-missing}}" == "defined" ]]; then
# We are using bash-preexec
__bp_trim_whitespace f "${1?}"
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
# Match on word-boundaries
prompt_re='(^|[^[:alnum:]_])'
prompt_er='([^[:alnum:]_]|$)'
if [[ ${PROMPT_COMMAND} =~ ${prompt_re}"${1}"${prompt_er} ]]; then
return
elif [[ -z ${PROMPT_COMMAND} ]]; then
PROMPT_COMMAND="${1}"
Expand All @@ -66,12 +60,12 @@ function safe_append_prompt_command {
fi
}

function safe_append_preexec {
function safe_append_preexec() {
local prompt_re f
__bp_trim_whitespace f "${1?}"

if [ "${bash_preexec_imported:-${__bp_imported:-missing}}" == "defined" ]; then
if [[ "${bash_preexec_imported:-${__bp_imported:-missing}}" == "defined" ]]; then
# We are using bash-preexec
__bp_trim_whitespace f "${1?}"
if ! __check_preexec_conflict "${f}"; then
preexec_functions+=("${f}")
fi
Expand Down

0 comments on commit 8246794

Please sign in to comment.