sprunge.plugin.zsh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. sprunge() {
  2. if [[ "$1" = --help ]]; then
  3. fmt -s >&2 << EOF
  4. DESCRIPTION
  5. Upload data and fetch URL from the pastebin http://sprunge.us
  6. USAGE
  7. $0 filename.txt
  8. $0 text string
  9. $0 < filename.txt
  10. piped_data | $0
  11. NOTES
  12. Input Methods:
  13. $0 can accept piped data, STDIN redirection [< filename.txt], text strings following the command as arguments, or filenames as arguments. Only one of these methods can be used at a time, so please see the note on precedence. Also, note that using a pipe or STDIN redirection will treat tabs as spaces, or disregard them entirely (if they appear at the beginning of a line). So I suggest using a filename as an argument if tabs are important either to the function or readability of the code.
  14. Precedence:
  15. STDIN redirection has precedence, then piped input, then a filename as an argument, and finally text strings as arguments. For example:
  16. echo piped | $0 arguments.txt < stdin_redirection.txt
  17. In this example, the contents of file_as_stdin_redirection.txt would be uploaded. Both the piped_text and the file_as_argument.txt are ignored. If there is piped input and arguments, the arguments will be ignored, and the piped input uploaded.
  18. Filenames:
  19. If a filename is misspelled or doesn't have the necessary path description, it will NOT generate an error, but will instead treat it as a text string and upload it.
  20. EOF
  21. return
  22. fi
  23. if [ -t 0 ]; then
  24. echo Running interactively, checking for arguments... >&2
  25. if [ "$*" ]; then
  26. echo Arguments present... >&2
  27. if [ -f "$*" ]; then
  28. echo Uploading the contents of "$*"... >&2
  29. cat "$*"
  30. else
  31. echo Uploading the text: \""$*"\"... >&2
  32. echo "$*"
  33. fi | curl -F 'sprunge=<-' http://sprunge.us
  34. else
  35. echo No arguments found, printing USAGE and exiting. >&2
  36. sprunge --help
  37. return 1
  38. fi
  39. else
  40. echo Using input from a pipe or STDIN redirection... >&2
  41. curl -F 'sprunge=<-' http://sprunge.us
  42. fi
  43. }