summaryrefslogtreecommitdiff
path: root/scripts/Scripts/clipboard-primary
blob: 4d65f81b48c20e0085466bcec83101aa152b400b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env bash

tmp_dir="/tmp/cliphist-rofi-primary"
db_path="$HOME/.cache/cliphist/primary.db"

clipboard_mode() {
	if [ "$ROFI_RETV" = "0" ]; then
		rm -rf "$tmp_dir"
		mkdir -p "$tmp_dir"

		cliphist -db-path "$db_path" list | while IFS=$'\t' read -r id content; do
			if [[ "$content" == *"[[ binary data"* ]]; then
				ext="png"

				if [[ "$content" =~ image/(jpeg|jpg|png|bmp|gif|webp) ]]; then
					ext="${BASH_REMATCH[1]}"
				fi

				if [[ "$ext" == "jpeg" ]]; then
					ext="jpg"
				fi

				imgfile="$tmp_dir/$id.$ext"
				printf '%s' "$id" | cliphist -db-path "$db_path" decode > "$imgfile"
				printf '󰋩 IMAGE: %s\0icon\x1fthumbnail://%s\0info\x1f%s\n' "$id" "$imgfile" "$id"
			else
				printf '%s\0info\x1f%s\n' "$content" "$id"
			fi
		done
	else
		printf '%s' "$ROFI_INFO" | cliphist -db-path "$db_path" decode | wl-copy --primary
		exit 0
	fi
}

export -f clipboard_mode

if [ -z "$ROFI_RETV" ]; then
	rofi -modi "clipboard:$0" \
	     -show clipboard \
	     -show-icons \
	     -theme ~/.config/rofi/clipboard.rasi
else
	clipboard_mode "$@"
fi