Skip to content

Commit

Permalink
Improve handling of CM_MAX_CLIPS with duplicates between cache files
Browse files Browse the repository at this point in the history
Fixes #73.
  • Loading branch information
cdown committed Apr 7, 2018
1 parent 0b003d4 commit 0364087
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions clipmenud
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,22 @@ for file in /proc/self/fd/2 /dev/stderr; do
break
done

_sort() {
LC_ALL=C sort
}

get_clips_over() {
head -n -"$CM_MAX_CLIPS" "${1?cache file missing}" | strip_time | _sort
}

_xsel() {
timeout 1 xsel --logfile "$xsel_log" "$@"
}

strip_time() {
cut -d' ' -f2-
}

get_first_line() {
# Args:
# - $1, the file or data
Expand Down Expand Up @@ -221,9 +233,21 @@ while true; do
fi

if (( CM_MAX_CLIPS )) && [[ -f $cache_file ]]; then
# It's possible that this clip's content still exists in some other
# file, see issue #73. As such, we have to check the other files first.
all_cache_files=("$cache_file_prefix"_*)
files_to_compare=()
for file in "${all_cache_files[@]}"; do
[[ "$file" != "$cache_file" ]] && files_to_compare+=( "$file" )
done
mapfile -t to_remove < <(
head -n -"$CM_MAX_CLIPS" "$cache_file" |
while read -r line; do cksum <<< "${line#* }"; done
comm -23 \
<(get_clips_over "$cache_file") \
<(cat <(
for file in "${files_to_compare[@]}"; do
get_clips_over "$file"
done
)) | while read -r line; do cksum <<< "$line"; done
)
num_to_remove="${#to_remove[@]}"
if (( num_to_remove )); then
Expand Down

0 comments on commit 0364087

Please sign in to comment.