Skip to content

Commit

Permalink
plugin/history: no need to set a trap
Browse files Browse the repository at this point in the history
Instead of globbally clearing `$HISTTIMEFORMAT` and setting a return trap to re-enable it, just make it local to the function.

Also, set the defaults in a way that is happy with read-only parameters.

# Conflicts:
#	plugins/available/history.plugin.bash
  • Loading branch information
gaelicWizard committed Oct 21, 2021
1 parent e0903b2 commit 538aff4
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions plugins/available/history.plugin.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@
cite about-plugin
about-plugin 'improve history handling with sane defaults'

# append to bash_history if Terminal.app quits
# append to bash_history on exit.
shopt -s histappend

# erase duplicates; alternative option: export HISTCONTROL=ignoredups
export HISTCONTROL=${HISTCONTROL:-ignorespace:erasedups}
# erase duplicates; alternative option: HISTCONTROL=ignoredups
: "${HISTCONTROL:=ignorespace:erasedups}"

# resize history to 100x the default (500)
export HISTSIZE=${HISTSIZE:-50000}
: "${HISTSIZE:=50000}"

top-history() {
about 'print the name and count of the most commonly run tools'

if [[ -n $HISTTIMEFORMAT ]]; then
# To parse history we need a predictable format, which HISTTIMEFORMAT
# gets in the way of. So we unset it and set a trap to guarantee the
# user's environment returns to normal even if the pipeline below fails.
# shellcheck disable=SC2064
trap "export HISTTIMEFORMAT='$HISTTIMEFORMAT'" RETURN
unset HISTTIMEFORMAT
fi
# To parse history we need a predictable format, which HISTTIMEFORMAT
# gets in the way of. So we clear it locally.
local HISTTIMEFORMAT

history \
| awk '{
Expand Down

0 comments on commit 538aff4

Please sign in to comment.