saturnin-clip 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/bash
  2. . $(ffoom path)
  3. ffoo import config
  4. ffoo import pretty
  5. STORAGE_DIR="$HOME/.local/share/saturnin/clips"
  6. usage() {
  7. usage_is "[ls]"
  8. usage_is "save [-1|-2|-c]"
  9. usage_is "load [-1|-2|-c]"
  10. }
  11. clipln() {
  12. #
  13. # Print desired clipboard(s) and \n
  14. #
  15. case $1 in
  16. primary|secondary|clipboard)
  17. xclip -o -selection $1 2>/dev/null
  18. ;;
  19. ALL)
  20. xclip -o -selection primary 2>/dev/null
  21. xclip -o -selection secondary 2>/dev/null
  22. xclip -o -selection clipboard 2>/dev/null
  23. ;;
  24. esac
  25. echo ""
  26. }
  27. save_clip() {
  28. local clipname=$1
  29. mkdir -p "$STORAGE_DIR" || die "could not create directory for saving"
  30. local path="$STORAGE_DIR/$(date +%Y%m%d-%H%M%S.clip)"
  31. clipln $clipname > "$path"
  32. }
  33. lsclips() {
  34. local clipname=$1
  35. local ft hint name
  36. test -d "$STORAGE_DIR" || return 0
  37. ls "$STORAGE_DIR/"*.clip 2>/dev/null \
  38. | while read name;
  39. do
  40. ft=$(file -b -i $name | cut -d\; -f1)
  41. case $ft in
  42. text/*)
  43. hint=$(head -c 80 $name | tr '\n' '↵')
  44. ;;
  45. *)
  46. hint=$(head -c 16 $name | hexdump -C | head -1)
  47. ;;
  48. esac
  49. echos "$(basename $name) || $ft || $hint"
  50. done
  51. }
  52. load_clip() {
  53. local clipname=$1
  54. local name=$(saturnin clip ls | saturnin dmenu | cut -d\ -f 1)
  55. cat $STORAGE_DIR/$name | xclip -i -selection $clipname
  56. }
  57. clipname=primary
  58. action=list
  59. while true; do case "$1" in
  60. save) action=save; shift ;;
  61. load) action=load; shift ;;
  62. ls) action=list; shift ;;
  63. -1) clipname=primary; shift ;;
  64. -2) clipname=secondary; shift ;;
  65. -c) clipname=clipboard; shift ;;
  66. "") break; ;;
  67. *) usage; ;;
  68. esac done
  69. debug "\$@='$@'"
  70. debug -v clipname action
  71. case $action in
  72. save) save_clip $clipname ;;
  73. load) load_clip $clipname ;;
  74. list) lsclips ;;
  75. esac