| 1 | package Vote::Count::TextTableTiny; | ||||||||||||||||||||||||||||||||||
| 2 | 36 | $Vote::Count::TextTableTiny::VERSION = '1.10'; | |||||||||||||||||||||||||||||||||
| 3 | 36 | ▶ | use 5.022; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 36 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 36 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 36 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 36 | ▶ | use parent 'Exporter'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 36 | ▶ | use Carp qw/ croak /; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 36 | ▶ | use Ref::Util 0.202 qw/ is_arrayref /; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 36 | ▶ | use String::TtyLength 0.02 qw/ tty_width /; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | 36 | our @EXPORT_OK = qw/ generate_table /; | |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | # Legacy package globals, that can be used to customise the look. | ||||||||||||||||||||||||||||||||||
| 15 | # These are only used in the "classic" style. | ||||||||||||||||||||||||||||||||||
| 16 | # I wish I could drop them, but I don't want to break anyone's code. | ||||||||||||||||||||||||||||||||||
| 17 | 36 | our $COLUMN_SEPARATOR = '|'; | |||||||||||||||||||||||||||||||||
| 18 | 36 | our $ROW_SEPARATOR = '-'; | |||||||||||||||||||||||||||||||||
| 19 | 36 | our $CORNER_MARKER = '+'; | |||||||||||||||||||||||||||||||||
| 20 | 36 | our $HEADER_ROW_SEPARATOR = '='; | |||||||||||||||||||||||||||||||||
| 21 | 36 | our $HEADER_CORNER_MARKER = 'O'; | |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | 36 | my %arguments = ( | |||||||||||||||||||||||||||||||||
| 24 | rows => "the rows, including a possible header row, of the table", | ||||||||||||||||||||||||||||||||||
| 25 | header_row => "if true, indicates that the first row is a header row", | ||||||||||||||||||||||||||||||||||
| 26 | separate_rows => "if true, a separate rule will be drawn between each row", | ||||||||||||||||||||||||||||||||||
| 27 | top_and_tail => "if true, miss out top and bottom edges of table", | ||||||||||||||||||||||||||||||||||
| 28 | align => "either single alignment, or an array per of alignments per col", | ||||||||||||||||||||||||||||||||||
| 29 | style => "styling of table, one of classic, boxrule, or norule", | ||||||||||||||||||||||||||||||||||
| 30 | indent => "indent every row of the table a certain number of spaces", | ||||||||||||||||||||||||||||||||||
| 31 | compact => "narrow columns (no space either side of content)", | ||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | 36 | my %charsets = ( | |||||||||||||||||||||||||||||||||
| 35 | classic => { TLC => '+', TT => '+', TRC => '+', HR => '-', VR => '|', FHR => '=', LT => '+', RT => '+', FLT => 'O', FRT => 'O', HC => '+', FHC => 'O', BLC => '+', BT => '+', BRC => '+' }, | ||||||||||||||||||||||||||||||||||
| 36 | boxrule => { TLC => '┌', TT => '┬', TRC => '┐', HR => '─', VR => '│', FHR => '═', LT => '├', RT => '┤', FLT => '╞', FRT => '╡', HC => '┼', FHC => '╪', BLC => '└', BT => '┴', BRC => '┘' }, | ||||||||||||||||||||||||||||||||||
| 37 | norule => { TLC => ' ', TT => ' ', TRC => ' ', HR => ' ', VR => ' ', FHR => ' ', LT => ' ', RT => ' ', FLT => ' ', FRT => ' ', HC => ' ', FHC => ' ', BLC => ' ', BT => ' ', BRC => ' ' }, | ||||||||||||||||||||||||||||||||||
| 38 | markdown => { | ||||||||||||||||||||||||||||||||||
| 39 | TLC => '|', TT => ' ', TRC => '|', HR => '-', | ||||||||||||||||||||||||||||||||||
| 40 | VR => '|', FHR => ' ', LT => '|', RT => '|', | ||||||||||||||||||||||||||||||||||
| 41 | FLT => ' ', FRT => ' ', HC => '|', FHC => ' ', | ||||||||||||||||||||||||||||||||||
| 42 | BLC => '|', BT => ' ', BRC => '|', | ||||||||||||||||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | sub generate_table | ||||||||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||||||||
| 48 | 589 | ▶ | my %param = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
generate_table: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | 589 | foreach my $arg (keys %param) { | |||||||||||||||||||||||||||||||||
| 51 | 993 | ▶ | croak "unknown argument '$arg'" if not exists $arguments{$arg}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | 588 | ▶ | my $rows = $param{rows} or croak "you must pass the 'rows' argument!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 55 | 587 | my @rows = @$rows; | |||||||||||||||||||||||||||||||||
| 56 | 587 | my @widths = _calculate_widths($rows); | |||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||
| 58 | 587 | ▶ | $param{style} //= 'classic'; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$param{"style"} //= "classic"
MC/DC100%1/1
$param{"style"} //= "classic"
$param{"style"}
Truth table100%2/2
A
A= $param{"style"}
| |||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | 587 | ▶ | $param{indent} //= ''; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$param{"indent"} //= ""
MC/DC100%1/1
$param{"indent"} //= ""
$param{"indent"}
Truth table100%2/2
A
A= $param{"indent"}
| |||||||||||||||||||||||||||||||||||
| 61 | 587 | ▶ | $param{indent} = ' ' x $param{indent} if $param{indent} =~ /^[0-9]+$/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||
| 63 | 587 | my $style = $param{style}; | |||||||||||||||||||||||||||||||||
| 64 | 587 | ▶ | croak "unknown style '$style'" if not exists($charsets{ $style }); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 65 | 587 | my $char = $charsets{$style}; | |||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||
| 67 | 587 | ▶ | if ($style eq 'classic') { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 68 | 281 | $char->{TLC} = $char->{TRC} = $char->{TT} = $char->{LT} = $char->{RT} = $char->{HC} = $char->{BLC} = $char->{BT} = $char->{BRC} = $CORNER_MARKER; | |||||||||||||||||||||||||||||||||
| 69 | 281 | $char->{HR} = $ROW_SEPARATOR; | |||||||||||||||||||||||||||||||||
| 70 | 281 | $char->{VR} = $COLUMN_SEPARATOR; | |||||||||||||||||||||||||||||||||
| 71 | 281 | $char->{FLT} = $char->{FRT} = $char->{FHC} = $HEADER_CORNER_MARKER; | |||||||||||||||||||||||||||||||||
| 72 | 281 | $char->{FHR} = $HEADER_ROW_SEPARATOR; | |||||||||||||||||||||||||||||||||
| 73 | } elsif ( $style eq 'markdown') { | ||||||||||||||||||||||||||||||||||
| 74 | 302 | _md_validate_data( $rows ); | |||||||||||||||||||||||||||||||||
| 75 | 299 | $param{'header_row'} = 1; | |||||||||||||||||||||||||||||||||
| 76 | 299 | $param{'top_and_tail'} = 1; | |||||||||||||||||||||||||||||||||
| 77 | 299 | $param{'separate_rows'} = 0; | |||||||||||||||||||||||||||||||||
| 78 | 299 | $param{'indent'} = ''; | |||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | 584 | my $header; | |||||||||||||||||||||||||||||||||
| 82 | my @align; | ||||||||||||||||||||||||||||||||||
| 83 | 584 | ▶ | if (defined $param{align}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 84 | @align = is_arrayref($param{align}) | ||||||||||||||||||||||||||||||||||
| 85 | 5 | ? @{ $param{align} } | |||||||||||||||||||||||||||||||||
| 86 | 12 | ▶ | : ($param{align}) x int(@widths) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 87 | ; | ||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||
| 89 | else { | ||||||||||||||||||||||||||||||||||
| 90 | 572 | @align = ('l') x int(@widths); | |||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||
| 93 | 584 | ▶ | $header = shift @rows if $param{header_row}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | 584 | my $table = _top_border(\%param, \@widths, $char) | |||||||||||||||||||||||||||||||||
| 96 | ._header_row(\%param, $header, \@widths, \@align, $char) | ||||||||||||||||||||||||||||||||||
| 97 | ._header_rule(\%param, \@widths, $char, \@align) | ||||||||||||||||||||||||||||||||||
| 98 | ._body(\%param, \@rows, \@widths, \@align, $char) | ||||||||||||||||||||||||||||||||||
| 99 | ._bottom_border(\%param, \@widths, $char); | ||||||||||||||||||||||||||||||||||
| 100 | 584 | chop($table); | |||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | 584 | return $table; | |||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | sub _top_border | ||||||||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||||||||
| 107 | 584 | ▶ | my ($param, $widths, $char) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_top_border: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||
| 109 | 584 | ▶ | return '' if $param->{top_and_tail}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 110 | 281 | return _rule_row($param, $widths, $char->{TLC}, $char->{HR}, $char->{TT}, $char->{TRC}); | |||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||
| 113 | sub _bottom_border | ||||||||||||||||||||||||||||||||||
| 114 | { | ||||||||||||||||||||||||||||||||||
| 115 | 584 | ▶ | my ($param, $widths, $char) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_bottom_border: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||
| 117 | 584 | ▶ | return '' if $param->{top_and_tail}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 118 | 281 | return _rule_row($param, $widths, $char->{BLC}, $char->{HR}, $char->{BT}, $char->{BRC}); | |||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||
| 121 | sub _rule_row | ||||||||||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||||||||||
| 123 | 884 | ▶ | my ($param, $widths, $le, $hr, $cross, $re) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_rule_row: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||
| 124 | 884 | ▶ | my $pad = $param->{compact} ? '' : $hr; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||
| 126 | return $param->{indent} | ||||||||||||||||||||||||||||||||||
| 127 | .$le | ||||||||||||||||||||||||||||||||||
| 128 | 884 | .join($cross, map { $pad.($hr x $_).$pad } @$widths) | |||||||||||||||||||||||||||||||||
| 129 | .$re | ||||||||||||||||||||||||||||||||||
| 130 | ."\n" | ||||||||||||||||||||||||||||||||||
| 131 | ; | ||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||
| 134 | sub _header_row | ||||||||||||||||||||||||||||||||||
| 135 | { | ||||||||||||||||||||||||||||||||||
| 136 | 584 | ▶ | my ($param, $row, $widths, $align, $char) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_header_row: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 137 | 584 | ▶ | return '' unless $param->{header_row}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||
| 139 | 316 | return _text_row($param, $row, $widths, $align, $char); | |||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | sub _md_validate_data { | ||||||||||||||||||||||||||||||||||
| 143 | 302 | ▶ | my $rows = shift @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_md_validate_data: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 144 | 302 | for my $row ( @{$rows}) { | |||||||||||||||||||||||||||||||||
| 145 | 1693 | ▶ | if ("@{$row}" =~ m/[^\\]\|/ ){ | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 146 | 3 | die "Unescaped | will produce invalid Markdown!\n@{$row}"; | |||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||
| 151 | sub _md_header_rule { | ||||||||||||||||||||||||||||||||||
| 152 | 6 | ▶ | my ($param, $widthref, $alignref ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_md_header_rule: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||
| 153 | 6 | ▶ | my $coladj = $param->{'compact'} ? -2 : 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 154 | 6 | my @align = @{$alignref}; | |||||||||||||||||||||||||||||||||
| 155 | 6 | my @width = @{$widthref}; | |||||||||||||||||||||||||||||||||
| 156 | 6 | my $rule = '|'; | |||||||||||||||||||||||||||||||||
| 157 | 6 | ▶ | while ( @width) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 158 | 21 | my $colwidth = $coladj + shift( @width); | |||||||||||||||||||||||||||||||||
| 159 | 21 | my $colalign = shift( @align); | |||||||||||||||||||||||||||||||||
| 160 | 21 | my $DASHES = '-' x ($colwidth ) ; | |||||||||||||||||||||||||||||||||
| 161 | 21 | ▶ | $rule .= ":$DASHES-|" if ( $colalign eq 'l') ; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 162 | 21 | ▶ | $rule .= "-$DASHES:|" if ( $colalign eq 'r') ; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 163 | 21 | ▶ | $rule .= ":$DASHES:|" if ( $colalign eq 'c') ; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||
| 165 | 6 | return "$rule\n" ; | |||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||
| 168 | sub _header_rule | ||||||||||||||||||||||||||||||||||
| 169 | { | ||||||||||||||||||||||||||||||||||
| 170 | 584 | ▶ | my ($param, $widths, $char, $align) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_header_rule: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||
| 171 | 584 | ▶ | if ( $param->{'style'} eq 'markdown' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 172 | # the default unaligned markdown header_rule | ||||||||||||||||||||||||||||||||||
| 173 | # is similar to other styles. the aligned | ||||||||||||||||||||||||||||||||||
| 174 | # header_rule is unique. | ||||||||||||||||||||||||||||||||||
| 175 | 299 | ▶ | return _md_header_rule($param, $widths, $align) if $param->{'align'}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||
| 177 | 581 | ▶ | return '' unless $param->{header_row}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 178 | 313 | ▶ | my $fancy = $param->{separate_rows} ? 'F' : ''; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||
| 180 | 313 | return _rule_row($param, $widths, $char->{"${fancy}LT"}, $char->{"${fancy}HR"}, $char->{"${fancy}HC"}, $char->{"${fancy}RT"}); | |||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||
| 183 | sub _body | ||||||||||||||||||||||||||||||||||
| 184 | { | ||||||||||||||||||||||||||||||||||
| 185 | 584 | ▶ | my ($param, $rows, $widths, $align, $char) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_body: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 186 | 584 | ▶ | my $divider = $param->{separate_rows} ? _rule_row($param, $widths, $char->{LT}, $char->{HR}, $char->{HC}, $char->{RT}) : ''; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||
| 188 | 584 | return join($divider, map { _text_row($param, $_, $widths, $align, $char) } @$rows); | |||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||
| 191 | sub _text_row | ||||||||||||||||||||||||||||||||||
| 192 | { | ||||||||||||||||||||||||||||||||||
| 193 | 8563 | ▶ | my ($param, $row, $widths, $align, $char) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_text_row: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||
| 194 | 8563 | my @columns = @$row; | |||||||||||||||||||||||||||||||||
| 195 | 8563 | my $text = $param->{indent}.$char->{VR}; | |||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||
| 197 | 8563 | ▶ | for (my $i = 0; $i < @$widths; $i++) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 198 | 38018 | ▶ | $text .= _format_column($columns[$i] // '', $widths->[$i], $align->[$i] // 'l', $param, $char); | ||||||||||||||||||||||||||||||||
|
Condition75%3/4
$columns[$i] // ""
$align->[$i] // "l"
MC/DC100%1/1
$columns[$i] // ""
$columns[$i]
MC/DC0%0/1
$align->[$i] // "l"
$align->[$i]
Truth table100%2/2
A
A= $columns[$i]
Truth table50%1/2
A
A= $align->[$i]
| |||||||||||||||||||||||||||||||||||
| 199 | 38018 | $text .= $char->{VR}; | |||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||
| 201 | 8563 | $text .= "\n"; | |||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||
| 203 | 8563 | return $text; | |||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||
| 206 | sub _format_column | ||||||||||||||||||||||||||||||||||
| 207 | { | ||||||||||||||||||||||||||||||||||
| 208 | 38018 | ▶ | my ($text, $width, $align, $param, $char) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_format_column: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||
| 209 | 38018 | ▶ | my $pad = $param->{compact} ? '' : ' '; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||
| 211 | 38018 | ▶ | if ($align eq 'r' || $align eq 'right') { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$align eq "r" or $align eq "right"
MC/DC100%2/2
$align eq "r" or $align eq "right"
$align eq "r"
$align eq "right"
Truth table100%3/3
A or B
A= $align eq "r"B= $align eq "right"
| |||||||||||||||||||||||||||||||||||
| 212 | 50 | return $pad.' ' x ($width - tty_width($text)).$text.$pad; | |||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||
| 214 | ▶ | elsif ($align eq 'c' || $align eq 'center' || $align eq 'centre') { | |||||||||||||||||||||||||||||||||
|
Condition83%5/6
$align eq "c" or $align eq "center" or $align eq "centre"
$align eq "c" or $align eq "center"
MC/DC66%2/3
$align eq "c" or $align eq "center" or $align eq "centre"
$align eq "c"
$align eq "center"
$align eq "centre"
Truth table75%3/4
A or B or C
A= $align eq "c"B= $align eq "center"C= $align eq "centre"
| |||||||||||||||||||||||||||||||||||
| 215 | 29 | my $total_spaces = $width - tty_width($text); | |||||||||||||||||||||||||||||||||
| 216 | 29 | my $left_spaces = int($total_spaces / 2); | |||||||||||||||||||||||||||||||||
| 217 | 29 | my $right_spaces = $left_spaces; | |||||||||||||||||||||||||||||||||
| 218 | 29 | ▶ | $right_spaces++ if $total_spaces % 2 == 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 219 | 29 | return $pad.(' ' x $left_spaces).$text.(' ' x $right_spaces).$pad; | |||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||
| 221 | else { | ||||||||||||||||||||||||||||||||||
| 222 | 37939 | return $pad.$text.' ' x ($width - tty_width($text)).$pad; | |||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||
| 226 | sub _calculate_widths | ||||||||||||||||||||||||||||||||||
| 227 | { | ||||||||||||||||||||||||||||||||||
| 228 | 587 | ▶ | my $rows = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_calculate_widths: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||
| 229 | 587 | my @widths; | |||||||||||||||||||||||||||||||||
| 230 | 587 | foreach my $row (@$rows) { | |||||||||||||||||||||||||||||||||
| 231 | 8575 | my @columns = @$row; | |||||||||||||||||||||||||||||||||
| 232 | 8575 | ▶ | for (my $i = 0; $i < @columns; $i++) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 233 | 35264 | ▶ | next unless defined($columns[$i]); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||
| 235 | 35217 | my $width = tty_width($columns[$i]); | |||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||
| 237 | 35217 | ▶ | $widths[$i] = $width if !defined($widths[$i]) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $widths[$i] or $width > $widths[$i]
MC/DC100%2/2
not defined $widths[$i] or $width > $widths[$i]
not defined $widths[$i]
$width > $widths[$i]
Truth table100%3/3
not(A) or B
A= not defined $widths[$i]B= $width > $widths[$i]
| |||||||||||||||||||||||||||||||||||
| 238 | || $width > $widths[$i]; | ||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||
| 241 | 587 | return @widths; | |||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||
| 244 | # Back-compat: 'table' is an alias for 'generate_table', but isn't exported | ||||||||||||||||||||||||||||||||||
| 245 | 36 | *table = \&generate_table; | |||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||
| 247 | 36 | 1; | |||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||
| 249 | __END__ | ||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||
| 251 | =pod | ||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||
| 253 | =encoding utf8 | ||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||
| 255 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||
| 257 | Text::Table::Tiny - generate simple text tables from 2D arrays | ||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||
| 259 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||
| 261 | use Text::Table::Tiny 1.02 qw/ generate_table /; | ||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||
| 263 | my $rows = [ | ||||||||||||||||||||||||||||||||||
| 264 | [qw/ Pokemon Type Count /], | ||||||||||||||||||||||||||||||||||
| 265 | [qw/ Abra Psychic 5 /], | ||||||||||||||||||||||||||||||||||
| 266 | [qw/ Ekans Poison 123 /], | ||||||||||||||||||||||||||||||||||
| 267 | [qw/ Feraligatr Water 5678 /], | ||||||||||||||||||||||||||||||||||
| 268 | ]; | ||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||
| 270 | print generate_table(rows => $rows, header_row => 1), "\n"; | ||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||
| 272 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||
| 274 | This module provides a single function, C<generate_table>, which formats | ||||||||||||||||||||||||||||||||||
| 275 | a two-dimensional array of data as a text table. | ||||||||||||||||||||||||||||||||||
| 276 | It handles text that includes ANSI escape codes and wide Unicode characters. | ||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||
| 278 | There are a number of options for adjusting the output format, | ||||||||||||||||||||||||||||||||||
| 279 | but the intention is that the default option is good enough for most uses. | ||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||
| 281 | The example shown in the SYNOPSIS generates the following table: | ||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||
| 283 | +------------+---------+-------+ | ||||||||||||||||||||||||||||||||||
| 284 | | Pokemon | Type | Count | | ||||||||||||||||||||||||||||||||||
| 285 | +------------+---------+-------+ | ||||||||||||||||||||||||||||||||||
| 286 | | Abra | Psychic | 5 | | ||||||||||||||||||||||||||||||||||
| 287 | | Ekans | Poison | 123 | | ||||||||||||||||||||||||||||||||||
| 288 | | Feraligatr | Water | 5678 | | ||||||||||||||||||||||||||||||||||
| 289 | +------------+---------+-------+ | ||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||
| 291 | Support for wide characters was added in 1.02, | ||||||||||||||||||||||||||||||||||
| 292 | so if you need that, | ||||||||||||||||||||||||||||||||||
| 293 | you should specify that as your minimum required version, | ||||||||||||||||||||||||||||||||||
| 294 | as per the SYNOPSIS. | ||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||
| 296 | The interface changed with version 0.04, | ||||||||||||||||||||||||||||||||||
| 297 | so if you use the C<generate_table()> function illustrated above, | ||||||||||||||||||||||||||||||||||
| 298 | then you need to require at least version 0.04 of this module. | ||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||
| 300 | Some of the options described below were added in version 1.00, | ||||||||||||||||||||||||||||||||||
| 301 | so your best bet is to require at least version 1.00. | ||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||
| 304 | =head2 generate_table() | ||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||
| 306 | The C<generate_table> function understands a number of arguments, | ||||||||||||||||||||||||||||||||||
| 307 | which are passed as a hash. | ||||||||||||||||||||||||||||||||||
| 308 | The only required argument is B<rows>. | ||||||||||||||||||||||||||||||||||
| 309 | Where arguments were not supported in the original release, | ||||||||||||||||||||||||||||||||||
| 310 | the first supporting version is noted. | ||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||
| 312 | If you pass an unknown argument, | ||||||||||||||||||||||||||||||||||
| 313 | C<generate_table> will die with an error message. | ||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||
| 315 | =over 4 | ||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||
| 318 | =item * | ||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||
| 320 | rows | ||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||
| 322 | Takes an array reference which should contain one or more rows | ||||||||||||||||||||||||||||||||||
| 323 | of data, where each row is an array reference. | ||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||
| 326 | =item * | ||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||
| 328 | header_row | ||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||
| 330 | If given a true value, the first row in the data will be interpreted | ||||||||||||||||||||||||||||||||||
| 331 | as a header row, and separated from the rest of the table with a ruled line. | ||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||
| 334 | =item * | ||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||
| 336 | separate_rows | ||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||
| 338 | If given a true value, a separator line will be drawn between every row in | ||||||||||||||||||||||||||||||||||
| 339 | the table, | ||||||||||||||||||||||||||||||||||
| 340 | and a thicker line will be used for the header separator. | ||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||
| 342 | =item * | ||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||
| 344 | top_and_tail | ||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||
| 346 | If given a true value, then the top and bottom border lines will be skipped. | ||||||||||||||||||||||||||||||||||
| 347 | This reduces the vertical height of the generated table. | ||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||
| 349 | Added in 0.04. | ||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||
| 351 | =item * | ||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||
| 353 | align | ||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||
| 355 | This takes an array ref with one entry per column, | ||||||||||||||||||||||||||||||||||
| 356 | to specify the alignment of that column. | ||||||||||||||||||||||||||||||||||
| 357 | Legal values are 'l', 'c', and 'r'. | ||||||||||||||||||||||||||||||||||
| 358 | You can also specify a single alignment for all columns. | ||||||||||||||||||||||||||||||||||
| 359 | ANSI escape codes are handled. | ||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||
| 361 | Added in 1.00. | ||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||
| 363 | =item * | ||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||
| 365 | style | ||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||
| 367 | Specifies the format of the output table. | ||||||||||||||||||||||||||||||||||
| 368 | The default is C<'classic'>, | ||||||||||||||||||||||||||||||||||
| 369 | but other options are C<'boxrule'>, C<'norule'> and C<'markdown'>. | ||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||
| 371 | If you use the C<boxrule> style, | ||||||||||||||||||||||||||||||||||
| 372 | you'll probably need to run C<binmode(STDOUT, ':utf8')>. | ||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||
| 374 | Added in 1.00. | ||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||
| 377 | =item * | ||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||
| 379 | indent | ||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||
| 381 | Specify an indent that should be prefixed to every line | ||||||||||||||||||||||||||||||||||
| 382 | of the generated table. | ||||||||||||||||||||||||||||||||||
| 383 | This can either be a string of spaces, | ||||||||||||||||||||||||||||||||||
| 384 | or an integer giving the number of spaces wanted. | ||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||
| 386 | Added in 1.00. | ||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||
| 388 | =item * | ||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||
| 390 | compact | ||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||
| 392 | If set to a true value then we omit the single space padding on either | ||||||||||||||||||||||||||||||||||
| 393 | side of every column. | ||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||
| 395 | Added in 1.00. | ||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||
| 397 | =back | ||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||
| 399 | =head3 Style markdown | ||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||
| 401 | Tables are not part of the original Markdown specification but have been adopted by many extended Markdown flavours. The GitHub Flavored Markdown specification is followed, many other implementations are compatible with it. | ||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||
| 403 | The markdown style requires that header and top_and_tail be true, and indent should be none while separate_rows should be false. These requirements are all forced when style is set to 'markdown', they will be ignored if specified. | ||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||
| 405 | Compatible options are compact and align. When align is not present the delimiter row is formatted like |---|, if alignment is set that would change to |:---|,|:---:|,|---:| depending on alignment. | ||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||
| 407 | If any of the data includes the '|' character, it must be escaped as '\|'. Since this would cause invalid Markdown, the error is fatal. | ||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||
| 409 | While the GFM spec allows for ragged (not padded to be even) columns, they are not available in Text::Table::Tiny. The spec is silent on leading space (indent), but in practice it often fails to be recognized by GitHub's Markdown display, so it is treated as not allowed. In the spec leading and trailing pipes are optional. If the indent is uneven or there is too much leading whitespace the table won't be displayed correctly. Leading and trailing pipes are treated as mandatory. | ||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||
| 411 | Added in 1.03. | ||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||
| 413 | =head2 EXAMPLES | ||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||
| 415 | If you just pass the data and no other options: | ||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||
| 417 | generate_table(rows => $rows); | ||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||
| 419 | You get minimal ruling: | ||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||
| 421 | +------------+---------+-------+ | ||||||||||||||||||||||||||||||||||
| 422 | | Pokemon | Type | Count | | ||||||||||||||||||||||||||||||||||
| 423 | | Abra | Psychic | 5 | | ||||||||||||||||||||||||||||||||||
| 424 | | Ekans | Poison | 123 | | ||||||||||||||||||||||||||||||||||
| 425 | | Feraligatr | Water | 5678 | | ||||||||||||||||||||||||||||||||||
| 426 | +------------+---------+-------+ | ||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||
| 428 | If you want a separate header, set the header_row option to a true value, | ||||||||||||||||||||||||||||||||||
| 429 | as shown in the SYNOPSIS. | ||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||
| 431 | To take up fewer lines, | ||||||||||||||||||||||||||||||||||
| 432 | you can miss out the top and bottom rules, | ||||||||||||||||||||||||||||||||||
| 433 | by setting C<top_and_tail> to a true value: | ||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||
| 435 | generate_table(rows => $rows, header_row => 1, top_and_tail => 1); | ||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||
| 437 | This will generate the following: | ||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||
| 439 | | Pokemon | Type | Count | | ||||||||||||||||||||||||||||||||||
| 440 | +------------+---------+-------+ | ||||||||||||||||||||||||||||||||||
| 441 | | Abra | Psychic | 5 | | ||||||||||||||||||||||||||||||||||
| 442 | | Ekans | Poison | 123 | | ||||||||||||||||||||||||||||||||||
| 443 | | Feraligatr | Water | 5678 | | ||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||
| 445 | If you want a more stylish looking table, | ||||||||||||||||||||||||||||||||||
| 446 | set the C<style> parameter to C<'boxrule'>: | ||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||
| 448 | binmode(STDOUT,':utf8'); | ||||||||||||||||||||||||||||||||||
| 449 | generate_table(rows => $rows, header_row => 1, style => 'boxrule'); | ||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||
| 451 | This uses the ANSI box rule characters. | ||||||||||||||||||||||||||||||||||
| 452 | Note that you will need to ensure UTF output. | ||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||
| 454 | ┌────────────┬─────────┬───────┐ | ||||||||||||||||||||||||||||||||||
| 455 | │ Pokemon │ Type │ Count │ | ||||||||||||||||||||||||||||||||||
| 456 | ├────────────┼─────────┼───────┤ | ||||||||||||||||||||||||||||||||||
| 457 | │ Abra │ Psychic │ 5 │ | ||||||||||||||||||||||||||||||||||
| 458 | │ Ekans │ Poison │ 123 │ | ||||||||||||||||||||||||||||||||||
| 459 | │ Feraligatr │ Water │ 5678 │ | ||||||||||||||||||||||||||||||||||
| 460 | └────────────┴─────────┴───────┘ | ||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||
| 462 | You might want to right-align numeric values: | ||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||
| 464 | generate_table( ... , align => [qw/ l l r /] ); | ||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||
| 466 | The C<align> parameter can either take an arrayref, | ||||||||||||||||||||||||||||||||||
| 467 | or a string with an alignment to apply to all columns: | ||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||
| 469 | ┌────────────┬─────────┬───────┐ | ||||||||||||||||||||||||||||||||||
| 470 | │ Pokemon │ Type │ Count │ | ||||||||||||||||||||||||||||||||||
| 471 | ├────────────┼─────────┼───────┤ | ||||||||||||||||||||||||||||||||||
| 472 | │ Abra │ Psychic │ 5 │ | ||||||||||||||||||||||||||||||||||
| 473 | │ Ekans │ Poison │ 123 │ | ||||||||||||||||||||||||||||||||||
| 474 | │ Feraligatr │ Water │ 5678 │ | ||||||||||||||||||||||||||||||||||
| 475 | └────────────┴─────────┴───────┘ | ||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||
| 477 | If you're using the boxrule style, | ||||||||||||||||||||||||||||||||||
| 478 | you might feel you can remove the padding on either side of every column, | ||||||||||||||||||||||||||||||||||
| 479 | done by setting C<compact> to a true value: | ||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||
| 481 | ┌──────────┬───────┬─────┐ | ||||||||||||||||||||||||||||||||||
| 482 | │Pokemon │Type │Count│ | ||||||||||||||||||||||||||||||||||
| 483 | ├──────────┼───────┼─────┤ | ||||||||||||||||||||||||||||||||||
| 484 | │Abra │Psychic│ 5│ | ||||||||||||||||||||||||||||||||||
| 485 | │Ekans │Poison │ 123│ | ||||||||||||||||||||||||||||||||||
| 486 | │Feraligatr│Water │ 5678│ | ||||||||||||||||||||||||||||||||||
| 487 | └──────────┴───────┴─────┘ | ||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||
| 489 | You can also ask for a rule between each row, | ||||||||||||||||||||||||||||||||||
| 490 | in which case the header rule becomes stronger. | ||||||||||||||||||||||||||||||||||
| 491 | This works best when combined with the boxrule style: | ||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||
| 493 | generate_table( ... , separate_rows => 1 ); | ||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||
| 495 | Which results in the following: | ||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||
| 497 | ┌────────────┬─────────┬───────┐ | ||||||||||||||||||||||||||||||||||
| 498 | │ Pokemon │ Type │ Count │ | ||||||||||||||||||||||||||||||||||
| 499 | ╞════════════╪═════════╪═══════╡ | ||||||||||||||||||||||||||||||||||
| 500 | │ Abra │ Psychic │ 5 │ | ||||||||||||||||||||||||||||||||||
| 501 | ├────────────┼─────────┼───────┤ | ||||||||||||||||||||||||||||||||||
| 502 | │ Ekans │ Poison │ 123 │ | ||||||||||||||||||||||||||||||||||
| 503 | ├────────────┼─────────┼───────┤ | ||||||||||||||||||||||||||||||||||
| 504 | │ Feraligatr │ Water │ 5678 │ | ||||||||||||||||||||||||||||||||||
| 505 | └────────────┴─────────┴───────┘ | ||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||
| 507 | You can use this with the other styles, | ||||||||||||||||||||||||||||||||||
| 508 | but I'm not sure you'd want to. | ||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||
| 510 | If you just want columnar output, | ||||||||||||||||||||||||||||||||||
| 511 | use the C<norule> style: | ||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||
| 513 | generate_table( ... , style => 'norule' ); | ||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||
| 515 | which results in: | ||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||
| 518 | Pokemon Type Count | ||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||
| 520 | Abra Psychic 5 | ||||||||||||||||||||||||||||||||||
| 521 | Ekans Poison 123 | ||||||||||||||||||||||||||||||||||
| 522 | Feraligatr Water 5678 | ||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||
| 525 | Note that everywhere you saw a line on the previous tables, | ||||||||||||||||||||||||||||||||||
| 526 | there will be a space character in this version. | ||||||||||||||||||||||||||||||||||
| 527 | So you may want to combine the C<top_and_tail> option, | ||||||||||||||||||||||||||||||||||
| 528 | to suppress the extra blank lines before and after | ||||||||||||||||||||||||||||||||||
| 529 | the body of the table. | ||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||
| 531 | To get a basic Markdown table: | ||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||
| 533 | generate_table( rows => $rows, style => 'markdown' ); | ||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||
| 535 | To get something like: | ||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||
| 537 | | Pokemon | Type | Seen | | ||||||||||||||||||||||||||||||||||
| 538 | |-----------|----------------|-------| | ||||||||||||||||||||||||||||||||||
| 539 | | Rattata | Normal | 10199 | | ||||||||||||||||||||||||||||||||||
| 540 | | Ekans | Poison | 536 | | ||||||||||||||||||||||||||||||||||
| 541 | | Vileplume | Grass / Poison | 4 | | ||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||
| 543 | If you want to specify alignment you can. Remember, if you need to use '|' in any of your data it must be escaped as '\|', and it will appear in the output that way. Failure to do so will result in invalid Markdown. If you change the Type for Vileplume to 'Grass\|Poison' and then: | ||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||
| 545 | generate_table( | ||||||||||||||||||||||||||||||||||
| 546 | rows => $rows, | ||||||||||||||||||||||||||||||||||
| 547 | style => 'markdown', | ||||||||||||||||||||||||||||||||||
| 548 | align => [ 'l','c','r' ] ); | ||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||
| 550 | You'll get: | ||||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||||
| 552 | | Pokemon | Type | Seen | | ||||||||||||||||||||||||||||||||||
| 553 | |:----------|:-------------:|------:| | ||||||||||||||||||||||||||||||||||
| 554 | | Rattata | Normal | 10199 | | ||||||||||||||||||||||||||||||||||
| 555 | | Ekans | Poison | 536 | | ||||||||||||||||||||||||||||||||||
| 556 | | Vileplume | Grass\|Poison | 4 | | ||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||
| 558 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||
| 560 | My L<blog post|http://neilb.org/2019/08/06/text-table-tiny-changes.html> | ||||||||||||||||||||||||||||||||||
| 561 | where I described changes to formatting; | ||||||||||||||||||||||||||||||||||
| 562 | this has more examples. | ||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||
| 564 | There are many modules for formatting text tables on CPAN. | ||||||||||||||||||||||||||||||||||
| 565 | A good number of them are listed in the | ||||||||||||||||||||||||||||||||||
| 566 | L<See Also|https://metacpan.org/pod/Text::Table::Manifold#See-Also> | ||||||||||||||||||||||||||||||||||
| 567 | section of the documentation for L<Text::Table::Manifold>. | ||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||
| 569 | The specification for tables in GitHub Flavored Markdown is at: L<https://github.github.com/gfm/#tables-extension-> | ||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||
| 573 | =head1 REPOSITORY | ||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||
| 575 | L<https://github.com/neilb/Text-Table-Tiny> | ||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||
| 578 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||
| 580 | Neil Bowers <neilb@cpan.org> | ||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||
| 582 | The original version was written by Creighton Higgins <chiggins@chiggins.com>, | ||||||||||||||||||||||||||||||||||
| 583 | but the module was entirely rewritten for 0.05_01. | ||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||
| 586 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||
| 588 | This software is copyright (c) 2020 by Neil Bowers. | ||||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||||
| 590 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||
| 591 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||
| 593 | =cut | ||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||
| 595 | #FOOTER | ||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||
| 597 | =pod | ||||||||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||||||||
| 599 | BUG TRACKER | ||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||
| 601 | L<https://github.com/brainbuz/Vote-Count/issues> | ||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||
| 603 | AUTHOR | ||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||
| 605 | John Karr (BRAINBUZ) brainbuz@cpan.org | ||||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||||
| 607 | CONTRIBUTORS | ||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||
| 609 | Copyright 2019-2020 by John Karr (BRAINBUZ) brainbuz@cpan.org. | ||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||
| 611 | LICENSE | ||||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||
| 613 | This module is released under the GNU Public License Version 3. See license file for details. For more information on this license visit L<http://fsf.org>. | ||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||
| 615 | =cut | ||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||