pacsearch: pattern arguments work as for pacman

Previously only one pattern was allowed.

  $ pacsearch foo bar
Search for packages containing 'foo bar'.

  $ pacman -Ss foo bar
Search for packages containing both 'foo' and 'bar'.

Note that removing the quotes from the call was not enough since
  $ pacsearch 'foo|bar'
would then fail.

Note the use of '--' to indicate the end of option parsing. This way we ensure
that input will always be valid and we need not input checks anymore.

Signed-off-by: Pierre Neidhardt <ambrevar@gmail.com>
Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Pierre Neidhardt 2014-02-14 13:18:55 +01:00 committed by Allan McRae
parent cfaff6e0c1
commit cfde337b7b

View File

@ -92,61 +92,46 @@ sub print_pkg {
print "$MAGENTA"; print "$MAGENTA";
} }
print "$v[0]/$RESET$BOLD$v[1] $GREEN$v[2]$BLUE$v[3]$CYAN$v[4]$RESET\n"; print "$v[0]/$RESET$BOLD$v[1] $GREEN$v[2]$BLUE$v[3]$CYAN$v[4]$RESET\n";
print "$v[5]\n"; print "$v[5]";
} }
my %allpkgs = (); my %allpkgs = ();
my @pkglist = (); my @pkglist = ();
my $syncout = `pacman -Ss '@ARGV'`; open (my $syncout, '-|', 'pacman', '-Ss', '--', @ARGV) or exit 1;
# split each sync search entry into its own array entry while ( readline($syncout) ) {
my @syncpkgs = split(/\n^(?=\w)/m, $syncout);
# remove the extra \n from the last desc entry
if ($#syncpkgs >= 0) {
chomp($syncpkgs[$#syncpkgs]);
}
foreach $_ (@syncpkgs) {
# We grab the following fields: repo, name, ver, group, installed, and # We grab the following fields: repo, name, ver, group, installed, and
# desc. We grab leading space for 'group' and 'installed' so that we do not # desc. We grab leading space for 'group' and 'installed' so that we do not
# need to test if non-empty when printing. # need to test if non-empty when printing.
my @pkgfields = /^(.*?)\/(.*?) (.*?)( \(.*?\))?( \[.*\])?\n(.*)$/s; my @pkgfields = /^(.*?)\/(.*?) (.*?)( \(.*?\))?( \[.*\])?$/s;
if(not @pkgfields) { my $desc = readline($syncout);
# skip any non-matching line and just print it for the user
print $_, "\n";
next;
}
# since 'group' and 'installed' are optional, we should fill it in if necessary # since 'group' and 'installed' are optional, we should fill it in if necessary
$pkgfields[3] = "" if not defined $pkgfields[3]; $pkgfields[3] = "" if not defined $pkgfields[3];
$pkgfields[4] = "" if not defined $pkgfields[4]; $pkgfields[4] = "" if not defined $pkgfields[4];
$pkgfields[5] = $desc;
# Add each sync pkg by name/ver to a hash table. # Add each sync pkg by name/ver to a hash table.
# Any value is good since we only check for existence. # Any value is good since we only check for existence.
$allpkgs{$pkgfields[1] . $pkgfields[2]} = 1; $allpkgs{$pkgfields[1] . $pkgfields[2]} = 1;
push (@pkglist, \@pkgfields); push (@pkglist, \@pkgfields);
} }
close ($syncout);
my $queryout = `pacman -Qs '@ARGV'`; open (my $queryout, '-|', 'pacman', '-Qs', '--', @ARGV) or exit 1;
# split each querysearch entry into its own array entry while ( readline($queryout) ) {
my @querypkgs = split(/\n^(?=\w)/m, $queryout);
# remove the extra \n from the last desc entry
if ($#querypkgs >= 0) {
chomp ($querypkgs[$#querypkgs]);
}
foreach $_ (@querypkgs) {
# We grab the same field as before, even the "installed" which is always # We grab the same field as before, even the "installed" which is always
# empty for local searches. This allows us to reserve a cell in @pkgfields. # empty for local searches. This allows us to reserve a cell in @pkgfields.
my @pkgfields = /^(.*?)\/(.*?) (.*?)( \(.*?\))?( \[.*\])?\n(.*)$/s; my @pkgfields = /^(.*?)\/(.*?) (.*?)( \(.*?\))?( \[.*\])?$/s;
# skip any non-matching line my $desc = readline($queryout);
next if not defined $pkgfields[1];
# check if the package was listed in the sync out # check if the package was listed in the sync out
if (not exists $allpkgs{$pkgfields[1] . $pkgfields[2]}) { if (not exists $allpkgs{$pkgfields[1] . $pkgfields[2]}) {
# since 'group' is optional, we should fill it in if necessary # since 'group' is optional, we should fill it in if necessary
$pkgfields[3] = "" if not defined $pkgfields[3]; $pkgfields[3] = "" if not defined $pkgfields[3];
$pkgfields[4] = " [$LC_INSTALLED]"; $pkgfields[4] = " [$LC_INSTALLED]";
$pkgfields[5] = $desc;
push (@pkglist, \@pkgfields); push (@pkglist, \@pkgfields);
} }
} }
close ($queryout);
foreach (@pkglist) { foreach (@pkglist) {
print_pkg (@{$_}); print_pkg (@{$_});