Refactor bin/{duckduckgo,urlencode}
This commit is contained in:
parent
28ed7298a7
commit
d16f0d14af
|
@ -1,16 +1,5 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
urlencode() {
|
query=$(urlencode <<< "$*")
|
||||||
local LANG=C
|
|
||||||
for ((i=0;i<${#1};i++)); do
|
|
||||||
if [[ ${1:$i:1} =~ ^[a-zA-Z0-9\.\~\_\-]$ ]]; then
|
|
||||||
printf "${1:$i:1}"
|
|
||||||
else
|
|
||||||
printf '%%%02X' "'${1:$i:1}"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
query=$(urlencode "$*")
|
|
||||||
echo "Opening https://duckduckgo.com/?q=$query"
|
echo "Opening https://duckduckgo.com/?q=$query"
|
||||||
open "https://duckduckgo.com/?q=$query"
|
open "https://duckduckgo.com/?q=$query"
|
||||||
|
|
|
@ -1,7 +1,12 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import os, sys, urllib
|
import sys
|
||||||
if os.isatty(file.fileno(sys.stdin)):
|
try:
|
||||||
text = sys.argv[1]
|
from urllib.parse import quote_plus
|
||||||
|
except ImportError:
|
||||||
|
from urllib import quote_plus
|
||||||
|
|
||||||
|
if sys.argv[1:]:
|
||||||
|
text = " ".join(sys.argv[1:])
|
||||||
else:
|
else:
|
||||||
text = "".join(sys.stdin.readlines())
|
text = "".join(sys.stdin.readlines())
|
||||||
print(urllib.quote_plus(text.strip()))
|
print(quote_plus(text.strip()))
|
||||||
|
|
Loading…
Reference in a new issue