saturnin-clip 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 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. lspaths() {
  64. #
  65. # List only clip paths
  66. #
  67. ls "$STORAGE_DIR/"*.clip
  68. }
  69. load_clip() {
  70. #
  71. # Load single clip of choice
  72. #
  73. local clipname=$1
  74. local name=$(pick_clip)
  75. test -n "$name" || return 1
  76. cat $STORAGE_DIR/$name | xclip -i -selection $clipname
  77. }
  78. rm_clip() {
  79. #
  80. # Remove single clip of choice
  81. #
  82. local clipname=$1
  83. local name=$(pick_clip)
  84. test -n "$name" || return 1
  85. rm -f $STORAGE_DIR/$name
  86. }
  87. rm_all() {
  88. #
  89. # Dump all clips
  90. #
  91. test -n "$STORAGE_DIR" || die "storage directory is unset, aborting"
  92. test -d "$STORAGE_DIR" || return 0
  93. find "$STORAGE_DIR" -name "*.clip" | xargs rm -f
  94. rmdir "$STORAGE_DIR" 2>/dev/null | :
  95. }
  96. pick_clip() {
  97. saturnin clip ls \
  98. | saturnin dmenu \
  99. | cut -d\ -f 1
  100. }
  101. clipname=primary
  102. action=list_nice
  103. while true; do case "$1" in
  104. save) action=save; shift ;;
  105. load) action=load; shift ;;
  106. ls) action=list; shift ;;
  107. lsh) action=list_nice shift ;;
  108. rm) action=remove; shift ;;
  109. clean) action=clean; shift ;;
  110. -1) clipname=primary; shift ;;
  111. -2) clipname=secondary; shift ;;
  112. -c) clipname=clipboard; shift ;;
  113. "") break ;;
  114. *) usage ;;
  115. esac done
  116. debug "\$@='$@'"
  117. debug -v clipname action
  118. case $action in
  119. save) save_clip $clipname ;;
  120. load) load_clip $clipname ;;
  121. remove) rm_clip ;;
  122. clean) rm_all ;;
  123. list) lspaths ;;
  124. list_nice) lsclips ;;
  125. esac