On 14-01-08 09:43:25, Dan McGee wrote:
On Tue, Jan 7, 2014 at 8:16 AM, Pierre Neidhardt <ambrevar@gmail.com> wrote:
[...] -# 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"; - -# color a "repo/pkgname pkgver" line based on the repository name +# define our colors to use when printing; we try to stick to pacman colors +my $CLR_CORE = "\e[1;94m"; # blue +my $CLR_EXTRA = "\e[1;92m"; # green +my $CLR_COMMUNITY = "\e[1;95m"; # magenta +my $CLR_TESTING = "\e[1;96m"; # cyan +my $CLR_COMMUNITY_TESTING = "\e[1;91m"; # red +my $CLR_MULTILIB = "\e[1;93m"; # yello +my $CLR_LOCAL = "\e[1;96m"; # cyan +my $CLR_INST = "\e[1;96m"; # cyan +my $CLR_PACKAGE = "\e[1m"; # bold +my $CLR_VERSION = "\e[1;92m"; # green +my $CLR_GROUP = "\e[1;94m"; # blue +my $CLR_BASE = "\e[0m"; +
This starts to cross the line between being agnostic and Arch-specific. I did have some regex magic to detect known-named repos in there, but the color variables were named in such a way that anyone could patch/change/etc. the code and still have things make sense. You might want to rename them to CLR_BLUE, CLR_GREEN, etc. rather than hardcode repo names.
You are right. I think naming colors numerically on a per-repo basis was just wrong in the first place. A smarter way to do it would be, as you suggest, to name variables following their formatting property. But this does not change the agnosticism of the script: repo are still hard-coded.
$line =~ s/(^core\/.*)/$CLR1$1$BASE/;
We could get the repos from pacman.conf or from /var/lib/pacman. This would be the only variable needing a change to port pacsearch to another OS. How to we get the list of active repos? I'm not sure about this. $ ls /var/lib/pacman/sync should do the trick. Otherwise from match /^[^\#]*\[([[:alpha:]]+)\]/ on /etc/pacman.conf, \1 should be the repo name if different from "options". Looks clumsy though. Otherwise we could use a true conf parser. How do we color them? We need to apply as many different colors as possible, while being persistent ("multilib" should always be yellow, whatever other active repo you have). We could hash the names modulo 6 or something like that.
To top it off, it now isn't clear whether BASE is a repo name, another package field (PKGBASE?), etc. when in fact it is really indicating color reset (yes, I probably chose a bad name originally). Also, I really dislike inline comments. To me it is a code smell to better name your variables.
I agree with all you comments indeed. But they all apply to the current version as well. CLR1, etc. do not mean much and are not flexible. (Why only 7 colors? this is Arch-specific too.) Nonetheless you are right, it's no use sticking to bad names, I should have changed them rightaway. I'll send an update very soon.
+# color a "repo/pkgname pkgver (goups) [installed]" line sub to_color { my $line = shift; - # get the installed text colored first - $line =~ s/(\[.*\]$)/$INST$1$BASE/; - # and now the repo and dealings - $line =~ s/(^core\/.*)/$CLR1$1$BASE/; - $line =~ s/(^extra\/.*)/$CLR2$1$BASE/; - $line =~ s/(^community\/.*)/$CLR3$1$BASE/; - $line =~ s/(^testing\/.*)/$CLR4$1$BASE/; - $line =~ s/(^community-testing\/.*)/$CLR5$1$BASE/; - $line =~ s/(^multilib\/.*)/$CLR6$1$BASE/; - $line =~ s/(^local\/.*)/$CLR7$1$BASE/; + # get the installed text colored first (between square brackets) + $line =~ s/(\[.*\]$)/$CLR_INST$1$CLR_BASE/; + # group (between parentheses) + $line =~ s/(\(.*\))/$CLR_GROUP$1$CLR_BASE/; + # version (second field) + $line =~ s/^([^ ]+) ([^ ]+) /$1 $CLR_VERSION$2$CLR_BASE /; + # name (word after slash) + $line =~ s/\/([\w-]*)/\/$CLR_PACKAGE$1$CLR_BASE/; + # repo (word before slash inclusive) + $line =~ s/(^core\/)/$CLR_CORE$1$CLR_BASE/; + $line =~ s/(^extra\/)/$CLR_EXTRA$1$CLR_BASE/; + $line =~ s/(^community\/)/$CLR_COMMUNITY$1$CLR_BASE/; + $line =~ s/(^testing\/)/$CLR_TESTING$1$CLR_BASE/; + $line =~ s/(^community-testing\/)/$CLR_COMMUNITY_TESTING$1$CLR_BASE/; + $line =~ s/(^multilib\/)/$CLR_MULTILIB$1$CLR_BASE/; + $line =~ s/(^local\/)/$CLR_LOCAL$1$CLR_BASE/; # any other unknown repository - $line =~ s/(^[\w-]*\/.*)/$CLR6$1$BASE/; + $line =~ s/(^[\w-]*\/)/$CLR_MULTILIB$1$CLR_BASE/; return $line; }