From 4844bc5eeafbb4c91dd99062def8ab9fae8679c0 Mon Sep 17 00:00:00 2001 From: Robert Vollmer Date: Fri, 10 Jun 2022 09:36:42 +0200 Subject: [PATCH 1/2] Add compatibility with Perl 5.8 --- lib/Text/Table/Tiny.pm | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/Text/Table/Tiny.pm b/lib/Text/Table/Tiny.pm index a53bf47..7c1650d 100644 --- a/lib/Text/Table/Tiny.pm +++ b/lib/Text/Table/Tiny.pm @@ -1,6 +1,6 @@ package Text::Table::Tiny; -use 5.010; +use 5.008; use strict; use warnings; use utf8; @@ -49,9 +49,9 @@ sub generate_table my @rows = @$rows; my @widths = _calculate_widths($rows); - $param{style} //= 'classic'; + $param{style} = 'classic' if not defined $param{style}; - $param{indent} //= ''; + $param{indent} = '' if not defined $param{indent}; $param{indent} = ' ' x $param{indent} if $param{indent} =~ /^[0-9]+$/; my $style = $param{style}; @@ -151,7 +151,7 @@ sub _text_row my $text = $param->{indent}.$char->{VR}; for (my $i = 0; $i < @$widths; $i++) { - $text .= _format_column($columns[$i] // '', $widths->[$i], $align->[$i] // 'l', $param, $char); + $text .= _format_column($columns[$i], $widths->[$i], $align->[$i], $param, $char); $text .= $char->{VR}; } $text .= "\n"; @@ -162,6 +162,8 @@ sub _text_row sub _format_column { my ($text, $width, $align, $param, $char) = @_; + $text = '' if not defined $text; + $align = 'l' if not defined $align; my $pad = $param->{compact} ? '' : ' '; if ($align eq 'r' || $align eq 'right') { From d57779dc49fb1fd142bee11f6e7ae9ed5a598e88 Mon Sep 17 00:00:00 2001 From: Robert Vollmer Date: Fri, 10 Jun 2022 10:48:46 +0200 Subject: [PATCH 2/2] Added left_and_right parameter to skip left & right borders --- lib/Text/Table/Tiny.pm | 43 ++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/lib/Text/Table/Tiny.pm b/lib/Text/Table/Tiny.pm index 7c1650d..a3bb488 100644 --- a/lib/Text/Table/Tiny.pm +++ b/lib/Text/Table/Tiny.pm @@ -25,6 +25,8 @@ my %arguments = ( header_row => "if true, indicates that the first row is a header row", separate_rows => "if true, a separate rule will be drawn between each row", top_and_tail => "if true, miss out top and bottom edges of table", + left_and_right => "if true, miss out left and right edges of table", + edges => "if true, miss out edges of table", align => "either single alignment, or an array per of alignments per col", style => "styling of table, one of classic, boxrule, or norule", indent => "indent every row of the table a certain number of spaces", @@ -54,6 +56,8 @@ sub generate_table $param{indent} = '' if not defined $param{indent}; $param{indent} = ' ' x $param{indent} if $param{indent} =~ /^[0-9]+$/; + $param{top_and_tail} = $param{left_and_right} = 1 if $param{edges}; + my $style = $param{style}; croak "unknown style '$style'" if not exists($charsets{ $style }); my $char = $charsets{$style}; @@ -111,12 +115,19 @@ sub _rule_row my ($param, $widths, $le, $hr, $cross, $re) = @_; my $pad = $param->{compact} ? '' : $hr; - return $param->{indent} - .$le - .join($cross, map { $pad.($hr x $_).$pad } @$widths) - .$re + if ($param->{left_and_right}) { + return $param->{indent} + .join($pad . $cross . $pad, map { $hr x $_ } @$widths) ."\n" ; + } else { + return $param->{indent} + .$le + .join($cross, map { $pad.($hr x $_).$pad } @$widths) + .$re + ."\n" + ; + } } sub _header_row @@ -148,15 +159,23 @@ sub _text_row { my ($param, $row, $widths, $align, $char) = @_; my @columns = @$row; - my $text = $param->{indent}.$char->{VR}; + if ($param->{left_and_right}) { + my $pad = $param->{compact} ? '' : ' '; + return $param->{indent} + .join($pad . $char->{VR} . $pad, map { _format_column($columns[$_], $widths->[$_], $align->[$_], $param, $char) } 0 .. $#$widths) + ."\n" + ; + } else { + my $text = $param->{indent}.$char->{VR}; - for (my $i = 0; $i < @$widths; $i++) { - $text .= _format_column($columns[$i], $widths->[$i], $align->[$i], $param, $char); - $text .= $char->{VR}; - } - $text .= "\n"; + for (my $i = 0; $i < @$widths; $i++) { + $text .= _format_column($columns[$i], $widths->[$i], $align->[$i], $param, $char); + $text .= $char->{VR}; + } + $text .= "\n"; - return $text; + return $text; + } } sub _format_column @@ -164,7 +183,7 @@ sub _format_column my ($text, $width, $align, $param, $char) = @_; $text = '' if not defined $text; $align = 'l' if not defined $align; - my $pad = $param->{compact} ? '' : ' '; + my $pad = $param->{compact} || $param->{left_and_right} ? '' : ' '; if ($align eq 'r' || $align eq 'right') { return $pad.' ' x ($width - tty_width($text)).$text.$pad;