Console.pm

filter: Statementstmt 88.6% 266 / 300 Branchbran 72.6% 106 / 146 Conditioncond 46.9% 23 / 49 MC/DCmcdc 20.6% 6 / 29 Subroutinesub 96.1% 25 / 26 Podpod 33.3% 1 / 3 totaltotal 77.2% 427 / 553 CC 118 SCAR 30.2
CC
118
Cov
80%
_render_table
42.1
_format_list
33.5
_detect_struct
26.3
CRAP 20.6
↑ Summary SimpleText.pm »
1 package Data::Format::Pretty::Console;
2  
3 4 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use experimental 'smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Text::ANSITable;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use YAML::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
15 4 our $DATE = '2023-08-07'; # DATE
16 4 our $DIST = 'Data-Format-Pretty-Console'; # DIST
17 4 our $VERSION = '0.392'; # VERSION
18  
19 4 my $json = JSON::MaybeXS->new->allow_nonref;
20  
21 4 require Exporter;
22 4 our @ISA = qw(Exporter);
23 4 our @EXPORT_OK = qw(format_pretty);
24  
25 0 ▶sub content_type { "text/plain" }
Subroutine content_type: not called CC=1 SCAR=6.9
Pod undocumented
26  
27 23 ▶sub format_pretty {
Subroutine format_pretty: called CC=2 SCAR=7.1
Pod documented
28 23 my ($data, $opts) = @_;
29 23 ▶ $opts //= {};
Condition50%1/2
$opts //= {}
l!l
230
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
30 23 __PACKAGE__->new($opts)->_format($data);
31 }
32  
33 # OO interface is nto documented, we use it just to subclass
34 # Data::Format::Pretty::HTML
35 44 ▶sub new {
Subroutine new: called CC=11 SCAR=32.6
Pod undocumented
36 44 my ($class, $opts) = @_;
37 44 ▶ $opts //= {};
Condition100%2/2
$opts //= {}
l!l
2321
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
38 44 ▶ $opts->{interactive} //= $ENV{INTERACTIVE} // (-t STDOUT); ## no critic: InputOutput::ProhibitInteractiveTest
Condition50%3/6
$opts->{"interactive"} //= $ENV{"INTERACTIVE"} // -t STDOUT
l!l&&r!l&&!r
23210
$ENV{"INTERACTIVE"} // -t STDOUT
l!l&&r!l&&!r
0210
MC/DC0%0/3
$opts->{"interactive"} //= $ENV{"INTERACTIVE"} // -t STDOUT
$opts->{"interactive"} $ENV{"INTERACTIVE"} -t STDOUT
Truth table0%0/4
A //= B // C
A= $opts->{"interactive"}B= $ENV{"INTERACTIVE"}C= -t STDOUT
ABCresult
1XX1
01X1
0011
0000
39 $opts->{table_column_orders} //= $json->decode(
40 $ENV{FORMAT_PRETTY_TABLE_COLUMN_ORDERS})
41 44 ▶ if defined($ENV{FORMAT_PRETTY_TABLE_COLUMN_ORDERS});
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"FORMAT_PRETTY_TABLE_COLUMN_ORDERS"} 0 44 44
Condition0%0/3
$opts->{"table_column_orders"} //= $json->decode($ENV{"FORMAT_PRETTY_TABLE_COLUMN_ORDERS"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"table_column_orders"} //= $json->decode($ENV{"FORMAT_PRETTY_TABLE_COLUMN_ORDERS"})
$opts->{"table_column_orders"} $json->decode($ENV{"FORMAT_PRETTY_TABLE_COLUMN_ORDERS"})
Truth table0%0/3
A //= B
A= $opts->{"table_column_orders"}B= $json->decode($ENV{"FORMAT_PRETTY_TABLE_COLUMN_ORDERS"})
ABresult
1X1
011
000
42 $opts->{table_column_formats} //= $json->decode(
43 $ENV{FORMAT_PRETTY_TABLE_COLUMN_FORMATS})
44 44 ▶ if defined($ENV{FORMAT_PRETTY_TABLE_COLUMN_FORMATS});
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"FORMAT_PRETTY_TABLE_COLUMN_FORMATS"} 0 44 44
Condition0%0/3
$opts->{"table_column_formats"} //= $json->decode($ENV{"FORMAT_PRETTY_TABLE_COLUMN_FORMATS"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"table_column_formats"} //= $json->decode($ENV{"FORMAT_PRETTY_TABLE_COLUMN_FORMATS"})
$opts->{"table_column_formats"} $json->decode($ENV{"FORMAT_PRETTY_TABLE_COLUMN_FORMATS"})
Truth table0%0/3
A //= B
A= $opts->{"table_column_formats"}B= $json->decode($ENV{"FORMAT_PRETTY_TABLE_COLUMN_FORMATS"})
ABresult
1X1
011
000
45 $opts->{table_column_types} //= $json->decode(
46 $ENV{FORMAT_PRETTY_TABLE_COLUMN_TYPES})
47 44 ▶ if defined($ENV{FORMAT_PRETTY_TABLE_COLUMN_TYPES});
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"FORMAT_PRETTY_TABLE_COLUMN_TYPES"} 0 44 44
Condition0%0/3
$opts->{"table_column_types"} //= $json->decode($ENV{"FORMAT_PRETTY_TABLE_COLUMN_TYPES"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"table_column_types"} //= $json->decode($ENV{"FORMAT_PRETTY_TABLE_COLUMN_TYPES"})
$opts->{"table_column_types"} $json->decode($ENV{"FORMAT_PRETTY_TABLE_COLUMN_TYPES"})
Truth table0%0/3
A //= B
A= $opts->{"table_column_types"}B= $json->decode($ENV{"FORMAT_PRETTY_TABLE_COLUMN_TYPES"})
ABresult
1X1
011
000
48 44 ▶ $opts->{list_max_columns} //= $ENV{FORMAT_PRETTY_LIST_MAX_COLUMNS};
Condition33%1/3
$opts->{"list_max_columns"} //= $ENV{"FORMAT_PRETTY_LIST_MAX_COLUMNS"}
l!l&&r!l&&!r
0044
MC/DC0%0/2
$opts->{"list_max_columns"} //= $ENV{"FORMAT_PRETTY_LIST_MAX_COLUMNS"}
$opts->{"list_max_columns"} $ENV{"FORMAT_PRETTY_LIST_MAX_COLUMNS"}
Truth table33%1/3
A //= B
A= $opts->{"list_max_columns"}B= $ENV{"FORMAT_PRETTY_LIST_MAX_COLUMNS"}
ABresult
1X1
011
000
49 44 bless {opts=>$opts}, $class;
50 }
51  
52 147 ▶sub _is_cell_or_format_cell {
Subroutine _is_cell_or_format_cell: called CC=13 SCAR=27.7
53 147 my ($self, $data, $is_format) = @_;
54  
55 # XXX currently hardcoded limits
56 147 my $maxlen = 1000;
57  
58 147 ▶ if (!ref($data) || blessed($data)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $data or builtin::blessed($data)) { } 135 12 147
elsif (ref $data eq 'ARRAY') { } 9 3 12
Condition66%2/3
not ref $data or builtin::blessed($data)
l!l&&r!l&&!r
135012
MC/DC50%1/2
not ref $data or builtin::blessed($data)
not ref $data builtin::blessed($data)
Truth table66%2/3
not(A) or B
A= not ref $dataB= builtin::blessed($data)
ABresult
1X1
011
000
59 135 ▶ if (!defined($data)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 8 127 135
60 8 ▶ return "" if $is_format;
Branch: 1 branch
BranchTrueFalseTotal
if $is_format 8 0 8
61 0 return 1;
62 }
63 127 ▶ if (length($data) > $maxlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data > $maxlen) 0 127 127
64 0 return;
65 }
66 127 ▶ return "$data" if $is_format;
Branch: 1 branch
BranchTrueFalseTotal
if $is_format 42 85 127
67 85 return 1;
68 } elsif (ref($data) eq 'ARRAY') {
69 9 ▶ if (grep {ref($_) && !blessed($_)} @$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {ref $_ and not builtin::blessed($_);} @$data) 0 9 9
Condition33%1/3
ref $_ && !builtin::blessed($_)
!ll&&!rl&&r
2100
MC/DC0%0/2
ref $_ && !builtin::blessed($_)
ref $_ !builtin::blessed($_)
Truth table33%1/3
A && not(B)
A= ref $_B= !builtin::blessed($_)
ABresult
0X0
100
111
70 0 return;
71 }
72 9 ▶ my $s = join(", ", map {defined($_) ? "$_":""} @$data);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 21 0 21
73 9 ▶ if (length($s) > $maxlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $s > $maxlen) 0 9 9
74 0 return;
75 }
76 9 ▶ return $s if $is_format;
Branch: 1 branch
BranchTrueFalseTotal
if $is_format 3 6 9
77 6 return 1;
78 } else {
79 3 return;
80 }
81 }
82  
83 # return a string when data can be represented as a cell, otherwise undef. what
84 # can be put in a table cell? a string (or stringified object) or array of
85 # strings (stringified objects) that is quite "short".
86 53 ▶sub _format_cell { _is_cell_or_format_cell(@_, 1) }
Subroutine _format_cell: called CC=1 SCAR=0.0
87  
88 94 ▶sub _is_cell { _is_cell_or_format_cell(@_, 0) }
Subroutine _is_cell: called CC=1 SCAR=0.0
89  
90 72 ▶sub _detect_struct {
Subroutine _detect_struct: called CC=26 SCAR=32.7
91 72 my ($self, $data) = @_;
92 72 my $struct;
93 72 my $struct_meta = {};
94  
95 # XXX perhaps, use Data::Schema later?
96 CHECK_FORMAT:
97 {
98 72 CHECK_SCALAR:
99 {
100 72 ▶ if (!ref($data) || blessed($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $data or builtin::blessed($data)) 27 45 72
Condition100%3/3
not ref $data or builtin::blessed($data)
l!l&&r!l&&!r
25245
MC/DC100%2/2
not ref $data or builtin::blessed($data)
not ref $data builtin::blessed($data)
Truth table100%3/3
not(A) or B
A= not ref $dataB= builtin::blessed($data)
ABresult
1X1
011
000
101 27 $struct = "scalar";
102 27 last CHECK_FORMAT;
103 }
104 }
105  
106 CHECK_AOA:
107 {
108 45 ▶ if (ref($data) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "ARRAY") 24 21 45
109 24 my $numcols;
110 24 for my $row (@$data) {
111 27 ▶ last CHECK_AOA unless ref($row) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "ARRAY" 17 10 27
112 10 ▶ last CHECK_AOA if defined($numcols) && $numcols != @$row;
Branch: 1 branch
BranchTrueFalseTotal
if defined $numcols and $numcols != @$row 2 8 10
Condition100%3/3
defined $numcols and $numcols != @$row
!ll&&!rl&&r
532
MC/DC100%2/2
defined $numcols and $numcols != @$row
defined $numcols $numcols != @$row
Truth table100%3/3
A and B
A= defined $numcolsB= $numcols != @$row
ABresult
0X0
100
111
113 8 ▶ last CHECK_AOA if grep { !$self->_is_cell($_) } @$row;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $self->_is_cell($_);} @$row 0 8 8
114 8 $numcols = @$row;
115 }
116 5 $struct = "aoa";
117 5 last CHECK_FORMAT;
118 }
119 }
120  
121 CHECK_AOH:
122 {
123 40 ▶ if (ref($data) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "ARRAY") 19 21 40
124 19 $struct_meta->{columns} = {};
125 19 for my $row (@$data) {
126 27 ▶ last CHECK_AOH unless ref($row) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "HASH" 7 20 27
127 20 for my $k (keys %$row) {
128 47 ▶ last CHECK_AOH if !$self->_is_cell($row->{$k});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_cell($row->{$k}) 0 47 47
129 47 $struct_meta->{columns}{$k} = 1;
130 }
131 }
132 12 $struct = "aoh";
133 12 last CHECK_FORMAT;
134 }
135 }
136  
137 # list of scalars/cells
138 CHECK_LIST:
139 {
140 28 ▶ if (ref($data) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "ARRAY") 7 21 28
141 7 for (@$data) {
142 16 ▶ last CHECK_LIST unless $self->_is_cell($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_cell($_) 0 16 16
143 }
144 7 $struct = "list";
145 7 last CHECK_FORMAT;
146 }
147 }
148  
149 # hash which contains at least one "table" (list/aoa/aoh)
150 CHECK_HOT:
151 {
152 21 ▶ last CHECK_HOT if $self->{opts}{skip_hot};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{"skip_hot"} 0 21 21
153 21 ▶ last CHECK_HOT unless ref($data) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 21 21
154 21 my $has_t;
155 21 ▶ while (my ($k, $v) = each %$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 24 21 45
156 24 my ($s2, $sm2) = $self->_detect_struct($v, {skip_hot=>1});
157 24 ▶ last CHECK_HOT unless $s2;
Branch: 1 branch
BranchTrueFalseTotal
unless $s2 0 24 24
158 24 ▶ $has_t = 1 if $s2 =~ /^(?:list|aoa|aoh|hash)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $s2 =~ /^(?:list|aoa|aoh|hash)$/ 9 15 24
159 }
160 21 ▶ last CHECK_HOT unless $has_t;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_t 14 7 21
161 7 $struct = "hot";
162 7 last CHECK_FORMAT;
163 }
164  
165 # hash of scalars/cells
166 CHECK_HASH:
167 {
168 14 ▶ if (ref($data) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH") 14 0 14
169 14 for (values %$data) {
170 13 ▶ last CHECK_HASH unless $self->_is_cell($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_cell($_) 3 10 13
171 }
172 11 $struct = "hash";
173 11 last CHECK_FORMAT;
174 }
175 }
176  
177 }
178  
179 72 ($struct, $struct_meta);
180 }
181  
182 # t (table) is a structure like this: {cols=>["colName1", "colName2", ...]},
183 # rows=>[ [row1.1, row1.2, ...], [row2.1, row2.2, ...], ... ], at_opts=>{...},
184 # col_widths=>{colName1=>5, ...}}. the job of this routine is to render it
185 # (currently uses Text::ANSITable).
186 12 ▶sub _render_table {
Subroutine _render_table: called CC=19 SCAR=37.4
187 12 my ($self, $t) = @_;
188  
189 12 my $colfmts;
190 12 my $tcff = $self->{opts}{table_column_formats};
191 12 ▶ if ($tcff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tcff) 2 10 12
192 2 for my $tcf (@$tcff) {
193 2 my $match = 1;
194 2 my @tcols = @{ $t->{cols} };
195 2 for my $scol (keys %$tcf) {
196 0 ▶ do { $match = 0; last } unless grep { $_ eq $scol } @tcols;
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $scol;} @tcols) 0 3 3
197 }
198 2 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 2 0 2
199 2 $colfmts = $tcf;
200 2 last;
201 }
202 }
203 }
204  
205 12 my $coltypes;
206 12 my $tctt = $self->{opts}{table_column_types};
207 12 ▶ if ($tctt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tctt) 0 12 12
208 0 for my $tct (@$tctt) {
209 0 my $match = 1;
210 0 my @tcols = @{ $t->{cols} };
211 0 for my $scol (keys %$tct) {
212 0 ▶ do { $match = 0; last } unless grep { $_ eq $scol } @tcols;
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $scol;} @tcols) 0 0 0
213 }
214 0 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 0 0 0
215 0 $coltypes = $tct;
216 0 last;
217 }
218 }
219 }
220  
221 # render using Text::ANSITable
222 12 my $at = Text::ANSITable->new;
223 12 $at->columns($t->{cols});
224 12 $at->rows($t->{rows});
225 12 ▶ if ($t->{at_opts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"at_opts"}) 7 5 12
226 7 $at->{$_} = $t->{at_opts}{$_} for keys %{ $t->{at_opts} };
227 }
228 12 ▶ if ($colfmts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colfmts) 2 10 12
229 $at->set_column_style($_ => formats => $colfmts->{$_})
230 2 for keys %$colfmts;
231 }
232 12 ▶ if ($coltypes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coltypes) 0 12 12
233 $at->set_column_style($_ => type => $coltypes->{$_})
234 0 for keys %$coltypes;
235 }
236 12 ▶ if ($t->{col_widths}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"col_widths"}) 0 12 12
237 $at->set_column_style($_ => width => $t->{col_widths}{$_})
238 0 for keys %{ $t->{col_widths} };
239 }
240 12 $at->draw;
241 }
242  
243 # format unknown structure, the default is to dump YAML structure
244 2 ▶sub _format_unknown {
Subroutine _format_unknown: called CC=1 SCAR=0.0
245 2 my ($self, $data) = @_;
246 2 Dump($data);
247 }
248  
249 8 ▶sub _format_scalar {
Subroutine _format_scalar: called CC=4 SCAR=13.9
250 8 my ($self, $data) = @_;
251  
252 8 ▶ my $sdata = defined($data) ? "$data" : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 7 1 8
253 8 ▶ return "" if !length($sdata);
Branch: 1 branch
BranchTrueFalseTotal
unless length $sdata 2 6 8
254 6 ▶ return $sdata =~ /\n\z/s ? $sdata : "$sdata\n";
Branch: 1 branch
BranchTrueFalseTotal
$sdata =~ /\n\z/s ? : 2 4 6
255 }
256  
257 3 ▶sub _format_list {
Subroutine _format_list: called CC=19 SCAR=35.1
258 3 my ($self, $data) = @_;
259 3 ▶ if ($self->{opts}{interactive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'opts'}{'interactive'}) { } 3 0 3
260  
261 3 require List::Util;
262 3 require POSIX;
263  
264 # format list as as columns (a la 'ls' output)
265  
266 3 my @rows = map { $self->_format_cell($_) } @$data;
267  
268 3 ▶ my $maxwidth = List::Util::max(map { length } @rows) // 0;
Condition50%1/2
&List::Util::max(map({length $_;} @rows)) // 0
l!l
30
MC/DC0%0/1
&List::Util::max(map({length $_;} @rows)) // 0
&List::Util::max(map({length $_;} @rows))
Truth table50%1/2
A
A= &List::Util::max(map({length $_;} @rows))
Aresult
11
00
269 3 my ($termcols, $termrows);
270 3 ▶ if ($ENV{COLUMNS}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{'COLUMNS'}) { } 0 3 3
elsif (eval { do { require Term::Size; 1 } }) { } 3 0 3
271 0 $termcols = $ENV{COLUMNS};
272 3 } elsif (eval { require Term::Size; 1 }) {
273 3 ($termcols, $termrows) = Term::Size::chars(*STDOUT{IO});
274 } else {
275 # sane default, on windows we need to offset by 1 because printing
276 # at the rightmost column will cause cursor to move down one line.
277 0 ▶ $termcols = $^O =~ /Win/ ? 79 : 80;
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /Win/ ? : 0 0 0
278 }
279 3 my $numcols = 1;
280 3 ▶ if ($maxwidth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxwidth) 3 0 3
281 # | some-text-some | some-text-some... |
282 # 2/\__maxwidth__/\3/\__maxwidth__/...\2
283 #
284 # table width = (2+maxwidth) + (3+maxwidth)*(numcols-1) + 2
285 #
286 # so with a bit of algrebra, solve for numcols:
287 3 $numcols = int( (($termcols-1)-$maxwidth-6)/(3+$maxwidth) + 1 );
288 3 ▶ $numcols = @rows if $numcols > @rows;
Branch: 1 branch
BranchTrueFalseTotal
if $numcols > @rows 0 3 3
289 3 ▶ $numcols = 1 if $numcols < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $numcols < 1 3 0 3
290 }
291 $numcols = $self->{opts}{list_max_columns}
292 if defined($self->{opts}{list_max_columns}) &&
293 3 ▶ $numcols > $self->{opts}{list_max_columns};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"opts"}{"list_max_columns"} and $numcols > $self->{"opts"}{"list_max_columns"} 0 3 3
Condition33%1/3
defined $self->{"opts"}{"list_max_columns"} and $numcols > $self->{"opts"}{"list_max_columns"}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $self->{"opts"}{"list_max_columns"} and $numcols > $self->{"opts"}{"list_max_columns"}
defined $self->{"opts"}{"list_max_columns"} $numcols > $self->{"opts"}{"list_max_columns"}
Truth table33%1/3
A and B
A= defined $self->{"opts"}{"list_max_columns"}B= $numcols > $self->{"opts"}{"list_max_columns"}
ABresult
0X0
100
111
294 3 my $numrows = POSIX::ceil(@rows/$numcols);
295 3 ▶ if ($numrows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numrows) 3 0 3
296 # reduce number of columns to avoid empty columns
297 3 $numcols = POSIX::ceil(@rows/$numrows);
298 }
299 #say "D: $numcols x $numrows";
300  
301 3 my $t = {rows=>[], at_opts=>{show_header=>0}};
302 3 $t->{cols} = [map { "c$_" } 1..$numcols];
303 3 ▶ if ($numcols > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numcols > 1) 0 3 3
304 0 $t->{col_widths}{"c$_"} = $maxwidth for 1..$numcols;
305 }
306 3 for my $r (1..$numrows) {
307 7 my @trow;
308 7 for my $c (1..$numcols) {
309 7 my $idx = ($c-1)*$numrows + ($r-1);
310 7 ▶ push @trow, $idx < @rows ? $rows[$idx] : '';
Branch: 1 branch
BranchTrueFalseTotal
$idx < @rows ? : 7 0 7
311 }
312 7 push @{$t->{rows}}, \@trow;
313 }
314  
315 3 return $self->_render_table($t);
316  
317 } else {
318 0 my @rows;
319 0 for my $row (@$data) {
320 0 ▶ push @rows, ($row // "") . "\n";
Condition0%0/2
$row // ""
l!l
00
MC/DC0%0/1
$row // ""
$row
Truth table0%0/2
A
A= $row
Aresult
11
00
321 }
322 0 return join("", @rows);
323 }
324 }
325  
326 3 ▶sub _format_hash {
Subroutine _format_hash: called CC=5 SCAR=20.2
327 3 my ($self, $data) = @_;
328 # format hash as two-column table
329 3 ▶ if ($self->{opts}{interactive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'opts'}{'interactive'}) { } 3 0 3
330 3 my $t = {cols=>[qw/key value/], rows=>[],
331 at_opts=>{}};
332 3 for my $k (sort keys %$data) {
333 4 push @{ $t->{rows} }, [$k, $self->_format_cell($data->{$k})];
334 }
335 3 return $self->_render_table($t);
336 } else {
337 0 my @t;
338 0 for my $k (sort keys %$data) {
339 0 ▶ push @t, $k, "\t", ($data->{$k} // ""), "\n";
Condition0%0/2
$data->{$k} // ""
l!l
00
MC/DC0%0/1
$data->{$k} // ""
$data->{$k}
Truth table0%0/2
A
A= $data->{$k}
Aresult
11
00
340 }
341 0 return join("", @t);
342 }
343 }
344  
345 3 ▶sub _format_aoa {
Subroutine _format_aoa: called CC=5 SCAR=16.1
346 3 my ($self, $data) = @_;
347 # show aoa as table
348 3 ▶ if ($self->{opts}{interactive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'opts'}{'interactive'}) { } 2 1 3
349 2 ▶ if (@$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$data) { } 1 1 2
350 1 my $t = {rows=>[], at_opts=>{}};
351 1 $t->{cols} = [map { "column$_" } 0..@{ $data->[0] }-1];
352 1 for my $i (0..@$data-1) {
353 2 push @{ $t->{rows} },
354 2 [map {$self->_format_cell($_)} @{ $data->[$i] }];
355 }
356 1 return $self->_render_table($t);
357 } else {
358 1 return "";
359 }
360 } else {
361 # tab-separated
362 1 my @t;
363 1 for my $row (@$data) {
364 2 push @t, join("\t", map { $self->_format_cell($_) } @$row) .
365 "\n";
366 }
367 1 return join("", @t);
368 }
369 }
370  
371 6 ▶sub _format_aoh {
Subroutine _format_aoh: called CC=4 SCAR=13.9
372 6 my ($self, $data, $struct_meta) = @_;
373 # show aoh as table
374 6 my @cols = @{ $self->_order_table_columns(
375 6 [keys %{$struct_meta->{columns}}]) };
376 6 ▶ if ($self->{opts}{interactive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'opts'}{'interactive'}) { } 5 1 6
377 5 my $t = {cols=>\@cols, rows=>[]};
378 5 for my $i (0..@$data-1) {
379 8 my $row = $data->[$i];
380 8 push @{ $t->{rows} }, [map {$self->_format_cell($row->{$_})} @cols];
381 }
382 5 return $self->_render_table($t);
383 } else {
384 # tab-separated
385 1 my @t;
386 1 for my $row (@$data) {
387 3 my @row = map {$self->_format_cell($row->{$_})} @cols;
388 3 push @t, join("\t", @row) . "\n";
389 }
390 1 return join("", @t);
391 }
392 }
393  
394 2 ▶sub _format_hot {
Subroutine _format_hot: called CC=2 SCAR=6.9
395 2 my ($self, $data) = @_;
396 # show hot as paragraphs:
397 #
398 # key:
399 # value (table)
400 #
401 # key2:
402 # value ...
403 2 my @t;
404 2 for my $k (sort keys %$data) {
405 4 push @t, "$k:\n", $self->_format($data->{$k}), "\n";
406 }
407 2 return join("", @t);
408 }
409  
410 27 ▶sub _format {
Subroutine _format: called CC=8 SCAR=20.8
411 27 my ($self, $data) = @_;
412  
413 27 my ($struct, $struct_meta) = $self->_detect_struct($data);
414  
415 27 ▶ if (!$struct) {
Branch: 7 branches
BranchTrueFalseTotal
if (not $struct) { } 2 25 27
elsif ($struct eq 'scalar') { } 8 17 25
elsif ($struct eq 'list') { } 3 14 17
elsif ($struct eq 'hash') { } 3 11 14
elsif ($struct eq 'aoa') { } 3 8 11
elsif ($struct eq 'aoh') { } 6 2 8
elsif ($struct eq 'hot') { } 2 0 2
416 2 return $self->_format_unknown($data, $struct_meta);
417 } elsif ($struct eq 'scalar') {
418 8 return $self->_format_scalar($data, $struct_meta);
419 } elsif ($struct eq 'list') {
420 3 return $self->_format_list($data, $struct_meta);
421 } elsif ($struct eq 'hash') {
422 3 return $self->_format_hash($data, $struct_meta);
423 } elsif ($struct eq 'aoa') {
424 3 return $self->_format_aoa($data, $struct_meta);
425 } elsif ($struct eq 'aoh') {
426 6 return $self->_format_aoh($data, $struct_meta);
427 } elsif ($struct eq 'hot') {
428 2 return $self->_format_hot($data, $struct_meta);
429 } else {
430 0 die "BUG: Unknown format `$struct`";
431 }
432 }
433  
434 6 ▶sub _order_table_columns {
Subroutine _order_table_columns: called CC=12 SCAR=24.9
435 #$log->tracef('=> _order_table_columns(%s)', \@_);
436 6 my ($self, $cols) = @_;
437  
438 6 my $found; # whether we found an ordering in table_column_orders
439 6 my $tco = $self->{opts}{table_column_orders};
440 6 my %orders; # colname => idx
441 6 ▶ if ($tco) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tco) 2 4 6
442 2 ▶ die "table_column_orders should be an arrayref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tco eq "ARRAY" 0 2 2
443 unless ref($tco) eq 'ARRAY';
444 CO:
445 2 for my $co (@$tco) {
446 2 ▶ die "table_column_orders elements must all be arrayrefs"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $co eq "ARRAY" 0 2 2
447 unless ref($co) eq 'ARRAY';
448 2 for my $c (@$co) {
449 7 ▶ next CO unless grep { $_ eq $c } @$cols;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $c;} @$cols 1 6 7
450 }
451  
452 1 $found++;
453 1 ▶ for (my $i=0; $i<@$co; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$co) 3 1 4
454 3 $orders{$co->[$i]} = $i;
455 }
456 1 $found++;
457 1 last CO;
458 }
459 }
460  
461 6 my @ocols;
462 6 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) { } 1 5 6
463 @ocols = sort {
464 1 (defined($orders{$a}) && defined($orders{$b}) ?
465 10 ▶ $orders{$a} <=> $orders{$b} : 0)
Branch: 1 branch
BranchTrueFalseTotal
defined $orders{$a} && defined $orders{$b} ? : 1 9 10
Condition83%5/6
(defined $orders{$a} && defined $orders{$b} ? $orders{$a} <=> $orders{$b} : 0) || $a cmp $b
l!l&&r!l&&!r
190
defined $orders{$a} and defined $orders{$b}
!ll&&!rl&&r
721
MC/DC0%0/3
(defined $orders{$a} && defined $orders{$b} ? $orders{$a} <=> $orders{$b} : 0) || $a cmp $b
defined $orders{$a} defined $orders{$b} $a cmp $b
Truth table60%3/5
A and B || C
A= defined $orders{$a}B= defined $orders{$b}C= $a cmp $b
ABCresult
11X1
0X11
1011
0X00
1000
466 || $a cmp $b
467 } (sort @$cols);
468 } else {
469 5 @ocols = sort @$cols;
470 }
471  
472 6 \@ocols;
473 }
474  
475 4 1;
476 # ABSTRACT: Pretty-print data structure for console output
477  
478 __END__
↑ Summary SimpleText.pm »