Skip to content

Commit

Permalink
Fix clipdel cutting timestamps from line cache files (#94)
Browse files Browse the repository at this point in the history
Clipdel `cut`ed the timestamp column from the line cache files.

For the end-user, it fixes clipdel apparently re-ordering clipmenu
entries.
  • Loading branch information
Gravemind authored and cdown committed Apr 28, 2019
1 parent 640e8a1 commit dc20b9c
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions clipdel
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ fi
raw_pattern=$1
esc_pattern=${raw_pattern//\#/'\#'}

# We use 2 separate sed commands so "esc_pattern" matches only the 'clip' text
# without the timestamp (e.g. $> clipdel '^delete_exact_match$')
sed_common_command="s#^[0-9]\+ ##;\\#${esc_pattern}#"

if ! [[ $raw_pattern ]]; then
printf '%s\n' 'No pattern provided, see --help' >&2
exit 2
Expand All @@ -60,8 +64,8 @@ if (( CM_REAL_DELETE )) && [[ "$raw_pattern" == ".*" ]]; then
exit 0
else
mapfile -t matches < <(
cat "${line_cache_files[@]}" | cut -d' ' -f2- | sort -u |
sed -n "\\#${esc_pattern}#p"
sed -n "${sed_common_command}p" "${line_cache_files[@]}" |
sort -u
)

if (( CM_REAL_DELETE )); then
Expand All @@ -74,7 +78,11 @@ else

for file in "${line_cache_files[@]}"; do
temp=$(mktemp)
cut -d' ' -f2- < "$file" | sed "\\#${esc_pattern}#d" > "$temp"
# sed 'h' and 'g' here means save and restore the line, so
# timestamps are not removed from non-deleted lines. 'd' deletes the
# line and restarts, skipping 'g'/restore.
# https://www.gnu.org/software/sed/manual/html_node/Other-Commands.html#Other-Commands
sed "h;${sed_common_command}d;g" "$file" > "$temp"
mv -- "$temp" "$file"
done

Expand Down

0 comments on commit dc20b9c

Please sign in to comment.