diff --git a/lib/preexec.bash b/lib/preexec.bash index 1035b11f..bb90e1e7 100644 --- a/lib/preexec.bash +++ b/lib/preexec.bash @@ -4,6 +4,7 @@ # 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 __bp_delay_install="delayed" @@ -23,3 +24,65 @@ set +T # Modify `$PROMPT_COMMAND` now __bp_install_after_session_init + +## Helper functions +function __check_precmd_conflict() { + local f # TODO: trim whitespace like preexec does + for f in "${precmd_functions[@]}"; do + if [[ "${f}" == "${1}" ]]; then + return 0 + fi + done + return 1 +} + +function __check_preexec_conflict() { + local f # TODO: trim whitespace like preexec does + for f in "${preexec_functions[@]}"; do + if [[ "${f}" == "${1}" ]]; then + return 0 + fi + done + return 1 +} + +function safe_append_prompt_command { + local prompt_re + + if [ "${__bp_imported:-missing}" == "defined" ]; then + # We are using bash-preexec + if ! __check_precmd_conflict "${1}"; then + precmd_functions+=("${1}") + 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 + return + elif [[ -z ${PROMPT_COMMAND} ]]; then + PROMPT_COMMAND="${1}" + else + PROMPT_COMMAND="${1};${PROMPT_COMMAND}" + fi + fi +} + +function safe_append_preexec { + local prompt_re + + if [ "${__bp_imported:-missing}" == "defined" ]; then + # We are using bash-preexec + if ! __check_preexec_conflict "${1}"; then + preexec_functions+=("${1}") + fi + else + : #can't... + fi +} diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 9e4a2562..a7e99961 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -583,44 +583,6 @@ function aws_profile { fi } -function __check_precmd_conflict() { - local f - for f in "${precmd_functions[@]}"; do - if [[ "${f}" == "${1}" ]]; then - return 0 - fi - done - return 1 -} - -function safe_append_prompt_command { - local prompt_re - - if [ "${__bp_imported:-missing}" == "defined" ]; then - # We are using bash-preexec - if ! __check_precmd_conflict "${1}"; then - precmd_functions+=("${1}") - 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 - return - elif [[ -z ${PROMPT_COMMAND} ]]; then - PROMPT_COMMAND="${1}" - else - PROMPT_COMMAND="${1};${PROMPT_COMMAND}" - fi - fi -} - function _save-and-reload-history() { local autosave=${1:-0} [[ $autosave -eq 1 ]] && history -a && history -c && history -r