|
| 1 | +#!/usr/bin/bash |
| 2 | + |
| 3 | +# executes same behaviour as below but with support for images |
| 4 | +# |
| 5 | +# cliphist list | wofi --dmenu | cliphist decode |
| 6 | +# |
| 7 | +# produces thumbnails and stores them in XDG_CACHE_HOME |
| 8 | +# note: does NOT put in clipboard, call wl-copy yourself! |
| 9 | + |
| 10 | +# set up thumbnail directory |
| 11 | +thumb_dir="${XDG_CACHE_HOME:-$HOME/.cache}/cliphist/thumbs" |
| 12 | +mkdir -p "$thumb_dir" |
| 13 | + |
| 14 | +cliphist_list="$(cliphist list)" |
| 15 | + |
| 16 | +# delete thumbnails in cache but not in cliphist |
| 17 | +for thumb in "$thumb_dir"/*; do |
| 18 | + clip_id="${thumb##*/}" |
| 19 | + clip_id="${clip_id%.*}" |
| 20 | + check=$(rg <<< "$cliphist_list" "^$clip_id\s") |
| 21 | + if [ -z "$check" ]; then |
| 22 | + >&2 rm -v "$thumb" |
| 23 | + fi |
| 24 | +done |
| 25 | + |
| 26 | +# remove unnecessary image tags |
| 27 | +# create thumbnail if image not processed already |
| 28 | +# print escape sequence |
| 29 | +read -r -d '' prog <<EOF |
| 30 | +/^[0-9]+\s<meta http-equiv=/ { next } |
| 31 | +match(\$0, /^([0-9]+)\s(\[\[\s)?binary.*(jpg|jpeg|png|bmp)/, grp) { |
| 32 | + image = grp[1]"."grp[3] |
| 33 | + system("[ -f $thumb_dir/"image" ] || echo " grp[1] "\\\\\t | cliphist decode | convert - -resize '256x256>' $thumb_dir/"image ) |
| 34 | + print "img:$thumb_dir/"image |
| 35 | + next |
| 36 | +} |
| 37 | +1 |
| 38 | +EOF |
| 39 | + |
| 40 | +choice=$(gawk <<< $cliphist_list "$prog" | wofi -I --dmenu) |
| 41 | + |
| 42 | +# stop execution if nothing selected in wofi menu |
| 43 | +[ -z "$choice" ] && exit 1 |
| 44 | + |
| 45 | +if [ "${choice::4}" = "img:" ]; then |
| 46 | + thumb_file="${choice:4}" |
| 47 | + clip_id="${thumb_file##*/}" |
| 48 | + clip_id="${clip_id%.*}\t" |
| 49 | +else |
| 50 | + clip_id="${choice}" |
| 51 | +fi |
| 52 | + |
| 53 | +printf "$clip_id" | cliphist decode |
0 commit comments