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); +} 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; }