fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerMark private functions that don't need localizationa53db7256

master
2.4 KiB65 linesraw
1# localization: skip(private)
2function __fish_print_apt_packages
3    argparse --name=__fish_print_packages i/installed -- $argv
4    or return
5
6    switch (commandline -ct)
7        case '-**'
8            return
9    end
10
11    set -l search_term (commandline -ct | string replace -ar '[\'"\\\\]' '' | string lower)
12
13    if ! test -f /var/lib/dpkg/status
14        return 1
15    end
16
17    if not set -q _flag_installed
18        # Do not generate the cache as apparently sometimes this is slow.
19        # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=547550
20        # (It is safe to use `sed -r` here as we are guaranteed to be on a GNU platform
21        # if apt-cache was found.)
22        # Uses the UTF-8/ASCII record separator (0x1A) character.
23        #
24        # Note: This can include "Description:" fields which we need to include,
25        # "Description-en_GB" (or another locale code) fields which we need to include
26        # as well as "Description-md5" fields which we absolutely do *not* want to include
27        # The regex doesn't allow numbers, so unless someone makes a hash algorithm without a number
28        # in the name, we're safe. (yes, this should absolutely have a better format).
29        #
30        # aptitude has options that control the output formatting, but is orders of magnitude slower
31        #
32        # sed could probably do all of the heavy lifting here, but would be even less readable
33        #
34        # The `head -n 500` causes us to stop once we have 500 lines. We do it after the `sed` because
35        # Debian package descriptions can be extremely long and are hard-wrapped: texlive-latex-extra
36        # has about 2700 lines on Debian 11.
37        apt-cache --no-generate show '.*'(commandline -ct)'.*' 2>/dev/null | sed -r '/^(Package|Description-?[a-zA-Z_]*):/!d;s/Package: (.*)/\1\t/g;s/Description-?[^:]*: (.*)/\1\x1a/g' | head -n 2500 | string join "" | string replace --all --regex \x1a+ \n | uniq
38        return 0
39    else
40        # Do not not use `apt-cache` as it is sometimes inexplicably slow (by multiple orders of magnitude).
41        awk '
42BEGIN {
43  FS=": "
44}
45
46/^Package/ {
47  pkg=$2
48}
49
50/^Status/ {
51  installed=0
52  if ($2 ~ /(^|\s)installed/) {
53    installed=1
54  }
55}
56
57/^Description(-[a-zA-Z]+)?:/ {
58  desc=$2
59  if (installed == 1 && index(pkg, "'$search_term'") > 0) {
60    print pkg "\t" desc
61    installed=0 # Prevent multiple description translations from being printed
62  }
63}' </var/lib/dpkg/status
64    end
65end