Skip to content

Commit

Permalink
Merge pull request #2073 from gaelicWizard/lib/preexec
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahGorny authored Feb 19, 2022
2 parents 1c8ad2c + 8246794 commit 33bade2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
41 changes: 17 additions & 24 deletions lib/preexec.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
# Load the `bash-preexec.sh` library, and define helper functions

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

# Disable immediate `$PROMPT_COMMAND` modification
# Disable `$PROMPT_COMMAND` modification for now.
__bp_delay_install="delayed"

# shellcheck source-path=SCRIPTDIR/../vendor/github.com/rcaloras/bash-preexec
Expand All @@ -18,12 +17,12 @@ function __bp_adjust_histcontrol() { :; }
# Don't fail on readonly variables
function __bp_require_not_readonly() { :; }

# Disable trap DEBUG on subshells - https://github.com/Bash-it/bash-it/pull/1040
__bp_enable_subshells= # blank
set +T
# For performance, testing, and to avoid unexpected behavior: disable DEBUG traps in subshells.
# See bash-it/bash-it#1040 and rcaloras/bash-preexec#26
: "${__bp_enable_subshells:=}" # blank

# Modify `$PROMPT_COMMAND` now
__bp_install_after_session_init
# Modify `$PROMPT_COMMAND` in finalize hook
_bash_it_library_finalize_hook+=('__bp_install_after_session_init')

## Helper functions
function __check_precmd_conflict() {
Expand All @@ -38,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 [ "${__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 @@ -67,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 [ "${__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
32 changes: 21 additions & 11 deletions vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 33bade2

Please sign in to comment.