saturnin-clip 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. 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. 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=$(saturnin clip ls | saturnin dmenu | cut -d\ -f 1)
  75. cat $STORAGE_DIR/$name | xclip -i -selection $clipname
  76. }
  77. rm_clip() {
  78. #
  79. # Remove single clip of choice
  80. #
  81. local clipname=$1
  82. local name=$(saturnin clip ls | saturnin dmenu | cut -d\ -f 1)
  83. rm -f $STORAGE_DIR/$name
  84. }
  85. rm_all() {
  86. #
  87. # Dump 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" | xargs rm -f
  92. rmdir "$STORAGE_DIR" 2>/dev/null | :
  93. }
  94. clipname=primary
  95. action=list_nice
  96. while true; do case "$1" in
  97. save) action=save; shift ;;
  98. load) action=load; shift ;;
  99. ls) action=list; shift ;;
  100. lsh) action=list_nice shift ;;
  101. rm) action=remove; shift ;;
  102. clean) action=clean; shift ;;
  103. -1) clipname=primary; shift ;;
  104. -2) clipname=secondary; shift ;;
  105. -c) clipname=clipboard; shift ;;
  106. "") break ;;
  107. *) usage ;;
  108. esac done
  109. debug "\$@='$@'"
  110. debug -v clipname action
  111. case $action in
  112. save) save_clip $clipname ;;
  113. load) load_clip $clipname ;;
  114. remove) rm_clip ;;
  115. clean) rm_all ;;
  116. list) lspaths ;;
  117. list_nice) lsclips ;;
  118. esac