forked from cdown/clipmenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipmenu
executable file
·81 lines (65 loc) · 2.25 KB
/
clipmenu
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env bash
: "${CM_LAUNCHER=rofi}"
: "${CM_HISTLENGTH=8}"
shopt -s nullglob
cache_dir=$(clipctl cache-dir)
image_dir=$(clipctl image-dir)
cache_file=$cache_dir/line_cache
# Not -h, see #142
if [[ $1 == --help ]]; then
cat << 'EOF'
clipmenu is a simple clipboard manager using dmenu and xsel. Launch this
when you want to select a clip.
All arguments are passed through to dmenu itself.
Environment variables:
- $CM_DIR: specify the base directory to store the cache dir in (default: $XDG_RUNTIME_DIR, $TMPDIR, or /tmp)
- $CM_HISTLENGTH: specify the number of lines to show in dmenu/rofi (default: 8)
- $CM_LAUNCHER: specify a dmenu-compatible launcher (default: rofi)
- $CM_OUTPUT_CLIP: if set, output clip selection to stdout
EOF
exit 0
fi
if ! [[ -f "$cache_file" ]]; then
printf '%s\n' 'No cache file yet, did you run clipmenud?'
exit 2
fi
# Blacklist of non-dmenu launchers
launcher_args=(-l "${CM_HISTLENGTH}")
if [[ "$CM_LAUNCHER" == fzf ]]; then
launcher_args=()
fi
# rofi supports dmenu-like arguments through the -dmenu flag. -p wastes space
# in real dmenu, but rofi shows "dmenu:" anyway, so pass it here only.
#[[ "$CM_LAUNCHER" == rofi ]] && set -- -dmenu -p clipmenu "$@"
list_clips() {
LC_ALL=C sort -rnk 1 < "$cache_file" | cut -d' ' -f2- | awk '!seen[$0]++'
}
if [[ "$CM_LAUNCHER" == rofi-script ]]; then
if (( $# )); then
chosen_line="${!#}"
else
list_clips
exit
fi
elif [[ "$CM_LAUNCHER" == rofi ]];then
chosen_line=$(list_clips | while read A ; do if [[ "$A" == $image_dir/clip_*.png ]] && [[ -e "$A" ]];then echo -en "$A\x00icon\x1f$A\n"; else echo "$A"; fi; done | "$CM_LAUNCHER" -dmenu -p clipmenu "${launcher_args[@]}")
launcher_exit=$?
else
chosen_line=$(list_clips | "$CM_LAUNCHER" "${launcher_args[@]}" "$@")
launcher_exit=$?
fi
[[ $chosen_line ]] || exit 1
file=$cache_dir/$(cksum <<< "$chosen_line")
[[ -f "$file" ]] || exit 2
for selection in clipboard primary; do
content=$(cat "$file")
if [[ "$content" == $image_dir/clip_*.png ]] && [[ -e "$content" ]];then
xclip -se "$selection" -t image/png -i < "$content"
else
xclip -se "$selection" -i < "$file"
fi
done
if (( CM_OUTPUT_CLIP )); then
cat "$file"
fi
exit "${launcher_exit:-"$?"}"