Text/Table/TinyBorderStyle.pm

filter: Statementstmt 23.1% 25 / 108 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 37.5% 6 / 16 Podpod 0.0% 0 / 1 totaltotal 18.2% 31 / 170 CC 20 SCAR 44.7
CC
20
Cov
17%
generate_table
210.0
_maxwidths
12.0
_get_top_border
12.0
CRAP 87.6
↑ Summary auto/share/dist/Text-Table-TinyBorderStyle/examples/demo1.pl »
1 package Text::Table::TinyBorderStyle;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12 1 our $DATE = '2022-02-14'; # DATE
13 1 our $DIST = 'Text-Table-TinyBorderStyle'; # DIST
14 1 our $VERSION = '0.005'; # VERSION
15  
16 1 our @EXPORT_OK = qw/ generate_table /;
17  
18 0 ▶sub generate_table {
Subroutine generate_table: not called CC=14 SCAR=53.5
Pod undocumented
19  
20 0 my %params = @_;
21 0 ▶ my $rows = $params{rows} or die "Must provide rows!";
Branch: 1 branch
BranchTrueFalseTotal
my $rows = $params{"rows"} or die "Must provide rows!" 0 0 0
22  
23 my $border_style_name = $params{border_style} ||
24 $ENV{TEXT_TABLE_TINY_BORDER_STYLE} ||
25 $ENV{BORDER_STYLE} ||
26 0 ▶ 'BorderStyle::ASCII::SingleLine';
Condition0%0/8
$params{'border_style'} || $ENV{'TEXT_TABLE_TINY_BORDER_STYLE'} || $ENV{'BORDER_STYLE'} || 'BorderStyle::ASCII::SingleLine'
l!l
00
$params{"border_style"} or $ENV{"TEXT_TABLE_TINY_BORDER_STYLE"} or $ENV{"BORDER_STYLE"}
l!l&&r!l&&!r
000
$params{"border_style"} or $ENV{"TEXT_TABLE_TINY_BORDER_STYLE"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$params{'border_style'} || $ENV{'TEXT_TABLE_TINY_BORDER_STYLE'} || $ENV{'BORDER_STYLE'} || 'BorderStyle::ASCII::SingleLine'
$params{"border_style"} $ENV{"TEXT_TABLE_TINY_BORDER_STYLE"} $ENV{"BORDER_STYLE"}
Truth table0%0/4
A or B or C
A= $params{"border_style"}B= $ENV{"TEXT_TABLE_TINY_BORDER_STYLE"}C= $ENV{"BORDER_STYLE"}
ABCresult
1XX1
01X1
0011
0000
27  
28 0 require Module::Load::Util;
29 0 my $border_style_obj = Module::Load::Util::instantiate_class_with_optional_args(
30 {ns_prefixes=>["BorderStyle", ""]}, $border_style_name);
31  
32 # foreach col, get the biggest width
33 0 my $widths = _maxwidths($rows);
34 0 my $max_index = _max_array_index($rows);
35  
36 0 ▶ my $table_has_header_row = $params{header_row} && @$rows;
Condition0%0/3
$params{'header_row'} && @$rows
!ll&&!rl&&r
000
MC/DC0%0/2
$params{'header_row'} && @$rows
$params{'header_row'} @$rows
Truth table0%0/3
A && B
A= $params{'header_row'}B= @$rows
ABresult
0X0
100
111
37 0 ▶ my $table_has_data_row = @$rows >= ($params{header_row} ? 2:1);
Branch: 1 branch
BranchTrueFalseTotal
$params{'header_row'} ? : 0 0 0
38  
39 # use that to get the field format and separators
40 0 my $row_format = _get_row_format ($border_style_obj, $widths);
41 0 my $header_row_format = _get_header_row_format ($border_style_obj, $widths);
42 0 my $top_border = _get_top_border ($border_style_obj, $widths, $table_has_header_row, $table_has_data_row);
43 0 my $head_row_sep = _get_header_row_separator($border_style_obj, $widths);
44 0 my $row_sep = _get_row_separator ($border_style_obj, $widths);
45 0 my $bottom_border = _get_bottom_border ($border_style_obj, $widths, $table_has_header_row, $table_has_data_row);
46  
47 # here we go...
48 0 my @table;
49 0 push @table, $top_border;
50  
51 # if the first row's a header:
52 0 my $data_begins = 0;
53 {
54 0 ▶ last unless $params{header_row};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"header_row"} 0 0 0
55 0 my $header_row = $rows->[0];
56 0 ▶ last unless $header_row;
Branch: 1 branch
BranchTrueFalseTotal
unless $header_row 0 0 0
57 0 $data_begins++;
58 push @table, sprintf(
59 $header_row_format,
60 0 ▶ map { defined($header_row->[$_]) ? $header_row->[$_] : '' } (0..$max_index)
Branch: 1 branch
BranchTrueFalseTotal
defined $header_row->[$_] ? : 0 0 0
61 );
62 0 push @table, $head_row_sep;
63 }
64  
65 # then the data
66 0 my $i = 0;
67 0 foreach my $row ( @{ $rows }[$data_begins..$#$rows] ) {
68 0 ▶ push @table, $row_sep if $params{separate_rows} && $i++;
Branch: 1 branch
BranchTrueFalseTotal
if $params{"separate_rows"} and $i++ 0 0 0
Condition0%0/3
$params{"separate_rows"} and $i++
!ll&&!rl&&r
000
MC/DC0%0/2
$params{"separate_rows"} and $i++
$params{"separate_rows"} $i++
Truth table0%0/3
A and B
A= $params{"separate_rows"}B= $i++
ABresult
0X0
100
111
69 push @table, sprintf(
70 $row_format,
71 0 ▶ map { defined($row->[$_]) ? $row->[$_] : '' } (0..$max_index)
Branch: 1 branch
BranchTrueFalseTotal
defined $row->[$_] ? : 0 0 0
72 );
73 }
74  
75 # this will have already done the bottom if called explicitly
76 0 push @table, $bottom_border;
77 0 return join("\n",grep {$_} @table);
78 }
79  
80 0 ▶sub _get_cols_and_rows ($) { ## no critic: Subroutines::ProhibitSubroutinePrototypes
Subroutine _get_cols_and_rows: not called CC=1 SCAR=6.9
81 0 my $rows = shift;
82 0 return ( List::Util::max( map { scalar @$_ } @$rows), scalar @$rows);
83 }
84  
85 0 ▶sub _maxwidths {
Subroutine _maxwidths: not called CC=3 SCAR=24.8
86 0 my $rows = shift;
87 # what's the longest array in this list of arrays?
88 0 my $max_index = _max_array_index($rows);
89 0 my $widths = [];
90 0 for my $i (0..$max_index) {
91 # go through the $i-th element of each array, find the longest
92 0 ▶ my $max = List::Util::max(map {defined $$_[$i] ? length($$_[$i]) : 0} @$rows);
Branch: 1 branch
BranchTrueFalseTotal
defined $_->[$i] ? : 0 0 0
93 0 push @$widths, $max;
94 }
95 0 return $widths;
96 }
97  
98 # return highest top-index from all rows in case they're different lengths
99 0 ▶sub _max_array_index {
Subroutine _max_array_index: not called CC=1 SCAR=6.9
100 0 my $rows = shift;
101 0 return List::Util::max( map { $#$_ } @$rows );
102 }
103  
104 # TODO: what if border character contains %
105 0 ▶sub _get_row_format {
Subroutine _get_row_format: not called CC=1 SCAR=6.9
106 0 my ($border_style_obj, $widths, $table_has_header_row, $table_has_data_row) = @_;
107 0 my %gbcargs = (for_data_row=>1);
108 join(
109 "",
110 $border_style_obj->get_border_char(char=>'v_l', %gbcargs),
111 " ",
112 join(
113 " ".
114 $border_style_obj->get_border_char(char=>'v_i', %gbcargs).
115 " ",
116 0 map { "%-${_}s" } @$widths),
117 " ",
118 $border_style_obj->get_border_char(char=>'v_r', %gbcargs),
119 );
120 }
121  
122 # TODO: what if border character contains %
123 0 ▶sub _get_header_row_format {
Subroutine _get_header_row_format: not called CC=1 SCAR=6.9
124 0 my ($border_style_obj, $widths) = @_;
125 0 my %gbcargs = (for_header_row=>1);
126 join(
127 "",
128 $border_style_obj->get_border_char(char=>'v_l', %gbcargs),
129 " ",
130 join(
131 " ".
132 $border_style_obj->get_border_char(char=>'v_i', %gbcargs).
133 " ",
134 0 map { "%-${_}s" } @$widths),
135 " ",
136 $border_style_obj->get_border_char(char=>'v_r', %gbcargs),
137 );
138 }
139  
140 0 ▶sub _get_top_border {
Subroutine _get_top_border: not called CC=3 SCAR=24.8
141 0 my ($border_style_obj, $widths, $table_has_header_row, $table_has_data_row) = @_;
142 0 my %gbcargs = ();
143 0 ▶ if ($table_has_header_row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table_has_header_row) { } 0 0 0
144 0 $gbcargs{for_header_row} = 1;
145 } else {
146 0 $gbcargs{for_data_row} = 1;
147 }
148  
149 # assume there's no top border when the rd_t character is empty
150 0 my $rd_t = $border_style_obj->get_border_char(char=>'rd_t', %gbcargs);
151 0 ▶ return '' unless length $rd_t;
Branch: 1 branch
BranchTrueFalseTotal
unless length $rd_t 0 0 0
152  
153 join(
154 "",
155 $rd_t,
156 $border_style_obj->get_border_char(char=>'h_t', %gbcargs),
157 join(
158 $border_style_obj->get_border_char(char=>'h_t', %gbcargs).
159 $border_style_obj->get_border_char(char=>'hd_t', %gbcargs).
160 $border_style_obj->get_border_char(char=>'h_t', %gbcargs),
161 0 map { sprintf("%-${_}s", $border_style_obj->get_border_char(char=>'h_t', repeat=>$_, %gbcargs)) } @$widths),
162 $border_style_obj->get_border_char(char=>'h_t', %gbcargs),
163 $border_style_obj->get_border_char(char=>'ld_t', %gbcargs),
164 );
165 }
166  
167 0 ▶sub _get_header_row_separator {
Subroutine _get_header_row_separator: not called CC=1 SCAR=6.9
168 0 my ($border_style_obj, $widths) = @_;
169 0 my %gbcargs = (for_header_data_separator=>1);
170 join(
171 "",
172 $border_style_obj->get_border_char(char=>'rv_l', %gbcargs),
173 $border_style_obj->get_border_char(char=>'h_i', %gbcargs),
174 join(
175 $border_style_obj->get_border_char(char=>'h_i', %gbcargs).
176 $border_style_obj->get_border_char(char=>'hv_i', %gbcargs).
177 $border_style_obj->get_border_char(char=>'h_i', %gbcargs),
178 0 map { sprintf("%-${_}s", $border_style_obj->get_border_char(char=>'h_i', repeat=>$_, %gbcargs)) } @$widths),
179 $border_style_obj->get_border_char(char=>'h_i', %gbcargs),
180 $border_style_obj->get_border_char(char=>'lv_r', %gbcargs),
181 );
182 }
183  
184 0 ▶sub _get_row_separator {
Subroutine _get_row_separator: not called CC=1 SCAR=6.9
185 0 my ($border_style_obj, $widths) = @_;
186 0 my %gbcargs = (for_data_row=>1, for_data_data_separator=>1);
187 join(
188 "",
189 $border_style_obj->get_border_char(char=>'rv_l', %gbcargs),
190 $border_style_obj->get_border_char(char=>'h_i', %gbcargs),
191 join(
192 $border_style_obj->get_border_char(char=>'h_i', %gbcargs).
193 $border_style_obj->get_border_char(char=>'hv_i', %gbcargs).
194 $border_style_obj->get_border_char(char=>'h_i', %gbcargs),
195 0 map { sprintf("%-${_}s", $border_style_obj->get_border_char(char=>'h_i', repeat=>$_, %gbcargs)) } @$widths),
196 $border_style_obj->get_border_char(char=>'h_i', %gbcargs),
197 $border_style_obj->get_border_char(char=>'lv_r', %gbcargs),
198 );
199 }
200  
201 0 ▶sub _get_bottom_border {
Subroutine _get_bottom_border: not called CC=3 SCAR=24.8
202 0 my ($border_style_obj, $widths, $table_has_header_row, $table_has_data_row) = @_;
203 0 my %gbcargs = ();
204 0 ▶ if (!$table_has_data_row) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $table_has_data_row) { } 0 0 0
205 0 $gbcargs{for_header_row} = 1;
206 } else {
207 0 $gbcargs{for_data_row} = 1;
208 }
209  
210 0 my $ru_b = $border_style_obj->get_border_char(char=>'ru_b', %gbcargs);
211 # assume we don't have bottom border when ru_b character is empty
212 0 ▶ return '' unless length $ru_b;
Branch: 1 branch
BranchTrueFalseTotal
unless length $ru_b 0 0 0
213  
214 join(
215 "",
216 $ru_b,
217 $border_style_obj->get_border_char(char=>'h_b', %gbcargs),
218 join(
219 $border_style_obj->get_border_char(char=>'h_b', %gbcargs).
220 $border_style_obj->get_border_char(char=>'hu_b', %gbcargs).
221 $border_style_obj->get_border_char(char=>'h_b', %gbcargs),
222 0 map { sprintf("%-${_}s", $border_style_obj->get_border_char(char=>'h_b', repeat=>$_, %gbcargs)) } @$widths),
223 $border_style_obj->get_border_char(char=>'h_b', %gbcargs),
224 $border_style_obj->get_border_char(char=>'lu_b', %gbcargs),
225 );
226 }
227  
228 # Back-compat: 'table' is an alias for 'generate_table', but isn't exported
229 {
230 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
231 1 *table = \&generate_table;
232 }
233  
234 1 1;
235 # ABSTRACT: Text::Table::Tiny + support for border styles
236  
237 __END__
↑ Summary auto/share/dist/Text-Table-TinyBorderStyle/examples/demo1.pl »