Wednesday, September 24, 2008

console highlighter

i've been squinting at my glassfish log tails for far too long now. i decided to write a small script that would highlight rows containing particular keywords.

this script will show error messages in red, and warning messages in yellow:

#!/usr/bin/perl

use strict;
use Term::ANSIColor;

while (<stdin>){
if ($_ =~ m/(exception|error)/i){
print color 'bold red';
print $_;
print color 'reset';
}elsif ($_ =~ m/(warning)/i){
print color 'bold yellow';
print $_;
print color 'reset';
}else{
print $_;
}
}
this was pretty straight forward and can be easily modified. to use this, i put this in a script called highlighter.pl, and pipe my logs through it:

tail -F server.log | ~/bin/highlighter.pl

Tuesday, September 23, 2008

Show Selectors

here is a simple perl script for parsing a project for css files. it will display occurrences of css id's and classes. this is useful if you need to display unused id's/classes.

show-selectors.pl


#!/usr/bin/perl
use File::List;
use CSS;

my $cssl = new File::List(".");
my @css = @{ $cssl->find("\.css\$") };
my $cssc = @css;
my $seen;
my @final;

for my $cssfile (@css){
my $css = CSS->new();
$css->read_file($cssfile);

for my $style ( @{ $css->{styles} }){
for my $selector ( @{ $style->{selectors}}){
my $name = $selector->{name};
my @multiple = split(/ /, $name);

foreach $multiple (@multiple){
if ($multiple =~ /.*(\.|\#)(.*)/){
my $sel = $1 . $2;
$seen{$sel}++;
next if $seen{$sel} > 1;
push(@final, $sel);
}
}
}
}
}

foreach $selector (@final){
if ($selector =~ /.(.*)/){
my $total = 0;
my $output = `grep -c '$1' . -R | grep -v -E '.svn|css'`;
foreach $line (split(/\n/, $output)){
if ($line =~ /(.*):(\d)/){
my $occ = eval($2);
$total += $occ;
}
}
print("$selector: $total\n");
}
}