diff options
Diffstat (limited to '.local/bin/bashhistory')
-rwxr-xr-x | .local/bin/bashhistory | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/.local/bin/bashhistory b/.local/bin/bashhistory index 6320834..98f1d33 100755 --- a/.local/bin/bashhistory +++ b/.local/bin/bashhistory @@ -4,16 +4,14 @@ directory="$XDG_DATA_HOME/bash" persist="$directory/history" default="$HOME/.bash_history" current="$HISTFILE" +lockfile='/tmp/bashhistory_Ri5ki9ei.lock' _prune() { - sed --in-place '/^#/d' "$1" && # Remove all timestamps. - sed --in-place '/......../!d' "$1" && # Remove all commands 8 characters or less. - sort --unique "$1" | sponge "$1" # Remove all duplicate entries. + sed --in-place '/^#/d;/^ /d;/^\t/d;/.........../!d' "$1" && # Remove all timestamps, lines beginning with tabs/spaces, and commands less than 11 characters + sort --unique "$1" > "$1.tmp" && # Remove all duplicate entries. + mv --force "$1.tmp" "$1" > /dev/null 2>&1 } -mkdir --parents "$directory" -touch "$current" - _migrate() { [ -f "$default" ] && cat "$default" >>"$persist" && @@ -23,9 +21,17 @@ _migrate() { _store() { cat "$current" >> "$persist" && + cat "$current" >> "$persist.bak" && _prune "$persist" && true > "$current" } +[ -f "$lockfile" ] && exit; + +mkdir --parents "$directory" +touch "$current" "$lockfile" + +trap 'rm "$lockfile" > /dev/null 2>&1; trap - EXIT; exit' EXIT INT HUP + _migrate || true _store |