makepkg: improve srcdir check and add pkgdir
[pacman-ng.git] / contrib / pacsearch
bloba20df265e83acc37d3787f0f842b6f0eaa8d02f1
1 #!/usr/bin/perl
2 # pacsearch - Adds color and install information to a 'pacman -Ss' search
4 # Copyright (C) 2008, 2010 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-2010 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";
57 # color a "repo/pkgname pkgver" line based on the repository name
58 sub to_color {
59 my $line = shift;
60 # get the installed text colored first
61 $line =~ s/(\[.*\]$)/$INST$1$BASE/;
62 # and now the repo and dealings
63 $line =~ s/(^core\/.*)/$CLR1$1$BASE/;
64 $line =~ s/(^extra\/.*)/$CLR2$1$BASE/;
65 $line =~ s/(^community\/.*)/$CLR3$1$BASE/;
66 $line =~ s/(^testing\/.*)/$CLR4$1$BASE/;
67 $line =~ s/(^community-testing\/.*)/$CLR5$1$BASE/;
68 $line =~ s/(^local\/.*)/$CLR7$1$BASE/;
69 # any other unknown repository
70 $line =~ s/(^[\w-]*\/.*)/$CLR6$1$BASE/;
71 return $line;
74 my %allpkgs = ();
76 my $syncout = `pacman -Ss '@ARGV'`;
77 # split each sync search entry into its own array entry
78 my @syncpkgs = split(/\n^(?=\w)/m, $syncout);
79 # remove the extra \n from the last desc entry
80 if ($#syncpkgs >= 0) {
81 chomp($syncpkgs[$#syncpkgs]);
84 # counter var for packages, used here and in the query loop too
85 my $cnt = 0;
86 foreach $_ (@syncpkgs) {
87 # we grab 4 fields here: repo, name/ver, installed, and desc
88 my @pkgfields = /^(.*?)\/(.*?) ?(\[.*\])?\n(.*)$/s;
89 # since installed is optional, we should fill it in if necessary
90 $pkgfields[2] = "" if not defined $pkgfields[2];
91 # add a fifth field that indicates original order
92 push (@pkgfields, $cnt++);
93 # add each sync pkg by name/ver to a hash table for quick lookup
94 $allpkgs{$pkgfields[1]} = [ @pkgfields ];
97 my $queryout = `pacman -Qs '@ARGV'`;
98 # split each querysearch entry into its own array entry
99 my @querypkgs = split(/\n^(?=\w)/m, $queryout);
100 # remove the extra \n from the last desc entry
101 if ($#querypkgs >= 0) {
102 chomp ($querypkgs[$#querypkgs]);
105 foreach $_ (@querypkgs) {
106 # we grab 4 fields here: repo, name/ver, installed, and desc
107 my @pkgfields = /^(.*?)\/(.*?) ?(\[.*\])?\n(.*)$/s;
108 # since installed is optional, we should fill it in if necessary
109 $pkgfields[2] = "" if not defined $pkgfields[2];
110 # check if the package was listed in the sync out
111 if (not exists $allpkgs{$pkgfields[1]}) {
112 $pkgfields[2] = "[installed]";
113 # add a fifth field that indicates original order (after sync)
114 push (@pkgfields, $cnt++);
115 # add our local-only package to the hash
116 $allpkgs{$pkgfields[1]} = [ @pkgfields ];
120 # sort by original order (the fifth field) and print
121 foreach $_ ( sort{ @{$allpkgs{$a}}[4] <=> @{$allpkgs{$b}}[4] } keys %allpkgs) {
122 my @v = @{$allpkgs{$_}};
123 my $line = "$v[0]/$v[1] $v[2]";
124 $line = to_color($line);
125 # print colorized "repo/pkgname pkgver" string with possible installed text
126 print "$line\n";
127 print "$v[3]\n";
130 #vim: set noet: