saturnin-clip 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/bin/bash
  2. . "$(ffoom path)"
  3. ffoo import inigrep
  4. ffoo import pretty
  5. #
  6. # Where to store clips
  7. #
  8. STORAGE_DIR="$SATURNIN_CACHE_HOME/clips"
  9. usage() {
  10. mkusage "[ls]" \
  11. "save [-1|-2|-c]" \
  12. "load [-1|-2|-c]" \
  13. "rm" \
  14. "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 ft hint name
  46. test -d "$STORAGE_DIR" || return 0
  47. find "$STORAGE_DIR" -name "*.clip" 2>/dev/null \
  48. | while read name;
  49. do
  50. ft=$(file -b -i "$name" | cut -d\; -f1)
  51. case $ft in
  52. text/*)
  53. hint=$(head -c 80 "$name" | tr '\n' '↵')
  54. ;;
  55. *)
  56. hint=$(head -c 16 "$name" | hexdump -C | head -1)
  57. ;;
  58. esac
  59. echos "$(basename "$name") || $ft || $hint"
  60. done
  61. }
  62. lspaths() {
  63. #
  64. # List only clip paths
  65. #
  66. ls "$STORAGE_DIR/"*.clip
  67. }
  68. load_clip() {
  69. #
  70. # Load single clip of choice
  71. #
  72. local clipname=$1
  73. local name=$(pick_clip)
  74. test -n "$name" || return 1
  75. < "$STORAGE_DIR/$name" xclip -i -selection "$clipname"
  76. }
  77. rm_clip() {
  78. #
  79. # Remove single clip of choice
  80. #
  81. local name=$(pick_clip)
  82. test -n "$name" || return 1
  83. rm -f "$STORAGE_DIR/$name"
  84. }
  85. rm_all() {
  86. #
  87. # Drop all clips
  88. #
  89. test -n "$STORAGE_DIR" || die "storage directory is unset, aborting"
  90. test -d "$STORAGE_DIR" || return 0
  91. find "$STORAGE_DIR" -name "*.clip" -print0 | xargs -0 rm -f
  92. rmdir "$STORAGE_DIR" 2>/dev/null | :
  93. }
  94. pick_clip() {
  95. saturnin clip ls \
  96. | saturnin dmenu \
  97. | cut -d\ -f 1
  98. }
  99. clipname=primary
  100. action=list_nice
  101. while true; do case "$1" in
  102. save) action=save; shift ;;
  103. load) action=load; shift ;;
  104. ls) action=list; shift ;;
  105. lsh) action=list_nice shift ;;
  106. rm) action=remove; shift ;;
  107. clean) action=clean; shift ;;
  108. -1) clipname=primary; shift ;;
  109. -2) clipname=secondary; shift ;;
  110. -c) clipname=clipboard; shift ;;
  111. "") break ;;
  112. *) usage ;;
  113. esac done
  114. debug "\$*='$*'"
  115. debug -v clipname action
  116. case $action in
  117. save) save_clip $clipname ;;
  118. load) load_clip $clipname ;;
  119. remove) rm_clip ;;
  120. clean) rm_all ;;
  121. list) lspaths ;;
  122. list_nice) lsclips ;;
  123. esac