frontend-search.plugin.zsh 4.26 KB
Newer Older
Marc Cornellà's avatar
Marc Cornellà committed
1
alias angular='frontend angular'
Marc Cornellà's avatar
Marc Cornellà committed
2
3
4
alias angularjs='frontend angularjs'
alias bem='frontend bem'
alias bootsnipp='frontend bootsnipp'
5
alias bundlephobia='frontend bundlephobia'
Marc Cornellà's avatar
Marc Cornellà committed
6
7
alias caniuse='frontend caniuse'
alias codepen='frontend codepen'
Will Mendes's avatar
Will Mendes committed
8
alias compassdoc='frontend compassdoc'
Marc Cornellà's avatar
Marc Cornellà committed
9
10
11
alias cssflow='frontend cssflow'
alias dartlang='frontend dartlang'
alias emberjs='frontend emberjs'
12
alias flowtype='frontend flowtype'
Marc Cornellà's avatar
Marc Cornellà committed
13
alias fontello='frontend fontello'
14
alias github='frontend github'
Marc Cornellà's avatar
Marc Cornellà committed
15
alias html5please='frontend html5please'
16
alias jestjs='frontend jestjs'
Marc Cornellà's avatar
Marc Cornellà committed
17
18
19
alias jquery='frontend jquery'
alias lodash='frontend lodash'
alias mdn='frontend mdn'
20
alias nodejs='frontend nodejs'
Marc Cornellà's avatar
Marc Cornellà committed
21
22
23
24
25
alias npmjs='frontend npmjs'
alias qunit='frontend qunit'
alias reactjs='frontend reactjs'
alias smacss='frontend smacss'
alias stackoverflow='frontend stackoverflow'
26
alias typescript='frontend typescript'
Marc Cornellà's avatar
Marc Cornellà committed
27
alias unheap='frontend unheap'
28
alias vuejs='frontend vuejs'
Marc Cornellà's avatar
Marc Cornellà committed
29

30
31
32
33
34
35
36
37
38
39
40
function _frontend_fallback() {
  local url
  if [[ "$FRONTEND_SEARCH_FALLBACK" == duckduckgo ]]; then
    url="https://duckduckgo.com/?sites=$1&q="
  else
    url="https://google.com/search?as_sitesearch=$1&as_q="
  fi

  echo "$url"
}

41
function frontend() {
42
43
  emulate -L zsh

44
  # define search context URLS
45
46
  typeset -A urls
  urls=(
47
    angular        'https://angular.io/?search='
48
49
    angularjs      $(_frontend_fallback 'angularjs.org')
    bem            $(_frontend_fallback 'bem.info')
Janosch Schwalm's avatar
Janosch Schwalm committed
50
    bootsnipp      'https://bootsnipp.com/search?q='
51
    bundlephobia   'https://bundlephobia.com/result?p='
Janosch Schwalm's avatar
Janosch Schwalm committed
52
53
    caniuse        'https://caniuse.com/#search='
    codepen        'https://codepen.io/search?q='
Will Mendes's avatar
Will Mendes committed
54
    compassdoc     'http://compass-style.org/search?q='
55
56
    cssflow        'http://www.cssflow.com/search?q='
    dartlang       'https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:'
57
58
    emberjs        $(_frontend_fallback 'emberjs.com/')
    flowtype       $(_frontend_fallback 'flow.org/en/docs/')
59
    fontello       'http://fontello.com/#search='
60
61
    github         'https://github.com/search?q='
    html5please    'https://html5please.com/#'
62
    jestjs         $(_frontend_fallback 'jestjs.io')
63
64
65
    jquery         'https://api.jquery.com/?s='
    lodash         'https://devdocs.io/lodash/index#'
    mdn            'https://developer.mozilla.org/search?q='
66
    nodejs         $(_frontend_fallback 'nodejs.org/en/docs/')
67
68
    npmjs          'https://www.npmjs.com/search?q='
    qunit          'https://api.qunitjs.com/?s='
69
70
    reactjs        $(_frontend_fallback 'reactjs.org/')
    smacss         $(_frontend_fallback 'smacss.com')
Janosch Schwalm's avatar
Janosch Schwalm committed
71
    stackoverflow  'https://stackoverflow.com/search?q='
72
    typescript     $(_frontend_fallback 'www.typescriptlang.org/docs')
73
    unheap         'http://www.unheap.com/?s='
74
    vuejs          $(_frontend_fallback 'vuejs.org')
75
  )
76

77
78
  # show help for command list
  if [[ $# -lt 2 ]]
79
  then
80
      print -P "Usage: frontend %Ucontext%u %Uterm%u [...%Umore%u] (or just: %Ucontext%u %Uterm%u [...%Umore%u])"
div_bhasin's avatar
div_bhasin committed
81
      print -P ""
82
83
84
      print -P "%Uterm%u and what follows is what will be searched for in the %Ucontext%u website,"
      print -P "and %Ucontext%u is one of the following:"
      print -P ""
85
86
87
      print -P "  angular, angularjs, bem, bootsnipp, caniuse, codepen, compassdoc, cssflow,"
      print -P "  dartlang, emberjs, fontello, flowtype, github, html5please, jestjs, jquery, lodash,"
      print -P "  mdn, npmjs, nodejs, qunit, reactjs, smacss, stackoverflow, unheap, vuejs, bundlephobia"
88
89
90
      print -P ""
      print -P "For example: frontend npmjs mocha (or just: npmjs mocha)."
      print -P ""
91
92
93
      return 1
  fi

94
  # check whether the search context is supported
95
  if [[ -z "$urls[$1]" ]]
96
  then
97
98
99
100
    echo "Search context \"$1\" currently not supported."
    echo ""
    echo "Valid contexts are:"
    echo ""
101
102
103
    echo "  angular, angularjs, bem, bootsnipp, caniuse, codepen, compassdoc, cssflow,"
    echo "  dartlang, emberjs, fontello, github, html5please, jest, jquery, lodash,"
    echo "  mdn, npmjs, nodejs, qunit, reactjs, smacss, stackoverflow, unheap, vuejs, bundlephobia"
104
105
106
107
    echo ""
    return 1
  fi

108
  # build search url:
Marc Cornellà's avatar
Marc Cornellà committed
109
  # join arguments passed with '%20', then append to search context URL
110
  # TODO substitute for proper urlencode method
div_bhasin's avatar
div_bhasin committed
111
  url="${urls[$1]}${(j:%20:)@[2,-1]}"
112

113
  echo "Opening $url ..."
114

115
  open_command "$url"
116
}