1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-11-11 03:54:59 -05:00
pacman/contrib/pacsearch.in
Pierre Neidhardt fe19a0c58d pacsearch: colors are portable (ANSI) and have natural variable names
Signed-off-by: Pierre Neidhardt <ambrevar@gmail.com>
Signed-off-by: Allan McRae <allan@archlinux.org>
2014-02-02 16:12:29 +10:00

158 lines
4.9 KiB
Perl

#!/usr/bin/perl
# pacsearch - Perform a pacman search using both the local and the sync databases
#
# Copyright (C) 2008-2014 Dan McGee <dan@archlinux.org>
#
# Based off original shell script version:
# Copyright (C) 2006-2007 Dan McGee <dan@archlinux.org>
#
# 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
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#TODO: colors flag on commandline
use strict;
use warnings;
use Term::ANSIColor;
my $myname = 'pacsearch';
my $myver = '@PACKAGE_VERSION@';
sub usage {
print "$myname (pacman) v$myver\n\n";
print "Perform a pacman search using both the local and the sync databases.\n\n";
print "Usage: $myname <pattern>\n\n";
print "Example: $myname ^gnome\n";
}
sub version {
printf "%s %s\n", $myname, $myver;
print "Copyright (C) 2008-2014 Dan McGee <dan\@archlinux.org>\n\n";
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;
if ($#ARGV lt 0) {
exit 1;
}
exit 0;
}
if ($ARGV[0] eq "--version" || $ARGV[0] eq "-V") {
version;
exit 0;
}
# define our colors to use when printing
my($BLUE, $CYAN, $GREEN, $MAGENTA, $RED, $YELLOW, $BOLD, $RESET);
$BLUE = color('blue');
$CYAN = color('cyan');
$GREEN = color('green');
$MAGENTA = color('magenta');
$RED = color('red');
$YELLOW = color('yellow');
$BOLD = color('bold');
$RESET = color('reset');
# localization
my $LC_INSTALLED = `gettext pacman installed`;
# color a "repo/pkgname pkgver" line based on the repository name
sub to_color {
my $line = shift;
# get the installed text colored first
$line =~ s/(\[.*\]$)/$CYAN$1$RESET/;
# and now the repo and dealings
$line =~ s/(^core\/.*)/$MAGENTA$1$RESET/;
$line =~ s/(^extra\/.*)/$MAGENTA$1$RESET/;
$line =~ s/(^community\/.*)/$MAGENTA$1$RESET/;
$line =~ s/(^testing\/.*)/$MAGENTA$1$RESET/;
$line =~ s/(^community-testing\/.*)/$MAGENTA$1$RESET/;
$line =~ s/(^multilib\/.*)/$MAGENTA$1$RESET/;
$line =~ s/(^local\/.*)/$RED$1$RESET/;
# any other unknown repository
$line =~ s/(^[\w-]*\/.*)/$RED$1$RESET/;
return $line;
}
my %allpkgs = ();
my $syncout = `pacman -Ss '@ARGV'`;
# 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]);
}
# counter var for packages, used here and in the query loop too
my $cnt = 0;
foreach $_ (@syncpkgs) {
# we grab the following fields: repo, name, ver, group, installed, and desc
my @pkgfields = /^(.*?)\/(.*?) (.*?) ?(\(.*?\))? ?(\[.*\])?\n(.*)$/s;
if(not @pkgfields) {
# skip any non-matching line and just print it for the user
print $_, "\n";
next;
}
# 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];
# add a last 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 ];
}
my $queryout = `pacman -Qs '@ARGV'`;
# 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]);
}
foreach $_ (@querypkgs) {
# we grab the following fields: repo, name, ver, group, installed, and desc
my @pkgfields = /^(.*?)\/(.*?) (.*?) ?(\(.*?\))? ?(\[.*\])?\n(.*)$/s;
# my @pkgfields = /^(.*?)\/(.*?) ?(\[.*\])?\n(.*)$/s;
# skip any non-matching line
next if not defined $pkgfields[1];
# check if the package was listed in the sync out
if (not exists $allpkgs{$pkgfields[1]}) {
# since 'group' is optional, we should fill it in if necessary
$pkgfields[3] = "" if not defined $pkgfields[3];
$pkgfields[4] = "[$LC_INSTALLED]";
# add a last field that indicates original order (after sync)
push (@pkgfields, $cnt++);
# add our local-only package to the hash
$allpkgs{$pkgfields[1]} = [ @pkgfields ];
}
}
# sort by original order (the last field) and print
foreach $_ ( sort{ @{$allpkgs{$a}}[6] <=> @{$allpkgs{$b}}[6] } keys %allpkgs) {
my @v = @{$allpkgs{$_}};
my $line = "$v[0]/$v[1] $v[2]";
$line .= " $v[3]" if $v[3] ne "";
$line .= " $v[4]" if $v[4] ne "";
$line = to_color($line);
# print colorized "repo/pkgname pkgver ..." string with possible installed text
print "$line\n";
print "$v[5]\n";
}
#vim: set noet: