aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/bashhistory
diff options
context:
space:
mode:
Diffstat (limited to '.local/bin/bashhistory')
-rwxr-xr-x.local/bin/bashhistory37
1 files changed, 37 insertions, 0 deletions
diff --git a/.local/bin/bashhistory b/.local/bin/bashhistory
new file mode 100755
index 0000000..98f1d33
--- /dev/null
+++ b/.local/bin/bashhistory
@@ -0,0 +1,37 @@
+#!/bin/sh -eu
+
+directory="$XDG_DATA_HOME/bash"
+persist="$directory/history"
+default="$HOME/.bash_history"
+current="$HISTFILE"
+lockfile='/tmp/bashhistory_Ri5ki9ei.lock'
+
+_prune() {
+ 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
+}
+
+_migrate() {
+ [ -f "$default" ] &&
+ cat "$default" >>"$persist" &&
+ _prune "$persist" &&
+ rm "$default"
+}
+
+_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