my xfce4 dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
2.7 KiB

3 years ago
  1. # URL Tools
  2. # Adds handy command line aliases useful for dealing with URLs
  3. #
  4. # Taken from:
  5. # https://ruslanspivak.com/2010/06/02/urlencode-and-urldecode-from-a-command-line/
  6. if [[ $(whence $URLTOOLS_METHOD) = "" ]]; then
  7. URLTOOLS_METHOD=""
  8. fi
  9. if [[ $(whence node) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xnode" ) ]]; then
  10. alias urlencode='node -e "console.log(encodeURIComponent(process.argv[1]))"'
  11. alias urldecode='node -e "console.log(decodeURIComponent(process.argv[1]))"'
  12. elif [[ $(whence python3) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xpython" ) ]]; then
  13. alias urlencode='python3 -c "import sys; del sys.path[0]; import urllib.parse as up; print(up.quote_plus(sys.argv[1]))"'
  14. alias urldecode='python3 -c "import sys; del sys.path[0]; import urllib.parse as up; print(up.unquote_plus(sys.argv[1]))"'
  15. elif [[ $(whence python2) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xpython" ) ]]; then
  16. alias urlencode='python2 -c "import sys; del sys.path[0]; import urllib as ul; print ul.quote_plus(sys.argv[1])"'
  17. alias urldecode='python2 -c "import sys; del sys.path[0]; import urllib as ul; print ul.unquote_plus(sys.argv[1])"'
  18. elif [[ $(whence xxd) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xshell" ) ]]; then
  19. function urlencode() {echo $@ | tr -d "\n" | xxd -plain | sed "s/\(..\)/%\1/g"}
  20. function urldecode() {printf $(echo -n $@ | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g')"\n"}
  21. elif [[ $(whence ruby) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xruby" ) ]]; then
  22. alias urlencode='ruby -r cgi -e "puts CGI.escape(ARGV[0])"'
  23. alias urldecode='ruby -r cgi -e "puts CGI.unescape(ARGV[0])"'
  24. elif [[ $(whence php) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xphp" ) ]]; then
  25. alias urlencode='php -r "echo rawurlencode(\$argv[1]); echo \"\n\";"'
  26. alias urldecode='php -r "echo rawurldecode(\$argv[1]); echo \"\\n\";"'
  27. elif [[ $(whence perl) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xperl" ) ]]; then
  28. if perl -MURI::Encode -e 1&> /dev/null; then
  29. alias urlencode='perl -MURI::Encode -ep "uri_encode($ARGV[0]);"'
  30. alias urldecode='perl -MURI::Encode -ep "uri_decode($ARGV[0]);"'
  31. elif perl -MURI::Escape -e 1 &> /dev/null; then
  32. alias urlencode='perl -MURI::Escape -ep "uri_escape($ARGV[0]);"'
  33. alias urldecode='perl -MURI::Escape -ep "uri_unescape($ARGV[0]);"'
  34. else
  35. alias urlencode="perl -e '\$new=\$ARGV[0]; \$new =~ s/([^A-Za-z0-9])/sprintf(\"%%%02X\", ord(\$1))/seg; print \"\$new\n\";'"
  36. alias urldecode="perl -e '\$new=\$ARGV[0]; \$new =~ s/\%([A-Fa-f0-9]{2})/pack(\"C\", hex(\$1))/seg; print \"\$new\n\";'"
  37. fi
  38. fi
  39. unset URLTOOLS_METHOD