2008-05-09 19:56:15 -04:00
|
|
|
#!/usr/bin/perl
|
2007-02-16 23:39:59 -05:00
|
|
|
# pacsearch - Adds color and install information to a 'pacman -Ss' search
|
|
|
|
#
|
2014-01-22 18:07:03 -05:00
|
|
|
# Copyright (C) 2008-2014 Dan McGee <dan@archlinux.org>
|
2008-05-09 19:56:15 -04:00
|
|
|
#
|
|
|
|
# Based off original shell script version:
|
2011-01-24 16:20:10 -05:00
|
|
|
# Copyright (C) 2006-2007 Dan McGee <dan@archlinux.org>
|
2007-02-16 23:39:59 -05:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2007-12-10 23:55:22 -05:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-02-16 23:39:59 -05:00
|
|
|
|
|
|
|
#TODO: colors flag on commandline
|
|
|
|
|
2008-05-09 19:56:15 -04:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2011-12-06 16:29:32 -05:00
|
|
|
my $myname = 'pacsearch';
|
2011-12-12 16:36:57 -05:00
|
|
|
my $myver = '@PACKAGE_VERSION@';
|
2008-05-09 19:56:15 -04:00
|
|
|
|
2011-12-12 16:36:57 -05:00
|
|
|
sub usage {
|
2013-11-08 02:53:33 -05:00
|
|
|
print "$myname (pacman) v$myver\n\n";
|
|
|
|
print "Add color and install information to a 'pacman -Ss' search\n\n";
|
|
|
|
print "Usage: $myname <pattern>\n\n";
|
2011-12-06 16:29:32 -05:00
|
|
|
print "Example: $myname ^gnome\n";
|
2011-12-12 16:36:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sub version {
|
|
|
|
printf "%s %s\n", $myname, $myver;
|
2014-01-22 18:07:03 -05:00
|
|
|
print "Copyright (C) 2008-2014 Dan McGee <dan\@archlinux.org>\n\n";
|
2011-12-12 16:36:57 -05:00
|
|
|
print "Based off original shell script version:\n";
|
|
|
|
print "Copyright (C) 2006-2007 Dan McGee <dan\@archlinux.org>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($#ARGV lt 0 || $ARGV[0] eq "--help" || $ARGV[0] eq "-h") {
|
|
|
|
usage;
|
2008-05-09 19:56:15 -04:00
|
|
|
if ($#ARGV lt 0) {
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
exit 0;
|
|
|
|
}
|
2007-02-16 23:39:59 -05:00
|
|
|
|
2011-12-12 16:36:57 -05:00
|
|
|
if ($ARGV[0] eq "--version" || $ARGV[0] eq "-V") {
|
|
|
|
version;
|
2008-05-09 19:56:15 -04:00
|
|
|
exit 0;
|
|
|
|
}
|
2007-02-16 23:39:59 -05:00
|
|
|
|
2008-05-09 19:56:15 -04:00
|
|
|
# define our colors to use when printing
|
|
|
|
my $CLR1 = "\e[0;34m";
|
|
|
|
my $CLR2 = "\e[0;32m";
|
|
|
|
my $CLR3 = "\e[0;35m";
|
|
|
|
my $CLR4 = "\e[0;36m";
|
|
|
|
my $CLR5 = "\e[0;31m";
|
|
|
|
my $CLR6 = "\e[0;33m";
|
|
|
|
my $CLR7 = "\e[1;36m";
|
|
|
|
my $INST = "\e[1;31m";
|
|
|
|
my $BASE = "\e[0m";
|
2007-02-16 23:39:59 -05:00
|
|
|
|
2010-05-25 11:23:12 -04:00
|
|
|
# color a "repo/pkgname pkgver" line based on the repository name
|
2008-05-09 19:56:15 -04:00
|
|
|
sub to_color {
|
|
|
|
my $line = shift;
|
2010-05-25 11:23:12 -04:00
|
|
|
# get the installed text colored first
|
|
|
|
$line =~ s/(\[.*\]$)/$INST$1$BASE/;
|
|
|
|
# and now the repo and dealings
|
2008-05-09 19:56:15 -04:00
|
|
|
$line =~ s/(^core\/.*)/$CLR1$1$BASE/;
|
|
|
|
$line =~ s/(^extra\/.*)/$CLR2$1$BASE/;
|
|
|
|
$line =~ s/(^community\/.*)/$CLR3$1$BASE/;
|
|
|
|
$line =~ s/(^testing\/.*)/$CLR4$1$BASE/;
|
2010-05-25 11:23:12 -04:00
|
|
|
$line =~ s/(^community-testing\/.*)/$CLR5$1$BASE/;
|
2011-01-24 16:20:10 -05:00
|
|
|
$line =~ s/(^multilib\/.*)/$CLR6$1$BASE/;
|
2008-05-09 19:56:15 -04:00
|
|
|
$line =~ s/(^local\/.*)/$CLR7$1$BASE/;
|
|
|
|
# any other unknown repository
|
|
|
|
$line =~ s/(^[\w-]*\/.*)/$CLR6$1$BASE/;
|
|
|
|
return $line;
|
|
|
|
}
|
2007-03-06 17:06:28 -05:00
|
|
|
|
2008-05-09 19:56:15 -04:00
|
|
|
my %allpkgs = ();
|
2007-03-06 17:06:28 -05:00
|
|
|
|
2008-07-24 21:34:11 -04:00
|
|
|
my $syncout = `pacman -Ss '@ARGV'`;
|
2008-05-09 19:56:15 -04:00
|
|
|
# split each sync search entry into its own array entry
|
|
|
|
my @syncpkgs = split(/\n^(?=\w)/m, $syncout);
|
|
|
|
# remove the extra \n from the last desc entry
|
|
|
|
if ($#syncpkgs >= 0) {
|
|
|
|
chomp($syncpkgs[$#syncpkgs]);
|
|
|
|
}
|
2007-02-16 23:39:59 -05:00
|
|
|
|
2008-05-09 19:56:15 -04:00
|
|
|
# counter var for packages, used here and in the query loop too
|
|
|
|
my $cnt = 0;
|
|
|
|
foreach $_ (@syncpkgs) {
|
2010-05-25 11:23:12 -04:00
|
|
|
# we grab 4 fields here: repo, name/ver, installed, and desc
|
|
|
|
my @pkgfields = /^(.*?)\/(.*?) ?(\[.*\])?\n(.*)$/s;
|
2011-01-24 16:20:10 -05:00
|
|
|
if(not @pkgfields) {
|
|
|
|
# skip any non-matching line and just print it for the user
|
|
|
|
print $_, "\n";
|
|
|
|
next;
|
|
|
|
}
|
2010-05-25 11:23:12 -04:00
|
|
|
# since installed is optional, we should fill it in if necessary
|
|
|
|
$pkgfields[2] = "" if not defined $pkgfields[2];
|
2008-05-09 19:56:15 -04:00
|
|
|
# add a fifth field that indicates original order
|
|
|
|
push (@pkgfields, $cnt++);
|
|
|
|
# add each sync pkg by name/ver to a hash table for quick lookup
|
|
|
|
$allpkgs{$pkgfields[1]} = [ @pkgfields ];
|
|
|
|
}
|
2007-02-16 23:39:59 -05:00
|
|
|
|
2008-07-24 21:34:11 -04:00
|
|
|
my $queryout = `pacman -Qs '@ARGV'`;
|
2008-05-09 19:56:15 -04:00
|
|
|
# split each querysearch entry into its own array entry
|
|
|
|
my @querypkgs = split(/\n^(?=\w)/m, $queryout);
|
|
|
|
# remove the extra \n from the last desc entry
|
|
|
|
if ($#querypkgs >= 0) {
|
|
|
|
chomp ($querypkgs[$#querypkgs]);
|
|
|
|
}
|
2007-02-16 23:39:59 -05:00
|
|
|
|
2008-05-09 19:56:15 -04:00
|
|
|
foreach $_ (@querypkgs) {
|
2010-05-25 11:23:12 -04:00
|
|
|
# we grab 4 fields here: repo, name/ver, installed, and desc
|
|
|
|
my @pkgfields = /^(.*?)\/(.*?) ?(\[.*\])?\n(.*)$/s;
|
2011-07-27 13:44:17 -04:00
|
|
|
# skip any non-matching line
|
|
|
|
next if not defined $pkgfields[1];
|
2010-05-25 11:23:12 -04:00
|
|
|
# since installed is optional, we should fill it in if necessary
|
|
|
|
$pkgfields[2] = "" if not defined $pkgfields[2];
|
2008-05-09 19:56:15 -04:00
|
|
|
# check if the package was listed in the sync out
|
2010-05-25 11:23:12 -04:00
|
|
|
if (not exists $allpkgs{$pkgfields[1]}) {
|
|
|
|
$pkgfields[2] = "[installed]";
|
2008-05-09 19:56:15 -04:00
|
|
|
# add a fifth field that indicates original order (after sync)
|
|
|
|
push (@pkgfields, $cnt++);
|
|
|
|
# add our local-only package to the hash
|
|
|
|
$allpkgs{$pkgfields[1]} = [ @pkgfields ];
|
|
|
|
}
|
|
|
|
}
|
2007-02-16 23:39:59 -05:00
|
|
|
|
2008-05-09 19:56:15 -04:00
|
|
|
# sort by original order (the fifth field) and print
|
|
|
|
foreach $_ ( sort{ @{$allpkgs{$a}}[4] <=> @{$allpkgs{$b}}[4] } keys %allpkgs) {
|
|
|
|
my @v = @{$allpkgs{$_}};
|
2010-05-25 11:23:12 -04:00
|
|
|
my $line = "$v[0]/$v[1] $v[2]";
|
2008-05-09 19:56:15 -04:00
|
|
|
$line = to_color($line);
|
2010-05-25 11:23:12 -04:00
|
|
|
# print colorized "repo/pkgname pkgver" string with possible installed text
|
|
|
|
print "$line\n";
|
|
|
|
print "$v[3]\n";
|
2008-05-09 19:56:15 -04:00
|
|
|
}
|
2007-02-16 23:39:59 -05:00
|
|
|
|
2008-05-09 19:56:15 -04:00
|
|
|
#vim: set noet:
|