Introduce -Suu
[pacman-ng.git] / contrib / pacsearch
blobc9be7594fffca88f25b0aca7a49236c523083c20
1 #!/usr/bin/perl
2 # pacsearch - Adds color and install information to a 'pacman -Ss' search
4 # Copyright (C) 2008 Dan McGee <dpmcgee@gmail.com>
6 # Based off original shell script version:
7 # Copyright (C) 2006-2007 Dan McGee <dpmcgee@gmail.com>
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 $progname = "pacsearch";
28 my $version = "2.0";
30 if ($#ARGV lt 0 || $ARGV[0] eq "--help" || $ARGV[0] eq "-h") {
31 print "$progname - Add color and install information to a pacman -Ss search\n";
32 print "Usage: $progname <pattern>\n";
33 print "Example: $progname ^gnome\n";
34 if ($#ARGV lt 0) {
35 exit 1;
37 exit 0;
40 if ($ARGV[0] eq "--version" || $ARGV[0] eq "-v") {
41 print "$progname version $version\n";
42 print "Copyright (C) 2006-2008 Dan McGee\n";
43 exit 0;
46 # define our colors to use when printing
47 my $CLR1 = "\e[0;34m";
48 my $CLR2 = "\e[0;32m";
49 my $CLR3 = "\e[0;35m";
50 my $CLR4 = "\e[0;36m";
51 my $CLR5 = "\e[0;31m";
52 my $CLR6 = "\e[0;33m";
53 my $CLR7 = "\e[1;36m";
54 my $INST = "\e[1;31m";
55 my $BASE = "\e[0m";
56 my $INSTMARK = $INST."***";
58 # color a "repo/pkgname pkgver" line based on the respository name
59 sub to_color {
60 my $line = shift;
61 $line =~ s/(^core\/.*)/$CLR1$1$BASE/;
62 $line =~ s/(^extra\/.*)/$CLR2$1$BASE/;
63 $line =~ s/(^community\/.*)/$CLR3$1$BASE/;
64 $line =~ s/(^testing\/.*)/$CLR4$1$BASE/;
65 $line =~ s/(^unstable\/.*)/$CLR5$1$BASE/;
66 $line =~ s/(^custom\/.*)/$CLR6$1$BASE/;
67 $line =~ s/(^local\/.*)/$CLR7$1$BASE/;
68 # any other unknown repository
69 $line =~ s/(^[\w-]*\/.*)/$CLR6$1$BASE/;
70 return $line;
73 my %allpkgs = ();
75 my $syncout = `pacman -Ss '@ARGV'`;
76 # split each sync search entry into its own array entry
77 my @syncpkgs = split(/\n^(?=\w)/m, $syncout);
78 # remove the extra \n from the last desc entry
79 if ($#syncpkgs >= 0) {
80 chomp($syncpkgs[$#syncpkgs]);
83 # counter var for packages, used here and in the query loop too
84 my $cnt = 0;
85 foreach $_ (@syncpkgs) {
86 # we grab 3 fields here: repo, name/ver, and desc
87 my @pkgfields = /^(.*?)\/(.*?)\n(.*)$/s;
88 # add a fourth field that will indicate install status
89 push (@pkgfields, "");
90 # add a fifth field that indicates original order
91 push (@pkgfields, $cnt++);
92 # add each sync pkg by name/ver to a hash table for quick lookup
93 $allpkgs{$pkgfields[1]} = [ @pkgfields ];
96 my $queryout = `pacman -Qs '@ARGV'`;
97 # split each querysearch entry into its own array entry
98 my @querypkgs = split(/\n^(?=\w)/m, $queryout);
99 # remove the extra \n from the last desc entry
100 if ($#querypkgs >= 0) {
101 chomp ($querypkgs[$#querypkgs]);
104 foreach $_ (@querypkgs) {
105 # we grab 3 fields here: repo, name/ver, and desc
106 my @pkgfields = /^(.*?)\/(.*?)\n(.*)$/s;
107 # check if the package was listed in the sync out
108 # if it is we want to mark it with a *** marker
109 if (exists $allpkgs{$pkgfields[1]}) {
110 # mark it in our fourth field as installed
111 @{ $allpkgs{$pkgfields[1]} }[3] = $INSTMARK;
112 } else {
113 # add a fourth field that will indicate install status
114 push (@pkgfields, $INSTMARK);
115 # add a fifth field that indicates original order (after sync)
116 push (@pkgfields, $cnt++);
117 # add our local-only package to the hash
118 $allpkgs{$pkgfields[1]} = [ @pkgfields ];
122 # sort by original order (the fifth field) and print
123 foreach $_ ( sort{ @{$allpkgs{$a}}[4] <=> @{$allpkgs{$b}}[4] } keys %allpkgs) {
124 my @v = @{$allpkgs{$_}};
125 my $line = "$v[0]/$v[1]";
126 $line = to_color($line);
127 # print install marker + colorized "repo/pkgname pkgver" string
128 print "$v[3]$line\n";
129 print "$v[2]\n";
132 #vim: set noet: