Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions example.pl
Original file line number Diff line number Diff line change
@@ -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);
}
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