]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/urltools/urltools.plugin.zsh
...
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / urltools / urltools.plugin.zsh
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
7 if [[ $(whence $URLTOOLS_METHOD) = "" ]]; then
8     URLTOOLS_METHOD=""
9 fi
10
11 if [[ $(whence node) != "" && ( "x$URLTOOLS_METHOD" = "x"  || "x$URLTOOLS_METHOD" = "xnode" ) ]]; then
12     alias urlencode='node -e "console.log(encodeURIComponent(process.argv[1]))"'
13     alias urldecode='node -e "console.log(decodeURIComponent(process.argv[1]))"'
14 elif [[ $(whence python3) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xpython" ) ]]; then
15     alias urlencode='python3 -c "import sys; del sys.path[0]; import urllib.parse as up; print(up.quote_plus(sys.argv[1]))"'
16     alias urldecode='python3 -c "import sys; del sys.path[0]; import urllib.parse as up; print(up.unquote_plus(sys.argv[1]))"'
17 elif [[ $(whence python2) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xpython" ) ]]; then
18     alias urlencode='python2 -c "import sys; del sys.path[0]; import urllib as ul; print ul.quote_plus(sys.argv[1])"'
19     alias urldecode='python2 -c "import sys; del sys.path[0]; import urllib as ul; print ul.unquote_plus(sys.argv[1])"'
20 elif [[ $(whence xxd) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xshell" ) ]]; then
21     function urlencode() {echo $@ | tr -d "\n" | xxd -plain | sed "s/\(..\)/%\1/g"}
22     function urldecode() {printf $(echo -n $@ | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g')"\n"}
23 elif [[ $(whence ruby) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xruby" ) ]]; then
24     alias urlencode='ruby -r cgi -e "puts CGI.escape(ARGV[0])"'
25     alias urldecode='ruby -r cgi -e "puts CGI.unescape(ARGV[0])"'
26 elif [[ $(whence php) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xphp" ) ]]; then
27     alias urlencode='php -r "echo rawurlencode(\$argv[1]); echo \"\n\";"'
28     alias urldecode='php -r "echo rawurldecode(\$argv[1]); echo \"\\n\";"'
29 elif [[ $(whence perl) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xperl" ) ]]; then
30     if perl -MURI::Encode -e 1&> /dev/null; then
31         alias urlencode='perl -MURI::Encode -ep "uri_encode($ARGV[0]);"'
32         alias urldecode='perl -MURI::Encode -ep "uri_decode($ARGV[0]);"'
33     elif perl -MURI::Escape -e 1 &> /dev/null; then
34         alias urlencode='perl -MURI::Escape -ep "uri_escape($ARGV[0]);"'
35         alias urldecode='perl -MURI::Escape -ep "uri_unescape($ARGV[0]);"'
36     else
37         alias urlencode="perl -e '\$new=\$ARGV[0]; \$new =~ s/([^A-Za-z0-9])/sprintf(\"%%%02X\", ord(\$1))/seg; print \"\$new\n\";'"
38         alias urldecode="perl -e '\$new=\$ARGV[0]; \$new =~ s/\%([A-Fa-f0-9]{2})/pack(\"C\", hex(\$1))/seg; print \"\$new\n\";'"
39     fi
40 fi
41
42 unset URLTOOLS_METHOD