clipmenu
#!/bin/bash
: "${CM_LAUNCHER=dmenu}"
: "${CM_DIR="${XDG_RUNTIME_DIR-"${TMPDIR-/tmp}"}"}"
major_version=5
shopt -s nullglob
cache_dir=$CM_DIR/clipmenu.$major_version.$USER
cache_file_prefix=$cache_dir/line_cache
if [[ == --help ]] || [[ == -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_LAUNCHER: specify a dmenu-compatible launcher (default: dmenu)
- $CM_DIR: specify the base directory to store the cache dir in (default: $XDG_RUNTIME_DIR, $TMPDIR, or /tmp)
EOF
exit 0
fi
if [[ "$CM_LAUNCHER" == rofi ]]; then
# rofi supports dmenu-like arguments through the -dmenu flag
set -- -dmenu "$@"
fi
# It's okay to hardcode `-l 8` here as a sensible default without checking
# whether `-l` is also in "$@", because the way that dmenu works allows a later
# argument to override an earlier one. That is, if the user passes in `-l`, our
# one will be ignored.
chosen_line=$(
cat "$cache_file_prefix"_* /dev/null | sort -rnk 1 | cut -d' ' -f2- |
awk '!seen[{{&blob}}]++' | "$CM_LAUNCHER" -l 8 "$@"
)
[[ $chosen_line ]] || exit 1
file=$cache_dir/$(cksum <<< "$chosen_line")
if ! [[ -f "$file" ]]; then
# We didn't find this in cache
printf 'FATAL: %s not in cache\n' "$chosen_line" >&2
exit 2
fi
for selection in clipboard primary; do
xsel --logfile /dev/stderr -i --"$selection" < "$file"
done