From 5c58bd4e5a534a4b2561b69a458414315676078c Mon Sep 17 00:00:00 2001 From: Andelib Taref Srizon <88821308+IamAndelib@users.noreply.github.com> Date: Mon, 9 Jun 2025 11:43:06 +0600 Subject: [PATCH] Create clipboard-screenshot-preview.sh Screenshot thumbnail preview in wofi dmenu. --- clipboard-screenshot-preview.sh | 73 +++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 clipboard-screenshot-preview.sh diff --git a/clipboard-screenshot-preview.sh b/clipboard-screenshot-preview.sh new file mode 100644 index 0000000..b5816d3 --- /dev/null +++ b/clipboard-screenshot-preview.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash + +thumb_dir="${XDG_CACHE_HOME:-$HOME/.cache}/cliphist/thumbs" +mkdir -p "$thumb_dir" + +cliphist_list=$(cliphist list) + +declare -A existing_ids +while IFS=$'\t' read -r id _; do + [[ -n "$id" ]] && existing_ids["$id"]=1 +done <<< "$cliphist_list" + +for thumb_path in "$thumb_dir"/*; do + [[ -e "$thumb_path" ]] || continue + thumb_id="${thumb_path##*/}" + thumb_id="${thumb_id%.*}" + + if [[ -z "${existing_ids[$thumb_id]}" ]]; then + rm -f "$thumb_path" + fi +done + +processed_list=$(awk -v thumb_dir="$thumb_dir" ' + /^[0-9]+\s/dev/null" + + if (system(decode_cmd " | " convert_cmd) == 0) { + print id + print "img:" thumb_file + next + } + + # Fallback to text if conversion fails + rm -f thumb_file + } + { print } # Print non-image entries as-is +' <<< "$cliphist_list") + +choice=$(wofi -I --dmenu \ + -Dimage_size=200 \ + -Dcontent_halign=center \ + -Dline_wrap=word_char \ + -Dwidth=40% \ + -Ddynamic_lines=true \ \ + -p "Clipboard" \ + -k /dev/null <<< "$processed_list") + +[[ -z "$choice" ]] && exit 0 + +if [[ "$choice" == img:* ]]; then + notify-send "Clipboard" "Item cannot be copied (select the ID instead)" -i dialog-error + exit 1 +fi + +clip_id=$(echo "$choice" | awk 'NR==1 {print $1}') + +printf "%s\t" "$clip_id" | cliphist decode | wl-copy + +notify-send "Clipboard" "Item copied to clipboard" -i edit-copy