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.
This commit is contained in:
Mahmoud Al-Qudsi 2018-07-02 11:40:01 -05:00
parent 84b7c2b152
commit 8c09d6e063
3 changed files with 25 additions and 7 deletions

View file

@ -80,6 +80,7 @@ This section is for changes merged to the `major` branch that are not also merge
- `unzip` - `unzip`
- `xsv` - `xsv`
- Improved completions for - Improved completions for
- `apt`
- `brew` - `brew`
- `diskutil` - `diskutil`
- `git` (#4395, #4396, #4592) - `git` (#4395, #4396, #4592)

View file

@ -1,7 +1,8 @@
# Completions for the `apt` command # 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 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 function __fish_apt_subcommand
set subcommand $argv[1] set subcommand $argv[1]
@ -15,7 +16,11 @@ function __fish_apt_option
complete -f -c apt -n "__fish_seen_subcommand_from $subcommand" $argv complete -f -c apt -n "__fish_seen_subcommand_from $subcommand" $argv
end 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 # Support flags
complete -x -f -c apt -s h -l help -d 'Display help' complete -x -f -c apt -s h -l help -d 'Display help'

View file

@ -1,5 +1,12 @@
# Use --installed to limit to installed packages only
function __fish_print_packages 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 # apt-cache is much, much faster than rpm, and can do this in real
# time. We use it if available. # time. We use it if available.
@ -13,11 +20,16 @@ function __fish_print_packages
set -l package (_ "Package") set -l package (_ "Package")
if type -q -f apt-cache if type -q -f apt-cache
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. # Do not generate the cache as apparently sometimes this is slow.
# http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=547550 # 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'/' apt-cache --no-generate pkgnames (commandline -ct) 2>/dev/null | sed -e 's/$/'\t$package'/'
return return
end end
end
# Pkg is fast on FreeBSD and provides versioning info which we want for # Pkg is fast on FreeBSD and provides versioning info which we want for
# installed packages # installed packages