Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.7 KiB

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