pacsearch: rewrite in perl

This rewrite in perl blows the socks off the old shell script version for
large searches:

$ time ./pacsearch.perl ^.*$ >/dev/null
real    0m0.836s
user    0m0.593s
sys     0m0.217s

$ time pacsearch.sh ^.*$ >/dev/null
real    1m53.818s
user    1m16.818s
sys     0m33.694s

Functionality and output is identical to the old version with the exception
of the old version's missing EOL after all the output.  It should be a lot
easier to add new things like the --color flag that has been a TODO at the
top of the script for a long time.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2008-05-09 18:56:15 -05:00
parent b49fc504ac
commit 1ba0d84da2

View file

@ -1,6 +1,9 @@
#!/bin/bash #!/usr/bin/perl
# pacsearch - Adds color and install information to a 'pacman -Ss' search # pacsearch - Adds color and install information to a 'pacman -Ss' search
# #
# Copyright (C) 2008 Dan McGee <dpmcgee@gmail.com>
#
# Based off original shell script version:
# Copyright (C) 2006-2007 Dan McGee <dpmcgee@gmail.com> # Copyright (C) 2006-2007 Dan McGee <dpmcgee@gmail.com>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
@ -18,72 +21,112 @@
#TODO: colors flag on commandline #TODO: colors flag on commandline
readonly progname="pacsearch" use strict;
readonly version="1.0" use warnings;
readonly CLR1='\\\e[0;34m' my $progname = "pacsearch";
readonly CLR2='\\\e[0;32m' my $version = "2.0";
readonly CLR3='\\\e[0;35m'
readonly CLR4='\\\e[0;36m'
readonly CLR5='\\\e[0;31m'
readonly CLR6='\\\e[0;33m'
readonly CLR7='\\\e[1;36m'
readonly INST='\\\e[1;31m'
readonly BASE='\\\e[0m'
if [ "$1" = "--help" -o "$1" = "-h" ]; then if ($#ARGV lt 0 || $ARGV[0] eq "--help" || $ARGV[0] eq "-h") {
echo "Usage: $progname <pattern>" print "$progname - Add color and install information to a pacman -Ss search\n";
echo "Ex: $progname ^gnome" print "Usage: $progname <pattern>\n";
exit 0 print "Example: $progname ^gnome\n";
fi if ($#ARGV lt 0) {
exit 1;
}
exit 0;
}
if [ "$1" = "--version" -o "$1" = "-v" ]; then if ($ARGV[0] eq "--version" || $ARGV[0] eq "-v") {
echo "$progname version $version" print "$progname version $version\n";
echo "Copyright (C) 2006-2007 Dan McGee" print "Copyright (C) 2006-2008 Dan McGee\n";
exit 0 exit 0;
fi }
if [ -z "$1" -o "${1:0:1}" = "-" ]; then # define our colors to use when printing
echo "Usage: $progname <pattern>" my $CLR1 = "\e[0;34m";
echo "Ex: $progname ^gnome" my $CLR2 = "\e[0;32m";
exit 1 my $CLR3 = "\e[0;35m";
fi 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";
my $INSTMARK = $INST."***";
# Make two temp files and send output of commands to these files # color a "repo/pkgname pkgver" line based on the respository name
querydump=$(mktemp) sub to_color {
pacman -Qs $1 > $querydump my $line = shift;
syncdump=$(mktemp) $line =~ s/(^core\/.*)/$CLR1$1$BASE/;
pacman -Ss $1 > $syncdump $line =~ s/(^extra\/.*)/$CLR2$1$BASE/;
$line =~ s/(^community\/.*)/$CLR3$1$BASE/;
$line =~ s/(^testing\/.*)/$CLR4$1$BASE/;
$line =~ s/(^unstable\/.*)/$CLR5$1$BASE/;
$line =~ s/(^custom\/.*)/$CLR6$1$BASE/;
$line =~ s/(^local\/.*)/$CLR7$1$BASE/;
# any other unknown repository
$line =~ s/(^[\w-]*\/.*)/$CLR6$1$BASE/;
return $line;
}
# Strip descriptions and 'local/' from -Qs query my %allpkgs = ();
instpkg=$(mktemp)
egrep '^[^ ]' $querydump | sed -e 's@^local/@@' > $instpkg
# Add pkgs not in sync db, mark pkgs that are installed my $syncout = `pacman -Ss @ARGV`;
cat $instpkg | while read -r pkg; do # split each sync search entry into its own array entry
if [ -z "$(grep "$pkg" $syncdump)" ]; then my @syncpkgs = split(/\n^(?=\w)/m, $syncout);
# grep package name; pipe to another grep that prints at most one # remove the extra \n from the last desc entry
# line starting with 'local/', allows for comments >1 line if ($#syncpkgs >= 0) {
grep -A10 "$pkg" $querydump | grep -A10 -m1 "local/" >> $syncdump chomp($syncpkgs[$#syncpkgs]);
fi }
sed -i "s@^\(.\+/$pkg\)@\***\1@" $syncdump
done
# Print colorized package list and descriptions to screen # counter var for packages, used here and in the query loop too
echo -e "$(sed -r \ my $cnt = 0;
-e "s@core/.*@$CLR1&$BASE@" \ foreach $_ (@syncpkgs) {
-e "s@extra/.*@$CLR2&$BASE@" \ # we grab 3 fields here: repo, name/ver, and desc
-e "s@community/.*@$CLR3&$BASE@" \ my @pkgfields = /^(.*?)\/(.*?)\n(.*)$/s;
-e "s@testing/.*@$CLR4&$BASE@" \ # add a fourth field that will indicate install status
-e "s@unstable/.*@$CLR5&$BASE@" \ push (@pkgfields, "");
-e "s@custom/.*@$CLR6&$BASE@" \ # add a fifth field that indicates original order
-e "s@local/.*@$CLR7&$BASE@" \ push (@pkgfields, $cnt++);
-e "s@(^|\*\*\*)([[:alnum:]]*/.* .*)@\1$CLR6\2$BASE@" \ # add each sync pkg by name/ver to a hash table for quick lookup
-e "s@\*\*\*@$INST&@" \ $allpkgs{$pkgfields[1]} = [ @pkgfields ];
< $syncdump )" }
echo -en "\e[0m"
rm $querydump my $queryout = `pacman -Qs @ARGV`;
rm $syncdump # split each querysearch entry into its own array entry
rm $instpkg 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 3 fields here: repo, name/ver, and desc
my @pkgfields = /^(.*?)\/(.*?)\n(.*)$/s;
# check if the package was listed in the sync out
# if it is we want to mark it with a *** marker
if (exists $allpkgs{$pkgfields[1]}) {
# mark it in our fourth field as installed
@{ $allpkgs{$pkgfields[1]} }[3] = $INSTMARK;
} else {
# add a fourth field that will indicate install status
push (@pkgfields, $INSTMARK);
# 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 ];
}
}
# sort by original order (the fifth field) and print
foreach $_ ( sort{ @{$allpkgs{$a}}[4] <=> @{$allpkgs{$b}}[4] } keys %allpkgs) {
my @v = @{$allpkgs{$_}};
my $line = "$v[0]/$v[1]";
$line = to_color($line);
# print install marker + colorized "repo/pkgname pkgver" string
print "$v[3]$line\n";
print "$v[2]\n";
}
#vim: set noet: