clipmenu

Simple clipboard management using dmenu
git clone https://git.sinitax.com/cdown/clipmenu
Log | Files | Refs | README | LICENSE | sfeed.txt

clipmenu (1822B)


      1#!/usr/bin/env bash
      2
      3: "${CM_LAUNCHER=dmenu}"
      4: "${CM_HISTLENGTH=8}"
      5
      6shopt -s nullglob
      7
      8cache_dir=$(clipctl cache-dir)
      9cache_file=$cache_dir/line_cache
     10
     11# Not -h, see #142
     12if [[ $1 == --help ]]; then
     13    cat << 'EOF'
     14clipmenu is a simple clipboard manager using dmenu and xsel. Launch this
     15when you want to select a clip.
     16
     17All arguments are passed through to dmenu itself.
     18
     19Environment variables:
     20
     21- $CM_DIR: specify the base directory to store the cache dir in (default: $XDG_RUNTIME_DIR, $TMPDIR, or /tmp)
     22- $CM_HISTLENGTH: specify the number of lines to show in dmenu/rofi (default: 8)
     23- $CM_LAUNCHER: specify a dmenu-compatible launcher (default: dmenu)
     24- $CM_OUTPUT_CLIP: if set, output clip selection to stdout
     25EOF
     26    exit 0
     27fi
     28
     29if ! [[ -f "$cache_file" ]]; then
     30    printf '%s\n' 'No cache file yet, did you run clipmenud?'
     31    exit 2
     32fi
     33
     34# Blacklist of non-dmenu launchers
     35launcher_args=(-l "${CM_HISTLENGTH}")
     36if [[ "$CM_LAUNCHER" == fzf ]]; then
     37    launcher_args=()
     38fi
     39
     40# rofi supports dmenu-like arguments through the -dmenu flag. -p wastes space
     41# in real dmenu, but rofi shows "dmenu:" anyway, so pass it here only.
     42[[ "$CM_LAUNCHER" == rofi ]] && set -- -dmenu -p clipmenu "$@"
     43
     44list_clips() {
     45    LC_ALL=C sort -rnk 1 < "$cache_file" | cut -d' ' -f2- | awk '!seen[$0]++'
     46}
     47
     48if [[ "$CM_LAUNCHER" == rofi-script ]]; then
     49    if (( $# )); then
     50        chosen_line="${!#}"
     51    else
     52        list_clips
     53        exit
     54    fi
     55else
     56    chosen_line=$(list_clips | "$CM_LAUNCHER" "${launcher_args[@]}" "$@")
     57    launcher_exit=$?
     58fi
     59
     60[[ $chosen_line ]] || exit 1
     61file=$cache_dir/$(cksum <<< "$chosen_line")
     62[[ -f "$file" ]] || exit 2
     63
     64for selection in clipboard primary; do
     65    xsel --logfile /dev/null -i --"$selection" < "$file"
     66done
     67
     68if (( CM_OUTPUT_CLIP )); then
     69    cat "$file"
     70fi
     71
     72exit "${launcher_exit:-"$?"}"