lib/history: 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.
pull/1940/head
John D Pell 2022-01-24 21:37:04 -08:00
parent f6119567e8
commit 5d5858058e
4 changed files with 57 additions and 7 deletions

View File

@ -83,6 +83,7 @@ completion/available/wpscan.completion.bash
# libraries # libraries
lib/colors.bash lib/colors.bash
lib/helpers.bash lib/helpers.bash
lib/history.bash
lib/log.bash lib/log.bash
lib/preexec.bash lib/preexec.bash
lib/search.bash lib/search.bash

49
lib/history.bash 100644
View File

@ -0,0 +1,49 @@
# shellcheck shell=bash
#
# Functions for working with Bash's command history.
function _bash-it-history-init() {
safe_append_preexec '_bash-it-history-auto-save'
safe_append_prompt_command '_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
;;
*)
# Append *only* if shell option `histappend` has been enabled.
shopt -q histappend && history -a && return
;;
esac
}
function _bash-it-history-auto-load() {
case $HISTCONTROL in
*'noauto'*)
: # Do nothing, as configured.
;;
*'autosave'*)
# Append new history from this session to the $HISTFILE
history -a
;;
*'autoloadnew'*)
# Read new entries from $HISTFILE
history -n
;;
*'auto'*)
# Blank in-memory history, then read entire $HISTFILE fresh from disk.
history -a && history -c && history -r
;;
*)
: # Do nothing, default.
;;
esac
}
_bash_it_library_finalize_hook+=('_bash-it-history-init')

View File

@ -5,15 +5,14 @@ about-plugin 'improve history handling with sane defaults'
# variable when the shell exits, rather than overwriting the file. # variable when the shell exits, rather than overwriting the file.
shopt -s histappend shopt -s histappend
# erase duplicates; alternative option: HISTCONTROL=ignoredups # 'ignorespace': don't save command lines which begin with a space to history
: "${HISTCONTROL:=ignorespace:erasedups}" # 'erasedups' (alternative 'ignoredups'): don't save duplicates to history
# 'autoshare': automatically share history between multiple running shells
: "${HISTCONTROL:=ignorespace:erasedups:autoshare}"
# resize history to 100x the default (500) # resize history to 100x the default (500)
: "${HISTSIZE:=50000}" : "${HISTSIZE:=50000}"
# Flush history to disk after each command.
export PROMPT_COMMAND="history -a;${PROMPT_COMMAND}"
function top-history() { function top-history() {
about 'print the name and count of the most commonly run tools' about 'print the name and count of the most commonly run tools'

View File

@ -584,6 +584,7 @@ function aws_profile {
} }
function _save-and-reload-history() { function _save-and-reload-history() {
local autosave=${1:-0} local autosave="${1:-${HISTORY_AUTOSAVE:-0}}"
[[ $autosave -eq 1 ]] && history -a && history -c && history -r [[ ${autosave} -eq 1 ]] && local HISTCONTROL="${HISTCONTROL:-}${HISTCONTROL:+:}autoshare"
_bash-it-history-auto-save && _bash-it-history-auto-load
} }