fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
a53db7256
master
1# localization: skip(private) 2function __fish_print_apt_packages 3argparse --name=__fish_print_packages i/installed --$argv 4or return 5 6switch ( commandline -ct) 7case '-**' 8return 9end 10 11set -l search_term( commandline -ct| string replace -ar'[\'"\\\\]' '' | string lower) 12 13if ! test -f /var/lib/dpkg/status 14return 1 15end 16 17if 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. 37apt-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 -n2500 | string join"" | string replace --all --regex\x1a +\n | uniq 38return 0 39else 40# Do not not use `apt-cache` as it is sometimes inexplicably slow (by multiple orders of magnitude). 41awk ' 42BEGIN { 43FS=": " 44} 45 46/^Package/ { 47pkg=$2 48} 49 50/^Status/ { 51installed=0 52if ($2 ~ /(^|\s)installed/) { 53installed=1 54} 55} 56 57/^Description(-[a-zA-Z]+)?:/ { 58desc=$2 59if (installed == 1 && index(pkg, "' $search_term '") > 0) { 60print pkg "\t" desc 61installed=0 # Prevent multiple description translations from being printed 62} 63}' < /var/lib/dpkg/status 64end 65end