saturnin-clip 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash
  2. . $(ffoom path)
  3. ffoo import config
  4. ffoo import pretty
  5. STORAGE_DIR="$HOME/.local/share/saturnin/clips"
  6. usage() {
  7. usage_is "save [-1|-2|-c]"
  8. usage_is "load [-1|-2|-c] name"
  9. }
  10. clipln() {
  11. #
  12. # Print desired clipboard(s) and \n
  13. #
  14. case $1 in
  15. primary|secondary|clipboard)
  16. xclip -o -selection $1 2>/dev/null
  17. ;;
  18. ALL)
  19. xclip -o -selection primary 2>/dev/null
  20. xclip -o -selection secondary 2>/dev/null
  21. xclip -o -selection clipboard 2>/dev/null
  22. ;;
  23. esac
  24. echo ""
  25. }
  26. save_clip() {
  27. local clipname=$1
  28. mkdir -p "$STORAGE_DIR" || die "could not create directory for saving"
  29. local path="$STORAGE_DIR/$(date +%Y%m%d-%H%M%S.clip)"
  30. clipln $clipname > "$path"
  31. }
  32. lsclips() {
  33. local clipname=$1
  34. local ft hint name
  35. test -d "$STORAGE_DIR" || return 0
  36. ls "$STORAGE_DIR/"*.clip 2>/dev/null \
  37. | while read name;
  38. do
  39. ft=$(file -b -i $name | cut -d\; -f1)
  40. case $ft in
  41. text/*)
  42. hint=$(head -c 80 $name | tr '\n' '↵')
  43. ;;
  44. *)
  45. hint=$(head -c 16 $name | hexdump -C)
  46. ;;
  47. esac
  48. echos "$(basename $name) || $ft || $hint"
  49. done
  50. }
  51. load_clip() {
  52. local clipname=$1
  53. local fname=$(saturnin clip ls | saturnin dmenu | cut -d\ -f 1)
  54. cat $STORAGE_DIR/$name | xclip -i -selection $clipname
  55. }
  56. clipname=primary
  57. action=ls
  58. while true; do case "$1" in
  59. save) action=save; shift ;;
  60. load) action=load; shift ;;
  61. ls) action=list; shift ;;
  62. -1) clipname=primary; shift ;;
  63. -2) clipname=secondary; shift ;;
  64. -c) clipname=clipboard; shift ;;
  65. "") break; ;;
  66. *) usage; ;;
  67. esac done
  68. debug "\$@='$@'"
  69. debug -v clipname action
  70. case $action in
  71. save) save_clip $clipname ;;
  72. load) load_clip $clipname ;;
  73. list) lsclips ;;
  74. esac