25313814775c08c64dc541fbadceb38c669c541a

Author
Hong Xu <hong@topbug.net>
Committer
Hong Xu <hong@topbug.net>
Date

Message

Add web-search plugin.

This plugin adds google, bing and yahoo commands to launch the default
web browser to do web search:

e.g.

    google oh-my-zsh
    bing what is zsh

Diff

 1diff --git a/plugins/web-search/web-search.plugin.zsh b/plugins/web-search/web-search.plugin.zsh
 2new file mode 100644
 3index 0000000000000000000000000000000000000000..6b6de2b1585d758cace3c0f808972945058114c4
 4--- /dev/null
 5+++ b/plugins/web-search/web-search.plugin.zsh
 6@@ -0,0 +1,43 @@
 7+# web_search from terminal
 8+
 9+function web_search() {
10+
11+  # get the open command
12+  local open_cmd
13+  if [[ $(uname -s) == 'Darwin' ]]; then
14+    open_cmd='open'
15+  else
16+    open_cmd='xdg-open'
17+  fi
18+
19+  # check whether the search engine is supported
20+  if [[ ! $1 =~ '(google|bing|yahoo)' ]];
21+  then
22+    echo "Search engine $1 not supported."
23+    return 1
24+  fi
25+
26+  local url="http://www.$1.com"
27+
28+  # no keyword provided, simply open the search engine homepage
29+  if [[ $# -le 1 ]]; then
30+    $open_cmd "$url"
31+    return
32+  fi
33+
34+  url="${url}/search?q="
35+  shift   # shift out $1
36+
37+  while [[ $# -gt 0 ]]; do
38+    url="${url}$1+"
39+    shift
40+  done
41+
42+  url="${url%?}" # remove the last '+'
43+
44+  $open_cmd "$url"
45+}
46+
47+alias bing='web_search bing'
48+alias google='web_search google'
49+alias yahoo='web_search yahoo'