SVN: toys/rsget.pl/RSGet: Get.pm HTTPRequest.pm Line.pm

sparky sparky at pld-linux.org
Sun Dec 19 20:42:14 CET 2010


Author: sparky
Date: Sun Dec 19 20:42:14 2010
New Revision: 12001

Modified:
   toys/rsget.pl/RSGet/Get.pm
   toys/rsget.pl/RSGet/HTTPRequest.pm
   toys/rsget.pl/RSGet/Line.pm
Log:
- some colors on terminal


Modified: toys/rsget.pl/RSGet/Get.pm
==============================================================================
--- toys/rsget.pl/RSGet/Get.pm	(original)
+++ toys/rsget.pl/RSGet/Get.pm	Sun Dec 19 20:42:14 2010
@@ -83,7 +83,7 @@
 		my $outifstr = $outif ? "[$outif]" :  "";
 
 		hadd %$self,
-			_line => new RSGet::Line( "[$getter->{short}]$outifstr " );
+			_line => new RSGet::Line( "[$getter->{short}]$outifstr ", undef, undef, "green" );
 		$self->print( "start" );
 		$self->linedata();
 	}

Modified: toys/rsget.pl/RSGet/HTTPRequest.pm
==============================================================================
--- toys/rsget.pl/RSGet/HTTPRequest.pm	(original)
+++ toys/rsget.pl/RSGet/HTTPRequest.pm	Sun Dec 19 20:42:14 2010
@@ -344,12 +344,9 @@
 	$r = '<fieldset id="log"><legend>log</legend><ul>';
 	
 	for ( my $i = $#RSGet::Line::dead; $i >= $start; $i-- ) {
-		my $line = $RSGet::Line::dead[ $i ];
+		my ( $line, $color ) = @{ $RSGet::Line::dead[ $i ] };
 		my $class = '';
-		$class = ' class="blue"' if $line =~ /PARTIAL/;
-		$class = ' class="green"' if $line =~ /DONE/;
-		$class = ' class="orange"' if $line =~ /^\[\S+\] WARNING/;
-		$class = ' class="red"' if $line =~ /ERROR/;
+		$class = qq{ class="$color"} if $color;
 		$r .= qq#<li$class># . href( $line ) . '</li>';
 	}
 

Modified: toys/rsget.pl/RSGet/Line.pm
==============================================================================
--- toys/rsget.pl/RSGet/Line.pm	(original)
+++ toys/rsget.pl/RSGet/Line.pm	Sun Dec 19 20:42:14 2010
@@ -19,6 +19,23 @@
 my $term_size_columns;
 my $term_size_rows;
 
+my %color_to_term = (
+	red => 31,
+	green => 32,
+	yellow => 33,
+	orange => 33,
+	blue => 34,
+	magenta => 35,
+	cyan => 36,
+	bold => 1,
+	"" => 0,
+);
+sub color_term
+{
+	my $color = shift || "";
+	return "\033[" . $color_to_term{$color} . "m";
+}
+
 sub term_size
 {
 	local $SIG{__DIE__};
@@ -51,19 +68,20 @@
 	my @print;
 	my @newdead;
 
+	my $endcolor = color_term();
 	if ( $last_day != $l[3] ) {
 		$last_day = $l[3];
 		my $date = sprintf "[Current date: %d-%.2d-%.2d]", $l[5] + 1900, $l[4] + 1, $l[3];
-		push @print, "\r" . $date . "\033[J\n";
-		push @newdead, $date;
+		push @print, "\r" . color_term( "green" ) . $date . $endcolor . "\033[J\n";
+		push @newdead, [ $date, "green" ];
 	}
 
 	foreach my $key ( sort { $a <=> $b } keys %dead ) {
-		my $text = $dead{$key};
+		my ( $text, $color ) = @{ $dead{$key} };
 		$text = $time . $text if $text =~ /\S/;
 
-		push @print, "\r" . $text . "\033[J\n";
-		push @newdead, $text;
+		push @print, "\r" . color_term( $color ) . $text . $endcolor . "\033[J\n";
+		push @newdead, [ $text, $color ];
 	}
 
 	print @print unless $nooutput;
@@ -103,12 +121,16 @@
 			$status[ $#status ] .= $s;
 		}
 	}
-	my @print = ( " \\$h/" );
+
+	my $endcolor = color_term();
+	my $bold = color_term( "bold" );
+
+	my @print = ( " $bold\\$h/$endcolor" );
 	foreach ( @status ) {
-		my $l = " |" . ( " " x ($columns - 4 - length $_ )) . $_ . "|";
+		my $l = " $bold|$endcolor" . ( " " x ($columns - 4 - length $_ )) . $_ . "$bold|$endcolor";
 		push @print, $l;
 	}
-	push @print, " /$horiz\\";
+	push @print, " $bold/$horiz\\$endcolor";
 	print map { "\r\n$_\033[K" } @print;
 	return scalar @print;
 }
@@ -119,6 +141,7 @@
     my $columns = shift() - 1;
 	my @print;
 
+	my $endcolor = color_term();
 	foreach my $key ( sort { $a <=> $b } keys %active ) {
 		my $line = $active{$key};
 
@@ -126,7 +149,7 @@
 		my $tl = length $line->[0] . $text;
 		substr $text, 4, $tl - $columns + 3, '...'
 			if $tl > $columns;
-		push @print, "\r\n\033[K" . $line->[0] . $text;
+		push @print, "\r\n\033[K" . color_term( $line->[3] ) . $line->[0] . $text . $endcolor;
 	}
 
 	print @print;
@@ -157,10 +180,11 @@
 	my $head = shift;
 	my $text = shift;
 	my $assoc = shift;
+	my $color = shift;
 	$head = "" unless defined $head;
 
 	my $line = "" . ($last_line++);
-	$active{ $line } = [ $head, "", $assoc ];
+	$active{ $line } = [ $head, "", $assoc, $color ];
 
 	my $self = \$line;
 	bless $self, $class;
@@ -190,6 +214,13 @@
 	$active{ $$self }->[2] = $data;
 }
 
+sub color
+{
+	my $self = shift;
+	my $color = shift;
+	$active{ $$self }->[3] = $color;
+}
+
 sub clone
 {
 	my $self = shift;
@@ -202,7 +233,7 @@
 	my $self = shift;
 	my $line = $$self;
 	my $l = $active{ $line };
-	$dead{ $line } = $l->[0] . $l->[1];
+	$dead{ $line } = [ $l->[0] . $l->[1], $l->[3] ];
 	delete $active{ $line };
 }
 
@@ -227,7 +258,7 @@
 	}
 
 	$SIG{__WARN__} = sub {
-		new RSGet::Line( "WARNING: ", shift );
+		RSGet::Line->new( "WARNING: ", shift, undef, "orange" );
 		update();
 	};
 


More information about the pld-cvs-commit mailing list