[pacman-dev] [PATCHv4 6/6] pacsearch: factored -Ss and -Qs parts into one single function

Pierre Neidhardt ambrevar at gmail.com
Fri Feb 14 07:18:56 EST 2014


Signed-off-by: Pierre Neidhardt <ambrevar at gmail.com>
---
 contrib/pacsearch.in | 55 +++++++++++++++++++---------------------------------
 1 file changed, 20 insertions(+), 35 deletions(-)

diff --git a/contrib/pacsearch.in b/contrib/pacsearch.in
index 18d4641..a89328d 100644
--- a/contrib/pacsearch.in
+++ b/contrib/pacsearch.in
@@ -95,46 +95,31 @@ sub print_pkg {
 	print "$v[5]";
 }
 
-my %allpkgs = ();
-my @pkglist = ();
-
-open (my $syncout, '-|', 'pacman', '-Ss', '--', @ARGV) or exit 1;
-while ( readline($syncout) ) {
-	# 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
-	# need to test if non-empty when printing.
-	my @pkgfields = /^(.*?)\/(.*?) (.*?)( \(.*?\))?( \[.*\])?$/s;
-	my $desc = readline($syncout);
-	# since 'group' and 'installed' are optional, we should fill it in if necessary
-	$pkgfields[3] = "" if not defined $pkgfields[3];
-	$pkgfields[4] = "" if not defined $pkgfields[4];
-	$pkgfields[5] = $desc;
-	# Add each sync pkg by name/ver to a hash table.
-	# Any value is good since we only check for existence.
-	$allpkgs{$pkgfields[1] . $pkgfields[2]} = 1;
-	push (@pkglist, \@pkgfields);
-}
-close ($syncout);
-
-open (my $queryout, '-|', 'pacman', '-Qs', '--', @ARGV) or exit 1;
-while ( readline($queryout) ) {
-	# 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.
-	my @pkgfields = /^(.*?)\/(.*?) (.*?)( \(.*?\))?( \[.*\])?$/s;
-	my $desc = readline($queryout);
-	# check if the package was listed in the sync out
-	if (not exists $allpkgs{$pkgfields[1] . $pkgfields[2]}) {
-		# since 'group' is optional, we should fill it in if necessary
+sub list_pkg {
+	my $db = shift;
+	open (my $out, '-|', 'pacman', $db, '--', @ARGV) or exit 1;
+	my @pkglist = ();
+	while ( readline($out) ) {
+		# 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 need to test if non-empty when printing.
+		my @pkgfields = /^(.*?)\/(.*?) (.*?)( \(.*?\))?( \[.*\])?$/s;
+		my $desc = readline($out);
+		# since 'group' and 'installed' are optional, we should fill it in if
+		# necessary
 		$pkgfields[3] = "" if not defined $pkgfields[3];
-		$pkgfields[4] = " [$LC_INSTALLED]";
+		$pkgfields[4] = "" if not defined $pkgfields[4];
 		$pkgfields[5] = $desc;
 		push (@pkglist, \@pkgfields);
 	}
+	close ($out);
+	return @pkglist;
 }
-close ($queryout);
 
-foreach (@pkglist) {
-	print_pkg (@{$_});
-}
+my @sync = list_pkg('-Ss', @ARGV);
+my %allpkgs = map {$_->[1] . $_->[2] => 1} @sync;
+my @query = grep { not $allpkgs{$_->[1] . $_->[2]}} list_pkg('-Qs', @ARGV);
+$_->[4] = " [$LC_INSTALLED]" foreach @query;
+print_pkg (@{$_}) foreach (@sync, @query);
 
 #vim: set noet:
-- 
1.8.5.4



More information about the pacman-dev mailing list