TheRepoClub-DotFiles/localbin/.local/bin/clipmenu/clipmenu
The-Repo-Club ade4f98505
DotFiles
2022-12-09 07:55:04 +00:00

80 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
#------------------------------------------------------------------------------
# Path - /usr/bin/clipmenu
# GitHub - https://github.com/The-Repo-Club/
# Author - The-Repo-Club [wayne6324@gmail.com]
# Start On - Tue 05 Oct 16:09:48 BST 2021
# Modified On - Sun 31 Oct 00:33:48 BST 2021
#------------------------------------------------------------------------------
: "${CM_LAUNCHER="rofi"}"
: "${CM_HISTLENGTH=15}"
shopt -s nullglob
cache_dir=$(clipmenu-ctl cache-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: dmenu)
- $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 clipmenu-daemon?'
exit 2
fi
# Blacklist of non-dmenu launchers
launcher_args=(-l "${CM_HISTLENGTH}")
if [[ "$CM_LAUNCHER" == fzf ]]; then
launcher_args=(--prompt="Copy to clipboard: " --border=rounded --margin=1% --color=dark --height 100% --reverse --header=" CLIPBOARD MENU " --info=hidden --header-first)
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" "${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
xsel --logfile /dev/null -i --"$selection" < "$file"
done
if (( CM_OUTPUT_CLIP )); then
cat "$file"
fi
exit "${launcher_exit:-"$?"}"