Move important information up in -Si output
[pacman-ng.git] / contrib / pacsearch.in
blobb1db8abe6647261b423833fec7873ab96051770f
1 #!/usr/bin/perl
2 # pacsearch - Adds color and install information to a 'pacman -Ss' search
4 # Copyright (C) 2008-2011 Dan McGee <dan@archlinux.org>
6 # Based off original shell script version:
7 # Copyright (C) 2006-2007 Dan McGee <dan@archlinux.org>
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #TODO: colors flag on commandline
24 use strict;
25 use warnings;
27 my $myname = 'pacsearch';
28 my $myver = '@PACKAGE_VERSION@';
30 sub usage {
31 print "$myname - Add color and install information to a pacman -Ss search\n";
32 print "Usage: $myname <pattern>\n";
33 print "Example: $myname ^gnome\n";
36 sub version {
37 printf "%s %s\n", $myname, $myver;
38 print "Copyright (C) 2008-2011 Dan McGee <dan\@archlinux.org>\n\n";
39 print "Based off original shell script version:\n";
40 print "Copyright (C) 2006-2007 Dan McGee <dan\@archlinux.org>\n";
43 if ($#ARGV lt 0 || $ARGV[0] eq "--help" || $ARGV[0] eq "-h") {
44 usage;
45 if ($#ARGV lt 0) {
46 exit 1;
48 exit 0;
51 if ($ARGV[0] eq "--version" || $ARGV[0] eq "-V") {
52 version;
53 exit 0;
56 # define our colors to use when printing
57 my $CLR1 = "\e[0;34m";
58 my $CLR2 = "\e[0;32m";
59 my $CLR3 = "\e[0;35m";
60 my $CLR4 = "\e[0;36m";
61 my $CLR5 = "\e[0;31m";
62 my $CLR6 = "\e[0;33m";
63 my $CLR7 = "\e[1;36m";
64 my $INST = "\e[1;31m";
65 my $BASE = "\e[0m";
67 # color a "repo/pkgname pkgver" line based on the repository name
68 sub to_color {
69 my $line = shift;
70 # get the installed text colored first
71 $line =~ s/(\[.*\]$)/$INST$1$BASE/;
72 # and now the repo and dealings
73 $line =~ s/(^core\/.*)/$CLR1$1$BASE/;
74 $line =~ s/(^extra\/.*)/$CLR2$1$BASE/;
75 $line =~ s/(^community\/.*)/$CLR3$1$BASE/;
76 $line =~ s/(^testing\/.*)/$CLR4$1$BASE/;
77 $line =~ s/(^community-testing\/.*)/$CLR5$1$BASE/;
78 $line =~ s/(^multilib\/.*)/$CLR6$1$BASE/;
79 $line =~ s/(^local\/.*)/$CLR7$1$BASE/;
80 # any other unknown repository
81 $line =~ s/(^[\w-]*\/.*)/$CLR6$1$BASE/;
82 return $line;
85 my %allpkgs = ();
87 my $syncout = `pacman -Ss '@ARGV'`;
88 # split each sync search entry into its own array entry
89 my @syncpkgs = split(/\n^(?=\w)/m, $syncout);
90 # remove the extra \n from the last desc entry
91 if ($#syncpkgs >= 0) {
92 chomp($syncpkgs[$#syncpkgs]);
95 # counter var for packages, used here and in the query loop too
96 my $cnt = 0;
97 foreach $_ (@syncpkgs) {
98 # we grab 4 fields here: repo, name/ver, installed, and desc
99 my @pkgfields = /^(.*?)\/(.*?) ?(\[.*\])?\n(.*)$/s;
100 if(not @pkgfields) {
101 # skip any non-matching line and just print it for the user
102 print $_, "\n";
103 next;
105 # since installed is optional, we should fill it in if necessary
106 $pkgfields[2] = "" if not defined $pkgfields[2];
107 # add a fifth field that indicates original order
108 push (@pkgfields, $cnt++);
109 # add each sync pkg by name/ver to a hash table for quick lookup
110 $allpkgs{$pkgfields[1]} = [ @pkgfields ];
113 my $queryout = `pacman -Qs '@ARGV'`;
114 # split each querysearch entry into its own array entry
115 my @querypkgs = split(/\n^(?=\w)/m, $queryout);
116 # remove the extra \n from the last desc entry
117 if ($#querypkgs >= 0) {
118 chomp ($querypkgs[$#querypkgs]);
121 foreach $_ (@querypkgs) {
122 # we grab 4 fields here: repo, name/ver, installed, and desc
123 my @pkgfields = /^(.*?)\/(.*?) ?(\[.*\])?\n(.*)$/s;
124 # skip any non-matching line
125 next if not defined $pkgfields[1];
126 # since installed is optional, we should fill it in if necessary
127 $pkgfields[2] = "" if not defined $pkgfields[2];
128 # check if the package was listed in the sync out
129 if (not exists $allpkgs{$pkgfields[1]}) {
130 $pkgfields[2] = "[installed]";
131 # add a fifth field that indicates original order (after sync)
132 push (@pkgfields, $cnt++);
133 # add our local-only package to the hash
134 $allpkgs{$pkgfields[1]} = [ @pkgfields ];
138 # sort by original order (the fifth field) and print
139 foreach $_ ( sort{ @{$allpkgs{$a}}[4] <=> @{$allpkgs{$b}}[4] } keys %allpkgs) {
140 my @v = @{$allpkgs{$_}};
141 my $line = "$v[0]/$v[1] $v[2]";
142 $line = to_color($line);
143 # print colorized "repo/pkgname pkgver" string with possible installed text
144 print "$line\n";
145 print "$v[3]\n";
148 #vim: set noet: