saturnin-clip 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. usage_is "rm"
  11. usage_is "clean"
  12. }
  13. clipln() {
  14. #
  15. # Print desired clipboard(s) and \n
  16. #
  17. case $1 in
  18. primary|secondary|clipboard)
  19. xclip -o -selection $1 2>/dev/null
  20. ;;
  21. ALL)
  22. xclip -o -selection primary 2>/dev/null
  23. xclip -o -selection secondary 2>/dev/null
  24. xclip -o -selection clipboard 2>/dev/null
  25. ;;
  26. esac
  27. echo ""
  28. }
  29. save_clip() {
  30. local clipname=$1
  31. mkdir -p "$STORAGE_DIR" || die "could not create directory for saving"
  32. local path="$STORAGE_DIR/$(date +%Y%m%d-%H%M%S.clip)"
  33. clipln $clipname > "$path"
  34. }
  35. lsclips() {
  36. local clipname=$1
  37. local ft hint name
  38. test -d "$STORAGE_DIR" || return 0
  39. ls "$STORAGE_DIR/"*.clip 2>/dev/null \
  40. | while read name;
  41. do
  42. ft=$(file -b -i $name | cut -d\; -f1)
  43. case $ft in
  44. text/*)
  45. hint=$(head -c 80 $name | tr '\n' '↵')
  46. ;;
  47. *)
  48. hint=$(head -c 16 $name | hexdump -C | head -1)
  49. ;;
  50. esac
  51. echos "$(basename $name) || $ft || $hint"
  52. done
  53. }
  54. load_clip() {
  55. local clipname=$1
  56. local name=$(saturnin clip ls | saturnin dmenu | cut -d\ -f 1)
  57. cat $STORAGE_DIR/$name | xclip -i -selection $clipname
  58. }
  59. rm_clip() {
  60. local clipname=$1
  61. local name=$(saturnin clip ls | saturnin dmenu | cut -d\ -f 1)
  62. rm -f $STORAGE_DIR/$name
  63. }
  64. rm_all() {
  65. test -n "$STORAGE_DIR" || die "storage directory is unset, aborting"
  66. test -d "$STORAGE_DIR" || return 0
  67. find "$STORAGE_DIR" -name "*.clip" | xargs rm -f
  68. rmdir "$STORAGE_DIR" 2>/dev/null | :
  69. }
  70. clipname=primary
  71. action=list
  72. while true; do case "$1" in
  73. save) action=save; shift ;;
  74. load) action=load; shift ;;
  75. ls) action=list; shift ;;
  76. rm) action=remove; shift ;;
  77. clean) action=clean; shift ;;
  78. -1) clipname=primary; shift ;;
  79. -2) clipname=secondary; shift ;;
  80. -c) clipname=clipboard; shift ;;
  81. "") break ;;
  82. *) usage ;;
  83. esac done
  84. debug "\$@='$@'"
  85. debug -v clipname action
  86. case $action in
  87. save) save_clip $clipname ;;
  88. load) load_clip $clipname ;;
  89. remove) rm_clip ;;
  90. clean) rm_all ;;
  91. list) lsclips ;;
  92. esac