simple-paste is a cli swiss army-knife for pasting, written in bash, powered by pb https://github.com/ptpb/pb

simple-paste 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/bin/bash
  2. # simple-paste
  3. # This file is part of simple paste -- cli based paste client
  4. #
  5. # Copyright 2017 Markus Meyer <coruja@siduction.org>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. # MA 02110-1301, USA.
  21. SP_URL="https://ptpb.pw/"
  22. SP_UP_OPTIONS="-F sunset=1209600"
  23. [ -f /etc/default/simple-paste.conf ] && source /etc/default/simple-paste.conf
  24. [ -f $HOME/.config/simple-paste.conf ] && source $HOME/.config/simple-paste.conf
  25. SP_COMMAND="upload"
  26. help () {
  27. echo "Usage: COMMAND | simple-paste [-e TIME]"
  28. echo " simple-paste -a|-s|-w [-e TIME]"
  29. echo " simple-paste [-e TIME] FILE"
  30. echo " simple-paste URL"
  31. echo " simple-paste -d UUID"
  32. echo "Options: -a paste screenshot of selected rectangle area (delay: 3s)"
  33. echo " -d UUID delete paste by its uuid"
  34. echo " -e TIME make paste be auto-deleted after TIME (default: 14 days)"
  35. echo " TIME must be Ns (seconds), Nm (minutes), Nh (hours),"
  36. echo " N or Nd (days), or Ny (years), e.g. 3h for 3 hours"
  37. echo " -s paste screenshot of whole screen (delay: 3s)"
  38. echo " -w paste screenshot of focused window (delay: 3s)"
  39. exit 0
  40. }
  41. seconds () {
  42. case ${1: -1} in
  43. s) s=${1:0:$(( ${#1} - 1 ))}
  44. ;;
  45. m) s=$(( ${1:0:$(( ${#1} - 1 ))} * 60 ))
  46. ;;
  47. h) s=$(( ${1:0:$(( ${#1} - 1 ))} * 3600 ))
  48. ;;
  49. d) s=$(( ${1:0:$(( ${#1} - 1 ))} * 86400 ))
  50. ;;
  51. y) s=$(( ${1:0:$(( ${#1} - 1 ))} * 31536000 ))
  52. ;;
  53. [0-9]) s=$(( $1 * 86400 ))
  54. ;;
  55. esac
  56. }
  57. check () {
  58. if [ $# -ne 1 ]
  59. then
  60. help
  61. else
  62. if [ -f $1 ]
  63. then
  64. SP_COMMAND="$SP_COMMAND $1"
  65. elif [[ $1 =~ ^http[s]?://.*$ ]]
  66. then
  67. SP_COMMAND="shorten_url $1"
  68. else
  69. help
  70. fi
  71. fi
  72. }
  73. upload () {
  74. curl $SP_UP_OPTIONS -F "c=@${1:--}" $SP_URL
  75. }
  76. delete () {
  77. curl -X DELETE $SP_URL$1
  78. }
  79. screenshot () {
  80. scrot -z -d 3 "$@" /tmp/$$.jpg
  81. upload /tmp/$$.jpg
  82. }
  83. shorten_url () {
  84. curl -F c=@- https://ptpb.pw/u <<< $1
  85. }
  86. if [ $# -eq 0 ] && [ ! -p /dev/fd/0 ]
  87. then
  88. help
  89. fi
  90. while getopts "ad:e:sw" o
  91. do
  92. case $o in
  93. a) SP_COMMAND="screenshot -s"
  94. ;;
  95. d) SP_COMMAND="delete $OPTARG"
  96. ;;
  97. e) seconds $OPTARG
  98. SP_UP_OPTIONS="-F sunset=$s"
  99. ;;
  100. s) SP_COMMAND="screenshot"
  101. ;;
  102. w) SP_COMMAND="screenshot -u -b"
  103. ;;
  104. *) help
  105. ;;
  106. esac
  107. done
  108. shift $((OPTIND - 1))
  109. if [ $# -ne 0 ]
  110. then
  111. check "$@"
  112. fi
  113. $SP_COMMAND