summaryrefslogtreecommitdiff
path: root/plugins/urltools
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/urltools')
-rw-r--r--plugins/urltools/README.md29
-rw-r--r--plugins/urltools/urltools.plugin.zsh11
2 files changed, 36 insertions, 4 deletions
diff --git a/plugins/urltools/README.md b/plugins/urltools/README.md
new file mode 100644
index 000000000..548301c72
--- /dev/null
+++ b/plugins/urltools/README.md
@@ -0,0 +1,29 @@
+# URLTools plugin
+
+This plugin provides two aliases to URL-encode and URL-decode strings.
+
+To start using it, add the `urltools` plugin to your plugins array in `~/.zshrc`:
+
+```zsh
+plugins=(... urltools)
+```
+
+Original author: [Ian Chesal](https://github.com/ianchesal)
+Original idea and aliases: [Ruslan Spivak](https://ruslanspivak.wordpress.com/2010/06/02/urlencode-and-urldecode-from-a-command-line/)
+
+## Commands
+
+| Command | Description |
+| :---------- | :--------------------------- |
+| `urlencode` | URL-encodes the given string |
+| `urldecode` | URL-decodes the given string |
+
+## Examples
+
+```zsh
+urlencode 'https://github.com/robbyrussell/oh-my-zsh/search?q=urltools&type=Code'
+# returns https%3A%2F%2Fgithub.com%2Frobbyrussell%2Foh-my-zsh%2Fsearch%3Fq%3Durltools%26type%3DCode
+
+urldecode 'https%3A%2F%2Fgithub.com%2Frobbyrussell%2Foh-my-zsh%2Fsearch%3Fq%3Durltools%26type%3DCode'
+# returns https://github.com/robbyrussell/oh-my-zsh/search?q=urltools&type=Code
+```
diff --git a/plugins/urltools/urltools.plugin.zsh b/plugins/urltools/urltools.plugin.zsh
index 22327334d..47d9a34e4 100644
--- a/plugins/urltools/urltools.plugin.zsh
+++ b/plugins/urltools/urltools.plugin.zsh
@@ -2,7 +2,7 @@
# Adds handy command line aliases useful for dealing with URLs
#
# Taken from:
-# http://ruslanspivak.com/2010/06/02/urlencode-and-urldecode-from-a-command-line/
+# https://ruslanspivak.com/2010/06/02/urlencode-and-urldecode-from-a-command-line/
if [[ $(whence $URLTOOLS_METHOD) = "" ]]; then
URLTOOLS_METHOD=""
@@ -11,9 +11,12 @@ fi
if [[ $(whence node) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xnode" ) ]]; then
alias urlencode='node -e "console.log(encodeURIComponent(process.argv[1]))"'
alias urldecode='node -e "console.log(decodeURIComponent(process.argv[1]))"'
-elif [[ $(whence python) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xpython" ) ]]; then
- alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'
- alias urldecode='python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"'
+elif [[ $(whence python3) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xpython" ) ]]; then
+ alias urlencode='python3 -c "import sys, urllib.parse as up; print(up.quote_plus(sys.argv[1]))"'
+ alias urldecode='python3 -c "import sys, urllib.parse as up; print(up.unquote_plus(sys.argv[1]))"'
+elif [[ $(whence python2) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xpython" ) ]]; then
+ alias urlencode='python2 -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'
+ alias urldecode='python2 -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"'
elif [[ $(whence xxd) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xshell" ) ]]; then
function urlencode() {echo $@ | tr -d "\n" | xxd -plain | sed "s/\(..\)/%\1/g"}
function urldecode() {printf $(echo -n $@ | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g')"\n"}