clipmenu-spmenu/clipmenu

75 lines
1.8 KiB
Plaintext
Raw Normal View History

2018-10-31 09:12:09 +01:00
#!/usr/bin/env bash
: "${CM_LAUNCHER=dmenu}"
: "${CM_DIR="${XDG_RUNTIME_DIR-"${TMPDIR-/tmp}"}"}"
: "${CM_HISTLENGTH=8}"
major_version=6
shopt -s nullglob
cache_dir=$CM_DIR/clipmenu.$major_version.$USER
cache_file=$cache_dir/line_cache
2020-10-19 02:54:31 +02:00
# Not -h, see #142
if [[ $1 == --help ]]; then
cat << 'EOF'
2017-03-19 08:52:55 +01:00
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)
2018-08-21 21:36:54 +02:00
- $CM_HISTLENGTH: specify the number of lines to show in dmenu/rofi (default: 8)
2018-08-21 21:39:17 +02:00
- $CM_LAUNCHER: specify a dmenu-compatible launcher (default: dmenu)
- $CM_OUTPUT_CLIP: if set, output clip selection to stdout
2017-03-19 08:52:55 +01:00
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
2020-03-24 14:49:38 +01:00
# rofi supports dmenu-like arguments through the -dmenu flag
[[ "$CM_LAUNCHER" == rofi ]] && set -- -dmenu "$@"
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
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
2017-01-06 15:02:47 +01:00
for selection in clipboard primary; do
xsel --logfile /dev/null -i --"$selection" < "$file"
done
if (( CM_OUTPUT_CLIP )); then
cat "$file"
fi
[ -n "$launcher_exit" ] && exit "$launcher_exit"