frontend-search.plugin.zsh 4.39 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
alias npmjs='frontend npmjs'
22
alias packagephobia='frontend packagephobia'
Marc Cornellà's avatar
Marc Cornellà committed
23
24
25
26
alias qunit='frontend qunit'
alias reactjs='frontend reactjs'
alias smacss='frontend smacss'
alias stackoverflow='frontend stackoverflow'
27
alias typescript='frontend typescript'
Marc Cornellà's avatar
Marc Cornellà committed
28
alias unheap='frontend unheap'
29
alias vuejs='frontend vuejs'
Marc Cornellà's avatar
Marc Cornellà committed
30

31
32
33
34
35
36
37
38
39
40
41
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"
}

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

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

79
80
  # show help for command list
  if [[ $# -lt 2 ]]
81
  then
82
      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
83
      print -P ""
84
85
86
      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 ""
87
      print -P "  angular, angularjs, bem, bootsnipp, caniuse, codepen, compassdoc, cssflow, packagephobia"
88
89
      print -P "  dartlang, emberjs, fontello, flowtype, github, html5please, jestjs, jquery, lodash,"
      print -P "  mdn, npmjs, nodejs, qunit, reactjs, smacss, stackoverflow, unheap, vuejs, bundlephobia"
90
91
92
      print -P ""
      print -P "For example: frontend npmjs mocha (or just: npmjs mocha)."
      print -P ""
93
94
95
      return 1
  fi

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

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

115
  echo "Opening $url ..."
116

117
  open_command "$url"
118
}