saturnin-clip 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/bash
  2. . $(ffoom path)
  3. ffoo import config
  4. ffoo import pretty
  5. STORAGE_DIR="$SATURNIN_CACHE_HOME/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. rm_clip() {
  58. local clipname=$1
  59. local name=$(saturnin clip ls | saturnin dmenu | cut -d\ -f 1)
  60. rm -f $STORAGE_DIR/$name
  61. }
  62. rm_all() {
  63. test -n "$STORAGE_DIR" || die "storage directory is unset, aborting"
  64. test -d "$STORAGE_DIR" || return 0
  65. find "$STORAGE_DIR" -name "*.clip" | xargs rm -f
  66. rmdir "$STORAGE_DIR" 2>/dev/null | :
  67. }
  68. clipname=primary
  69. action=list
  70. while true; do case "$1" in
  71. save) action=save; shift ;;
  72. load) action=load; shift ;;
  73. ls) action=list; shift ;;
  74. rm) action=remove; shift ;;
  75. clean) action=clean; shift ;;
  76. -1) clipname=primary; shift ;;
  77. -2) clipname=secondary; shift ;;
  78. -c) clipname=clipboard; shift ;;
  79. "") break; ;;
  80. *) usage; ;;
  81. esac done
  82. debug "\$@='$@'"
  83. debug -v clipname action
  84. case $action in
  85. save) save_clip $clipname ;;
  86. load) load_clip $clipname ;;
  87. remove) rm_clip ;;
  88. clean) rm_all ;;
  89. list) lsclips ;;
  90. esac