From 8c09d6e0634a5424577ab924943886510f7702ba Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Mon, 2 Jul 2018 11:40:01 -0500 Subject: [PATCH] Limit `apt remove ...` completions to installed packages only Added a new flag `--installed` via `argparse` to `__fish_print_packages` which indicates that only installed packages should be listed. TODO: Other non-debian/apt platforms should take advantage of this flag/ behavior as well. --- CHANGELOG.md | 1 + share/completions/apt.fish | 9 +++++++-- share/functions/__fish_print_packages.fish | 22 +++++++++++++++++----- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b046698b6..08d3ead98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,7 @@ This section is for changes merged to the `major` branch that are not also merge - `unzip` - `xsv` - Improved completions for + - `apt` - `brew` - `diskutil` - `git` (#4395, #4396, #4592) diff --git a/share/completions/apt.fish b/share/completions/apt.fish index 239798430..14cde0cdc 100644 --- a/share/completions/apt.fish +++ b/share/completions/apt.fish @@ -1,7 +1,8 @@ # Completions for the `apt` command set -l all_subcmds update upgrade full-upgrade search list install show remove edit-sources purge changelog autoremove depends rdepends -set -l pkg_subcmds install remove upgrade full-upgrade show search purge changelog policy depends rdepends +set -l pkg_subcmds install upgrade full-upgrade show search purge changelog policy depends rdepends +set -l installed_pkg_subcmds remove function __fish_apt_subcommand set subcommand $argv[1] @@ -15,7 +16,11 @@ function __fish_apt_option complete -f -c apt -n "__fish_seen_subcommand_from $subcommand" $argv end -complete -c apt -n "__fish_seen_subcommand_from $pkg_subcmds" -a '(__fish_print_packages | head -n 100)' -d 'Package' +#using -r and not -e as string match -e is broken, this will cause problems +#if the commandline contains special characters, but most package names do +#not contain special characters. Can switch to -e after #4971 is fixed. +complete -c apt -n "__fish_seen_subcommand_from $pkg_subcmds" -a '(__fish_print_packages | string match -r -- (commandline -ct) | head -n 100)' -d 'Package' +complete -c apt -n "__fish_seen_subcommand_from $installed_pkg_subcmds" -a '(__fish_print_packages --installed | string match -r -- ".*"(commandline -ct)".*" | head -n 100)' -d 'Package' # Support flags complete -x -f -c apt -s h -l help -d 'Display help' diff --git a/share/functions/__fish_print_packages.fish b/share/functions/__fish_print_packages.fish index 830baabca..43ef0d9c7 100644 --- a/share/functions/__fish_print_packages.fish +++ b/share/functions/__fish_print_packages.fish @@ -1,5 +1,12 @@ - +# Use --installed to limit to installed packages only function __fish_print_packages + argparse --name=__fish_print_packages 'i/installed' -- $argv + or return; + + set -l only_installed 1 + if not set -q _flag_installed + set -e only_installed + end # apt-cache is much, much faster than rpm, and can do this in real # time. We use it if available. @@ -13,10 +20,15 @@ function __fish_print_packages set -l package (_ "Package") if type -q -f apt-cache - # Do not generate the cache as apparently sometimes this is slow. - # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=547550 - apt-cache --no-generate pkgnames (commandline -ct) 2>/dev/null | sed -e 's/$/'\t$package'/' - return + if set -q only_installed + dpkg --get-selections | string replace -r '(\S+).*' "\$1\t$package" + return + else + # Do not generate the cache as apparently sometimes this is slow. + # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=547550 + apt-cache --no-generate pkgnames (commandline -ct) 2>/dev/null | sed -e 's/$/'\t$package'/' + return + end end # Pkg is fast on FreeBSD and provides versioning info which we want for