commit 4a0bd8f1f48f7a4262c02c71cb2a658a3fb7084d
parent e6c312b6394150384330c86a7c225030f96c1126
Author: Chris Down <chris@chrisdown.name>
Date: Tue, 24 Oct 2017 17:14:06 +0200
Only keep $CM_MAX_CLIPS newest clips
Closes #50.
Diffstat:
1 file changed, 15 insertions(+), 0 deletions(-)
diff --git a/clipmenud b/clipmenud
@@ -4,6 +4,7 @@
: "${CM_OWN_CLIPBOARD=1}"
: "${CM_DEBUG=0}"
: "${TMPDIR=/tmp}"
+: "${CM_MAX_CLIPS=1000}"
major_version=3
cache_dir=$TMPDIR/clipmenu.$major_version.$USER/
@@ -62,6 +63,7 @@ Environment variables:
- $CM_ONESHOT: run once immediately, do not loop (default: 0)
- $CM_DEBUG: turn on debugging output (default: 0)
- $CM_OWN_CLIPBOARD: take ownership of the clipboard (default: 1)
+- $CM_MAX_CLIPS: maximum number of clips to store, 0 for inf (default: 1000)
- $TMPDIR: specify the base directory to store the cache dir in (default: /tmp)
EOF
exit 0
@@ -143,6 +145,19 @@ while (( CM_ONESHOT )) || sleep "${CM_SLEEP:-0.5}"; do
# we would skip first.
_xsel -o --"$selection" | _xsel -i --"$selection"
fi
+
+ mapfile -t to_remove < <(
+ head -n -"$CM_MAX_CLIPS" "$cache_file" |
+ while read -r line; do cksum <<< "$line"; done
+ )
+ num_to_remove="${#to_remove[@]}"
+ if (( num_to_remove )); then
+ debug "Removing $num_to_remove old clips"
+ rm -- "${to_remove[@]/#/"$cache_dir/"}"
+ trunc_tmp=$(mktemp)
+ tail -n "$CM_MAX_CLIPS" "$cache_file" | uniq > "$trunc_tmp"
+ mv -- "$trunc_tmp" "$cache_file"
+ fi
done
flock -u "$lock_fd"