Count/TextTableTiny.pm

filter: Statementstmt 100% 138 / 138 Branchbran 96.8% 62 / 64 Conditioncond 90.0% 18 / 20 MC/DCmcdc 81.8% 9 / 11 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 1 totaltotal 97.2% 247 / 254 CC 44 SCAR 18.1
CC
44
Cov
98%
generate_table
13.0
_format_column
8.0
_md_header_rule
6.0
CRAP 6.1
« Count/Method/CondorcetVsIRV.pm ↑ Summary Count/Helper/FullCascadeCharge.pm »
1 package Vote::Count::TextTableTiny;
2 50 $Vote::Count::TextTableTiny::VERSION = '2.03';
3 50 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 50 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 50 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 50 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 50 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 50 ▶use Ref::Util 0.202 qw/ is_arrayref /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 50 ▶use String::TtyLength 0.02 qw/ tty_width /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 50 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 50 our $COLUMN_SEPARATOR = '|';
18 50 our $ROW_SEPARATOR = '-';
19 50 our $CORNER_MARKER = '+';
20 50 our $HEADER_ROW_SEPARATOR = '=';
21 50 our $HEADER_CORNER_MARKER = 'O';
22  
23 50 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 50 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 718 ▶ my %param = @_;
Subroutine generate_table: called CC=13 SCAR=25.7
Pod undocumented
49  
50 718 foreach my $arg (keys %param) {
51 1228 ▶ croak "unknown argument '$arg'" if not exists $arguments{$arg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arguments{$arg} 1 1227 1228
52 }
53  
54 717 ▶ my $rows = $param{rows} or croak "you must pass the 'rows' argument!";
Branch: 1 branch
BranchTrueFalseTotal
my $rows = $param{"rows"} or croak("you must pass the 'rows' argument!") 1 716 717
55 716 my @rows = @$rows;
56 716 my @widths = _calculate_widths($rows);
57  
58 716 ▶ $param{style} //= 'classic';
Condition100%2/2
$param{"style"} //= "classic"
l!l
412304
MC/DC100%1/1
$param{"style"} //= "classic"
$param{"style"}
Truth table100%2/2
A
A= $param{"style"}
Aresult
11
00
59  
60 716 ▶ $param{indent} //= '';
Condition100%2/2
$param{"indent"} //= ""
l!l
1715
MC/DC100%1/1
$param{"indent"} //= ""
$param{"indent"}
Truth table100%2/2
A
A= $param{"indent"}
Aresult
11
00
61 716 ▶ $param{indent} = ' ' x $param{indent} if $param{indent} =~ /^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $param{"indent"} =~ /^[0-9]+$/u 1 715 716
62  
63 716 my $style = $param{style};
64 716 ▶ croak "unknown style '$style'" if not exists($charsets{ $style });
Branch: 1 branch
BranchTrueFalseTotal
unless exists $charsets{$style} 0 716 716
65 716 my $char = $charsets{$style};
66  
67 716 ▶ if ($style eq 'classic') {
Branch: 2 branches
BranchTrueFalseTotal
if ($style eq 'classic') { } 309 407 716
elsif ($style eq 'markdown') { } 403 4 407
68 309 $char->{TLC} = $char->{TRC} = $char->{TT} = $char->{LT} = $char->{RT} = $char->{HC} = $char->{BLC} = $char->{BT} = $char->{BRC} = $CORNER_MARKER;
69 309 $char->{HR} = $ROW_SEPARATOR;
70 309 $char->{VR} = $COLUMN_SEPARATOR;
71 309 $char->{FLT} = $char->{FRT} = $char->{FHC} = $HEADER_CORNER_MARKER;
72 309 $char->{FHR} = $HEADER_ROW_SEPARATOR;
73 } elsif ( $style eq 'markdown') {
74 403 _md_validate_data( $rows );
75 400 $param{'header_row'} = 1;
76 400 $param{'top_and_tail'} = 1;
77 400 $param{'separate_rows'} = 0;
78 400 $param{'indent'} = '';
79 }
80  
81 713 my $header;
82 my @align;
83 713 ▶ if (defined $param{align}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{'align'}) { } 13 700 713
84 @align = is_arrayref($param{align})
85 6 ? @{ $param{align} }
86 13 ▶ : ($param{align}) x int(@widths)
Branch: 1 branch
BranchTrueFalseTotal
is_arrayref($param{'align'}) ? : 6 7 13
87 ;
88 }
89 else {
90 700 @align = ('l') x int(@widths);
91 }
92  
93 713 ▶ $header = shift @rows if $param{header_row};
Branch: 1 branch
BranchTrueFalseTotal
if $param{"header_row"} 417 296 713
94  
95 713 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 713 chop($table);
101  
102 713 return $table;
103 }
104  
105 sub _top_border
106 {
107 713 ▶ my ($param, $widths, $char) = @_;
Subroutine _top_border: called CC=2 SCAR=6.9
108  
109 713 ▶ return '' if $param->{top_and_tail};
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"top_and_tail"} 404 309 713
110 309 return _rule_row($param, $widths, $char->{TLC}, $char->{HR}, $char->{TT}, $char->{TRC});
111 }
112  
113 sub _bottom_border
114 {
115 713 ▶ my ($param, $widths, $char) = @_;
Subroutine _bottom_border: called CC=2 SCAR=6.9
116  
117 713 ▶ return '' if $param->{top_and_tail};
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"top_and_tail"} 404 309 713
118 309 return _rule_row($param, $widths, $char->{BLC}, $char->{HR}, $char->{BT}, $char->{BRC});
119 }
120  
121 sub _rule_row
122 {
123 1040 ▶ my ($param, $widths, $le, $hr, $cross, $re) = @_;
Subroutine _rule_row: called CC=2 SCAR=7.0
124 1040 ▶ my $pad = $param->{compact} ? '' : $hr;
Branch: 1 branch
BranchTrueFalseTotal
$param->{'compact'} ? : 0 1040 1040
125  
126 return $param->{indent}
127 .$le
128 1040 .join($cross, map { $pad.($hr x $_).$pad } @$widths)
129 .$re
130 ."\n"
131 ;
132 }
133  
134 sub _header_row
135 {
136 713 ▶ my ($param, $row, $widths, $align, $char) = @_;
Subroutine _header_row: called CC=2 SCAR=6.9
137 713 ▶ return '' unless $param->{header_row};
Branch: 1 branch
BranchTrueFalseTotal
unless $param->{"header_row"} 296 417 713
138  
139 417 return _text_row($param, $row, $widths, $align, $char);
140 }
141  
142 sub _md_validate_data {
143 403 ▶ my $rows = shift @_;
Subroutine _md_validate_data: called CC=3 SCAR=11.0
144 403 for my $row ( @{$rows}) {
145 2170 ▶ if ("@{$row}" =~ m/[^\\]\|/ ){
Branch: 1 branch
BranchTrueFalseTotal
if (join($", @{$row;}) =~ /[^\\]\|/u) 3 2167 2170
146 3 die "Unescaped | will produce invalid Markdown!\n@{$row}";
147 }
148 }
149 }
150  
151 sub _md_header_rule {
152 7 ▶ my ($param, $widthref, $alignref ) = @_;
Subroutine _md_header_rule: called CC=6 SCAR=17.9
153 7 ▶ my $coladj = $param->{'compact'} ? -2 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$param->{'compact'} ? : 2 5 7
154 7 my @align = @{$alignref};
155 7 my @width = @{$widthref};
156 7 my $rule = '|';
157 7 ▶ while ( @width) {
Branch: 1 branch
BranchTrueFalseTotal
if (@width) 27 7 34
158 27 my $colwidth = $coladj + shift( @width);
159 27 my $colalign = shift( @align);
160 27 my $DASHES = '-' x ($colwidth ) ;
161 27 ▶ $rule .= ":$DASHES-|" if ( $colalign eq 'l') ;
Branch: 1 branch
BranchTrueFalseTotal
if $colalign eq "l" 9 18 27
162 27 ▶ $rule .= "-$DASHES:|" if ( $colalign eq 'r') ;
Branch: 1 branch
BranchTrueFalseTotal
if $colalign eq "r" 12 15 27
163 27 ▶ $rule .= ":$DASHES:|" if ( $colalign eq 'c') ;
Branch: 1 branch
BranchTrueFalseTotal
if $colalign eq "c" 6 21 27
164 }
165 7 return "$rule\n" ;
166 }
167  
168 sub _header_rule
169 {
170 713 ▶ my ($param, $widths, $char, $align) = @_;
Subroutine _header_rule: called CC=5 SCAR=16.1
171 713 ▶ if ( $param->{'style'} eq 'markdown' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"style"} eq "markdown") 400 313 713
172 # the default unaligned markdown header_rule
173 # is similar to other styles. the aligned
174 # header_rule is unique.
175 400 ▶ return _md_header_rule($param, $widths, $align) if $param->{'align'};
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"align"} 4 396 400
176 }
177 709 ▶ return '' unless $param->{header_row};
Branch: 1 branch
BranchTrueFalseTotal
unless $param->{"header_row"} 296 413 709
178 413 ▶ my $fancy = $param->{separate_rows} ? 'F' : '';
Branch: 1 branch
BranchTrueFalseTotal
$param->{'separate_rows'} ? : 6 407 413
179  
180 413 return _rule_row($param, $widths, $char->{"${fancy}LT"}, $char->{"${fancy}HR"}, $char->{"${fancy}HC"}, $char->{"${fancy}RT"});
181 }
182  
183 sub _body
184 {
185 713 ▶ my ($param, $rows, $widths, $align, $char) = @_;
Subroutine _body: called CC=2 SCAR=6.9
186 713 ▶ my $divider = $param->{separate_rows} ? _rule_row($param, $widths, $char->{LT}, $char->{HR}, $char->{HC}, $char->{RT}) : '';
Branch: 1 branch
BranchTrueFalseTotal
$param->{'separate_rows'} ? : 9 704 713
187  
188 713 return join($divider, map { _text_row($param, $_, $widths, $align, $char) } @$rows);
189 }
190  
191 sub _text_row
192 {
193 10606 ▶ my ($param, $row, $widths, $align, $char) = @_;
Subroutine _text_row: called CC=4 SCAR=13.9
194 10606 my @columns = @$row;
195 10606 my $text = $param->{indent}.$char->{VR};
196  
197 10606 ▶ for (my $i = 0; $i < @$widths; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$widths) 47121 10606 57727
198 47121 ▶ $text .= _format_column($columns[$i] // '', $widths->[$i], $align->[$i] // 'l', $param, $char);
Condition75%3/4
$columns[$i] // ""
l!l
437843337
$align->[$i] // "l"
l!l
471210
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]
Aresult
11
00
Truth table50%1/2
A
A= $align->[$i]
Aresult
11
00
199 47121 $text .= $char->{VR};
200 }
201 10606 $text .= "\n";
202  
203 10606 return $text;
204 }
205  
206 sub _format_column
207 {
208 47121 ▶ my ($text, $width, $align, $param, $char) = @_;
Subroutine _format_column: called CC=8 SCAR=20.8
209 47121 ▶ my $pad = $param->{compact} ? '' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$param->{'compact'} ? : 15 47106 47121
210  
211 47121 ▶ if ($align eq 'r' || $align eq 'right') {
Branch: 2 branches
BranchTrueFalseTotal
if ($align eq 'r' or $align eq 'right') { } 86 47035 47121
elsif ($align eq 'c' or $align eq 'center' or $align eq 'centre') { } 29 47006 47035
Condition100%3/3
$align eq "r" or $align eq "right"
l!l&&r!l&&!r
82447035
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"
ABresult
1X1
011
000
212 86 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"
l!l&&r!l&&!r
29047006
$align eq "c" or $align eq "center"
l!l&&r!l&&!r
131647006
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"
ABCresult
1XX1
01X1
0011
0000
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
BranchTrueFalseTotal
if $total_spaces % 2 == 1 1 28 29
219 29 return $pad.(' ' x $left_spaces).$text.(' ' x $right_spaces).$pad;
220 }
221 else {
222 47006 return $pad.$text.' ' x ($width - tty_width($text)).$pad;
223 }
224 }
225  
226 sub _calculate_widths
227 {
228 716 ▶ my $rows = shift;
Subroutine _calculate_widths: called CC=6 SCAR=17.9
229 716 my @widths;
230 716 foreach my $row (@$rows) {
231 10618 my @columns = @$row;
232 10618 ▶ for (my $i = 0; $i < @columns; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @columns) 43867 10618 54485
233 43867 ▶ next unless defined($columns[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $columns[$i] 47 43820 43867
234  
235 43820 my $width = tty_width($columns[$i]);
236  
237 43820 ▶ $widths[$i] = $width if !defined($widths[$i])
Branch: 1 branch
BranchTrueFalseTotal
if not defined $widths[$i] or $width > $widths[$i] 4376 39444 43820
Condition100%3/3
not defined $widths[$i] or $width > $widths[$i]
l!l&&r!l&&!r
2445193139444
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]
ABresult
1X1
011
000
238 || $width > $widths[$i];
239 }
240 }
241 716 return @widths;
242 }
243  
244 # Back-compat: 'table' is an alias for 'generate_table', but isn't exported
245 50 *table = \&generate_table;
246  
247 50 1;
248  
249 __END__
250  
251 =pod
252  
253 =encoding utf8
254  
255 =head1 NAME
256  
257 Vote::Count::TextTableTiny
258  
259 =head1 SYNOPSIS
260  
261 Don't use this module. It is a fork from a pending Pull Request, and will be withdrawn when the PR merges.
262  
263 =head1 REPOSITORY
264  
265 L<https://github.com/neilb/Text-Table-Tiny>
266  
267 =head1 AUTHOR
268  
269 Neil Bowers <neilb@cpan.org>
270  
271 The original version was written by Creighton Higgins <chiggins@chiggins.com>,
272 but the module was entirely rewritten for 0.05_01.
273  
274 =head1 COPYRIGHT AND LICENSE
275  
276 This software is copyright (c) 2020 by Neil Bowers.
277  
278 This is free software; you can redistribute it and/or modify it under
279 the same terms as the Perl 5 programming language system itself.
280  
281 =cut
282  
283 #FOOTER
284  
285 =pod
286  
287 BUG TRACKER
288  
289 L<https://github.com/brainbuz/Vote-Count/issues>
290  
291 AUTHOR
292  
293 John Karr (BRAINBUZ) brainbuz@cpan.org
294  
295 CONTRIBUTORS
296  
297 Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
298  
299 LICENSE
300  
301 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>.
302  
303 SUPPORT
304  
305 This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
306  
307 =cut
308  
« Count/Method/CondorcetVsIRV.pm ↑ Summary Count/Helper/FullCascadeCharge.pm »