clipmenu-spmenu/clipmenu
William Casarin 630e7a016c clipmenu: print selection to stdout
This allows you to use clipmenu in desktop scripts. For example you
could pipe the output of your narrowed selection to another command.

Signed-off-by: William Casarin <jb55@jb55.com>
2020-03-24 18:00:20 -07:00

60 lines
1.5 KiB
Bash
Executable file

#!/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
if [[ $1 == --help ]] || [[ $1 == -h ]]; 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: dmenu)
- $CM_OUTPUT_CLIP: if set, output clip selection to stdout
EOF
exit 0
fi
# 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" -l "${CM_HISTLENGTH}" "$@")
fi
[[ $chosen_line ]] || exit 1
file=$cache_dir/$(cksum <<< "$chosen_line")
[[ -f "$file" ]] || exit 2
for selection in clipboard primary; do
xsel --logfile /dev/null -i --"$selection" < "$file"
done
if (( CM_OUTPUT_CLIP )); then
cat "$file"
fi