diff --git a/lib/helpers.bash b/lib/helpers.bash index c638d78dea..f2b54c6422 100644 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -952,6 +952,38 @@ function safe_append_preexec { fi } -function _save-and-reload-history() { - [[ ${autosave:-0} -eq 1 ]] && history -a && history -c && history -r +function _bash_it_history_auto_save() +{ + case $HISTCONTROL in + *'noauto'*|*'autoload'*) + : # Do nothing, as configured. + ;; + *'auto'*) + # Append new history from this session to the $HISTFILE + history -a + ;; + *) + : # Do nothing, default. + ;; + esac +} + +function _bash_it_history_auto_load() +{ + case $HISTCONTROL in + *'noauto'*|*'autosave'*) + : # Do nothing, as configured. + ;; + *'autoloadnew'*) + # Read new entries from $HISTFILE + history -n + ;; + *'auto'*) + # Blank in-memory history, then read entire $HISTFILE fresh from disk. + history -c && history -r + ;; + *) + : # Do nothing, default. + ;; + esac } diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 62ab4b3b00..a1562378b7 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -579,3 +579,46 @@ function aws_profile { echo -e "default" 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}" == "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() { + [[ ${1:-${autosave:-${HISTORY_AUTOSAVE:-0}}} -eq 1 ]] && local HISTCONTROL="${HISTCONTROL:-}${HISTCONTROL:+:}autoshare" + _bash_it_history_auto_save && _bash_it_history_auto_load +}