about summary refs log tree commit diff
path: root/Completion/Debian/_deb_packages
blob: be36c817c617e938c0c90928c9e0a9828cdd065a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#autoload

# Usage: _deb_packages expl... avail|installed|uninstalled

_deb_packages_update_avail () {
  if ( [[ ${+_deb_packages_cache_avail} -eq 0 ]] ||
      _cache_invalid DEBS_avail ) && ! _retrieve_cache DEBS_avail;
  then
    _deb_packages_cache_avail=(
      ${(f)"$(apt-cache dumpavail | awk '/^Package:/ { print $2 }')"}
    )

    _store_cache DEBS_avail _deb_packages_cache_avail
  fi
  cachevar=_deb_packages_cache_avail
}

_deb_packages_update_installed () {
  if ( [[ ${+_deb_packages_cache_installed} -eq 0 ]] ||
      _cache_invalid DEBS_installed ) && ! _retrieve_cache DEBS_installed;
  then
    _deb_packages_cache_installed=(
      ${${${(f)"$(dpkg --get-selections)"}:#*deinstall}%%	*}
    )
    _store_cache DEBS_installed _deb_packages_cache_installed
  fi
  cachevar=_deb_packages_cache_installed
}

_deb_packages_update_uninstalled () {
  _deb_packages_update_avail
  _deb_packages_update_installed
  if (( ! $+_deb_packages_cache_uninstalled )); then
    _deb_packages_cache_uninstalled=(
      ${_deb_packages_cache_avail:#${(j:|:)~${_deb_packages_cache_installed:q}}}
    )
  fi
  cachevar=_deb_packages_cache_uninstalled
}

_deb_packages () {
  local command="$argv[$#]" expl cachevar pkgset update_policy

  zstyle -s ":completion:*:*:$service:*" cache-policy update_policy
  if [[ -z "$update_policy" ]]; then
    zstyle ":completion:*:*:$service:*" cache-policy _debs_caching_policy
  fi

  [[ "$command" = (installed|uninstalled|avail) ]] || {
    _message "_deb_packages:unknown command: $command"
    return
  }

  zstyle -s ":completion:${curcontext}:" packageset pkgset

  [[ "$pkgset" = (installed|uninstalled|avail|available) ]] || {
    pkgset="$command"
  }

  [[ "$pkgset" = "available" ]] && pkgset="avail"

  expl=("${(@)argv[1,-2]}")

  _deb_packages_update_$pkgset

  _tags packages && compadd "$expl[@]" - "${(@P)cachevar}"
}

 _debs_caching_policy () {
    # rebuild if cache is more than a week old
      oldp=( "$1"(mw+1) )
        (( $#oldp )) && return 0

	  [[ /var/cache/apt/pkgcache.bin -nt "$1" ||
	     /var/lib/dpkg/available -nt "$1" ]]
	  }



_deb_packages "$@"