ohmyzsh/plugins/web-search/web-search.plugin.zsh
2013-07-01 15:36:38 +08:00

64 lines
1.5 KiB
Bash

# web_search from terminal
# You can use the default browser to open the search, you can also choose to open
# the search browser, if you add in the parameter '-f' or '--firefox' will use firefox;
# Add the '-c' or '- chrome' will use of google-chrome
function web_search() {
# get the open command
local open_cmd firefox chrome
for i in "$@"
do
if [[ $i =~ '(-f|--firefox)' ]];
then
firefox='true'
elif [[ $i =~ '(-c|--chrome)' ]];
then
chrome='true'
fi
done
if [[ $(uname -s) == 'Darwin' ]]; then
open_cmd='open'
[[ -n $firefox ]] && open_cmd='open -a "/Applications/Firefox.app"'
[[ -n $chrome ]] && open_cmd='open -a "/Applications/Google Chrome.app"'
else
open_cmd='xdg-open'
[[ -n $firefox ]] && open_cmd='firefox'
[[ -n $chrome ]] && open_cmd='google-chrome'
fi
# check whether the search engine is supported
if [[ ! $1 =~ '(google|bing|yahoo)' ]];
then
echo "Search engine $1 not supported."
return 1
fi
local url="http://www.$1.com"
# no keyword provided, simply open the search engine homepage
if [[ $# -le 1 ]]; then
eval $open_cmd "'$url'"
return
fi
url="${url}/search?q="
shift # shift out $1
while [[ $# -gt 0 ]]; do
if [[ ! $1 =~ '(-f|-c|--chrome|--firefox)' ]];
then
url="${url}$1+"
fi
shift
done
url="${url%?}" # remove the last '+'
eval "$open_cmd" "'$url'"
}
alias bing='web_search bing'
alias google='web_search google'
alias yahoo='web_search yahoo'