|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# requires imagemagick to generate thumbnails |
| 4 | + |
| 5 | +thumbnail_size=64 |
| 6 | +thumbnail_dir="${XDG_CACHE_HOME:-$HOME/.cache}/cliphist/thumbnails" |
| 7 | + |
| 8 | +cliphist_list=$(cliphist list) |
| 9 | +if [ -z "$cliphist_list" ]; then |
| 10 | + fuzzel -d --placeholder "cliphist: please store something first" --lines 0 |
| 11 | + rm -rf "$thumbnail_dir" |
| 12 | + exit |
| 13 | +fi |
| 14 | + |
| 15 | +[ -d "$thumbnail_dir" ] || mkdir -p "$thumbnail_dir" |
| 16 | + |
| 17 | +# Write square shaped thumbnail to cache if it doesn't exist |
| 18 | +read -r -d '' thumbnail <<EOF |
| 19 | +/^[0-9]+\s<meta http-equiv=/ { next } |
| 20 | +match(\$0, /^([0-9]+)\s(\[\[\s)?binary.*(jpg|jpeg|png|bmp)/, grp) { |
| 21 | + cliphist_item_id=grp[1] |
| 22 | + ext=grp[3] |
| 23 | + thumbnail_file=cliphist_item_id"."ext |
| 24 | + system("[ -f ${thumbnail_dir}/"thumbnail_file" ] || echo " cliphist_item_id "\\\\\t | cliphist decode | magick - -thumbnail ${thumbnail_size}^ -gravity center -extent ${thumbnail_size} ${thumbnail_dir}/"thumbnail_file) |
| 25 | + print \$0"\0icon\x1f${thumbnail_dir}/"thumbnail_file |
| 26 | + next |
| 27 | +} |
| 28 | +1 |
| 29 | +EOF |
| 30 | + |
| 31 | +item=$(echo "$cliphist_list" | gawk "$thumbnail" | fuzzel -d --placeholder "Search clipboard..." --counter --no-sort --with-nth 2) |
| 32 | +exit_code=$? |
| 33 | + |
| 34 | +# ALT+0 to clear history |
| 35 | +if [ "$exit_code" -eq 19 ]; then |
| 36 | + confirmation=$(echo -e "No\nYes" | fuzzel -d --placeholder "Delete history?" --lines 2) |
| 37 | + [ "$confirmation" == "Yes" ] && rm ~/.cache/cliphist/db && rm -rf "$thumbnail_dir" |
| 38 | +else |
| 39 | + [ -z "$item" ] || echo "$item" | cliphist decode | wl-copy |
| 40 | +fi |
| 41 | + |
| 42 | +# Delete cached thumbnails that are no longer in cliphist db |
| 43 | +find "$thumbnail_dir" -type f | while IFS= read -r thumbnail_file; do |
| 44 | + cliphist_item_id=$(basename "${thumbnail_file%.*}") |
| 45 | + if [ -z "$(grep <<< "$cliphist_list" "^${cliphist_item_id}\s\[\[ binary data")" ]; then |
| 46 | + rm "$thumbnail_file" |
| 47 | + fi |
| 48 | +done |
0 commit comments