saturnin-clip 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/bin/bash
  2. . $(ffoom path)
  3. ffoo import config
  4. ffoo import pretty
  5. #
  6. # Where to store clips
  7. #
  8. STORAGE_DIR="$SATURNIN_CACHE_HOME/clips"
  9. usage() {
  10. usage_is "[ls]"
  11. usage_is "save [-1|-2|-c]"
  12. usage_is "load [-1|-2|-c]"
  13. usage_is "rm"
  14. usage_is "clean"
  15. }
  16. clipln() {
  17. #
  18. # Print desired clipboard(s) and \n
  19. #
  20. case $1 in
  21. primary|secondary|clipboard)
  22. xclip -o -selection $1 2>/dev/null
  23. ;;
  24. ALL)
  25. xclip -o -selection primary 2>/dev/null
  26. xclip -o -selection secondary 2>/dev/null
  27. xclip -o -selection clipboard 2>/dev/null
  28. ;;
  29. esac
  30. echo ""
  31. }
  32. save_clip() {
  33. #
  34. # Save single clip
  35. #
  36. local clipname=$1
  37. mkdir -p "$STORAGE_DIR" || die "could not create directory for saving"
  38. local path="$STORAGE_DIR/$(date +%Y%m%d-%H%M%S.clip)"
  39. clipln $clipname > "$path"
  40. }
  41. lsclips() {
  42. #
  43. # List clips with MIME types and hints
  44. #
  45. local clipname=$1
  46. local ft hint name
  47. test -d "$STORAGE_DIR" || return 0
  48. ls "$STORAGE_DIR/"*.clip 2>/dev/null \
  49. | while read name;
  50. do
  51. ft=$(file -b -i $name | cut -d\; -f1)
  52. case $ft in
  53. text/*)
  54. hint=$(head -c 80 $name | tr '\n' '↵')
  55. ;;
  56. *)
  57. hint=$(head -c 16 $name | hexdump -C | head -1)
  58. ;;
  59. esac
  60. echos "$(basename $name) || $ft || $hint"
  61. done
  62. }
  63. load_clip() {
  64. #
  65. # Load single clip of choice
  66. #
  67. local clipname=$1
  68. local name=$(saturnin clip ls | saturnin dmenu | cut -d\ -f 1)
  69. cat $STORAGE_DIR/$name | xclip -i -selection $clipname
  70. }
  71. rm_clip() {
  72. #
  73. # Remove single clip of choice
  74. #
  75. local clipname=$1
  76. local name=$(saturnin clip ls | saturnin dmenu | cut -d\ -f 1)
  77. rm -f $STORAGE_DIR/$name
  78. }
  79. rm_all() {
  80. #
  81. # Dump all clips
  82. #
  83. test -n "$STORAGE_DIR" || die "storage directory is unset, aborting"
  84. test -d "$STORAGE_DIR" || return 0
  85. find "$STORAGE_DIR" -name "*.clip" | xargs rm -f
  86. rmdir "$STORAGE_DIR" 2>/dev/null | :
  87. }
  88. clipname=primary
  89. action=list
  90. while true; do case "$1" in
  91. save) action=save; shift ;;
  92. load) action=load; shift ;;
  93. ls) action=list; shift ;;
  94. rm) action=remove; shift ;;
  95. clean) action=clean; shift ;;
  96. -1) clipname=primary; shift ;;
  97. -2) clipname=secondary; shift ;;
  98. -c) clipname=clipboard; shift ;;
  99. "") break ;;
  100. *) usage ;;
  101. esac done
  102. debug "\$@='$@'"
  103. debug -v clipname action
  104. case $action in
  105. save) save_clip $clipname ;;
  106. load) load_clip $clipname ;;
  107. remove) rm_clip ;;
  108. clean) rm_all ;;
  109. list) lsclips ;;
  110. esac