From 5105e62bc889c0a74e8a24c864d79adc3a63de67 Mon Sep 17 00:00:00 2001 From: Ricardo Signes Date: Sun, 25 Jun 2023 11:51:49 -0400 Subject: [PATCH 1/2] example.pl: add a second example, with color This demonstrates that color codes are ignored for width. It also demonstrates a current bug: that color codes leak from column to column. --- example.pl | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/example.pl b/example.pl index 4b6bd64..cc0da77 100644 --- a/example.pl +++ b/example.pl @@ -1,10 +1,35 @@ +use strict; +use warnings; + use lib 'lib'; + +use Term::ANSIColor; use Text::FormatTable; -my $table = Text::FormatTable->new('r|l'); -$table->head('a', 'b'); -$table->rule('='); -$table->row('this a test, a nice test', 'a test!'); -$table->rule; -$table->row('you mean it\'s really a test?', 'yep'); -$table->rule('='); -print $table->render(20); + +{ + my $table = Text::FormatTable->new('r|l'); + $table->head('a', 'b'); + $table->rule('='); + $table->row('this a test, a nice test', 'a test!'); + $table->rule; + $table->row('you mean it\'s really a test?', 'yep'); + $table->rule('='); + print $table->render(20); +} + +print "\n", "-" x 78, "\n\n"; + + +{ + my $table = Text::FormatTable->new('r|l'); + $table->head('a', 'b'); + $table->rule('='); + $table->row( + 'this a ' . colored(['bright_blue'], 'test, a nice') . ' test', + 'a test you can count on!', + ); + $table->rule; + $table->row('you mean it\'s really a test?', 'yep'); + $table->rule('='); + print $table->render(20); +} From cbf06ea546308821ae653875833b9b1dcd31a086 Mon Sep 17 00:00:00 2001 From: Ricardo Signes Date: Sun, 25 Jun 2023 11:53:43 -0400 Subject: [PATCH 2/2] contain and continue color codes If a column contains color codes, we should keep the color contained to the column itself, stopping it at the column boundary and starting it again on the next continuation. This may have a more subtle bug, related to padding between end of data in a column and its boundary, when the color codes have added a background color. --- lib/Text/FormatTable.pm | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/Text/FormatTable.pm b/lib/Text/FormatTable.pm index 5c2a7af..d04a945 100644 --- a/lib/Text/FormatTable.pm +++ b/lib/Text/FormatTable.pm @@ -5,6 +5,8 @@ use strict; use warnings; use vars qw($VERSION); +use Term::ANSIColor (); + $VERSION = '1.03'; =head1 NAME @@ -84,13 +86,14 @@ sub _wrap_line($$) my ($width, $text) = @_; my $width_m1 = $width-1; my @t = ($text); - while(1) { + + QUEUE: while(1) { my $t = pop @t; my $l = _uncolorized_length $t; if($l <= $width){ # last line is ok => done push @t, $t; - return \@t; + last QUEUE; } elsif($t =~ /^(.{0,$width_m1}\S)\s+(\S.*?)$/) { # farest space < width @@ -122,9 +125,23 @@ sub _wrap_line($$) push @t, $left; push @t, $right; - return \@t; + last QUEUE; } } + + my $prefix = q{}; + + for (@t) { + my @codes = m{ (\e\[ [\d;]* m) }xg; + + next unless @codes || $prefix; + + my $new_prefix = join q{}, @codes; + + $_ = "$prefix$_" . Term::ANSIColor::color('reset'); + $prefix = $new_prefix if length $new_prefix; + } + return \@t; }