From 068b8fd730d75ed4dd985a831d8635b25230bc9d Mon Sep 17 00:00:00 2001 From: John D Pell Date: Fri, 10 Sep 2021 12:13:22 -0700 Subject: [PATCH] lib/helpers: new functions `_bash_it_history_auto_*()` Two new functions `_bash_it_history_auto_save()` and `_bash_it_history_auto_load()`, which append new history to disk and load new history from disk, respectively. See bash-it/bash-it#1595 for discussion. --- lib/helpers.bash | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/helpers.bash b/lib/helpers.bash index 469d55f100..2930a5d251 100644 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -954,6 +954,44 @@ function safe_append_preexec { fi } -function _save-and-reload-history() { - [[ ${autosave:-0} -eq 1 ]] && history -a && history -c && history -r +function _save-and-reload-history() +{ + [[ ${autosave:-${HISTORY_AUTOSAVE:-0}} -eq 1 ]] && local HISTCONTROL="${HISTCONTROL:-}${HISTCONTROL:+:}autoshare" + _bash_it_history_auto_save && _bash_it_history_auto_load +} + +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 }