Skip to content

Commit

Permalink
contain and continue color codes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rjbs committed Jun 25, 2023
1 parent 5105e62 commit cbf06ea
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lib/Text/FormatTable.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use strict;
use warnings;
use vars qw($VERSION);

use Term::ANSIColor ();

$VERSION = '1.03';

=head1 NAME
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit cbf06ea

Please sign in to comment.