Parent directory

gitignore.plugin.zsh

1337 bytes
 1# gitignore plugin for oh-my-zsh
 2# Uses gitignore.io CDN endpoint
 3function _gi_curl() {
 4  curl -sfL "https://www.gitignore.io/api/$1"
 5}
 6
 7function gi() {
 8  local query="${(j:,:)@}"
 9  _gi_curl "$query" || return 1
10}
11
12_gitignoreio_get_command_list() {
13  setopt local_options pipe_fail
14  _gi_curl "list" | tr "," "\n"
15}
16
17__gitignoreio_caching_policy() {
18  local -a oldp
19  oldp=("$1"(Nm+7))
20  (($#oldp))
21}
22
23_gitignoreio_retrieve_stale_cache() {
24  zstyle -t ":completion:${curcontext}:" use-cache || return 1
25
26  local cache_dir
27  zstyle -s ":completion:${curcontext}:" cache-path cache_dir
28  : ${cache_dir:=${ZDOTDIR:-$HOME}/.zcompcache}
29
30  [[ -e "$cache_dir/gi-list" ]] || return 1
31  . "$cache_dir/gi-list"
32}
33
34_gitignoreio() {
35  compset -P '*,'
36
37  local cache_policy
38  zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
39  if [[ -z "$cache_policy" ]]; then
40    zstyle ":completion:${curcontext}:" cache-policy __gitignoreio_caching_policy
41  fi
42
43  local -a _gi_list
44  if _cache_invalid gi-list || ! _retrieve_cache gi-list; then
45    local command_list
46    if command_list="$(_gitignoreio_get_command_list)" && [[ -n "$command_list" ]]; then
47      _gi_list=(${(f)command_list})
48      _store_cache gi-list _gi_list
49    else
50      _gitignoreio_retrieve_stale_cache
51    fi
52  fi
53
54  compadd -S '' -a _gi_list
55}
56
57compdef _gitignoreio gi