From 8d1475c59c06660b8d18b87362bf3cd1afead3d4 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 | 36 ++++++++++++++++++++++++++++++++++++ themes/base.theme.bash | 7 ++++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/lib/helpers.bash b/lib/helpers.bash index a528c14c0f..ccaa1e7b02 100755 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -837,3 +837,39 @@ then fi } fi + +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 4d6a1b7f51..b84c7ba3f6 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -611,7 +611,8 @@ function safe_append_prompt_command { fi } -function _save-and-reload-history() { - local autosave=${1:-0} - [[ $autosave -eq 1 ]] && history -a && history -c && history -r +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 }