Table/Utils.pm

filter: Statementstmt 46.5% 27 / 58 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 70.0% 7 / 10 Podpod 100% 1 / 1 totaltotal 44.3% 35 / 79 CC 7 SCAR 25.0
CC
7
Cov
40%
table
30.0
_columns
12.0
_column_left
2.0
CRAP 12.2
« Table.pm ↑ Summary
1 package Map::Tube::Text::Table::Utils;
2  
3 # Pragmas.
4 6 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Modules.
9 6 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Text::UnicodeBox;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Text::UnicodeBox::Control qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Constants.
15 6 Readonly::Array our @EXPORT_OK => qw(table);
16 6 Readonly::Scalar our $EMPTY_STR => q{};
17 6 Readonly::Scalar our $SPACE => q{ };
18 6 Readonly::Scalar our $SPACE_ON_END_COUNT => 1;
19  
20 # Version.
21 6 our $VERSION = 0.04;
22  
23 # Print table.
24 sub table {
25 0 ▶ my ($title, $data_len_ar, $header_ar, $data_ar) = @_;
Subroutine table: not called CC=5 SCAR=34.0
Pod documented
26  
27 # Check data.
28 0 ▶ if (! @{$data_ar}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$data_ar;}) 0 0 0
29 0 return $EMPTY_STR;
30 }
31  
32 0 my $t = Text::UnicodeBox->new;
33  
34 # Table title.
35 0 my $pipes_in_count = @{$data_len_ar} * 2 - 2;
36 $t->add_line(
37 BOX_START('bottom' => 'light', 'top' => 'light'),
38 0 _column_left($title, sum(map { $_ + $SPACE_ON_END_COUNT }
39 0 @{$data_len_ar}) + $pipes_in_count),
40 BOX_END(),
41 );
42  
43 # Legend.
44 0 ▶ if (defined $header_ar) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $header_ar) 0 0 0
45 0 $t->add_line(
46 BOX_START('bottom' => 'light', 'top' => 'light'),
47 _columns($header_ar, $data_len_ar),
48 );
49 }
50  
51 # Data.
52 0 ▶ while (my $row_ar = shift @{$data_ar}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row_ar = shift @{$data_ar;}) 0 0 0
53 $t->add_line(
54 BOX_START(
55 0 ▶ @{$data_ar} == 0 ? ('bottom' => 'light') : (),
Branch: 1 branch
BranchTrueFalseTotal
@{$data_ar;} == 0 ? : 0 0 0
56 ),
57 _columns($row_ar, $data_len_ar),
58 );
59 }
60  
61 # Render to output.
62 0 return $t->render;
63 }
64  
65 # Column text with left align.
66 sub _column_left {
67 0 ▶ my ($text, $width) = @_;
Subroutine _column_left: not called CC=1 SCAR=6.9
68 0 my $text_len = length $text;
69 0 return $SPACE.$text.($SPACE x ($width - $text_len));
70 }
71  
72 # Get Text::UnicodeBox columns.
73 sub _columns {
74 0 ▶ my ($data_ar, $data_len_ar) = @_;
Subroutine _columns: not called CC=3 SCAR=24.8
75 0 my @ret;
76 0 my $i = 0;
77 0 foreach my $item (@{$data_ar}) {
78 0 push @ret, _column_left($item, $data_len_ar->[$i++]
79 + $SPACE_ON_END_COUNT);
80 0 ▶ if (@{$data_ar} > $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$data_ar;} > $i) { } 0 0 0
81 0 push @ret, BOX_RULE;
82 } else {
83 0 push @ret, BOX_END;
84 }
85 }
86 0 return @ret;
87 }
88  
89 6 1;
90  
91 __END__
92  
93 =pod
94  
95 =encoding utf8
96  
97 =head1 NAME
98  
99 Map::Tube::Text::Table::Utils - Utilities for Map::Tube::Text::Table.
100  
101 =head1 SYNOPSIS
102  
103 use Map::Tube::Text::Table::Utils qw(table);
104 my $table = table($title, $data_len_ar, $header_ar, $data_ar);
105  
106 =head1 SUBROUTINES
107  
108 =over 8
109  
110 =item C<table($title, $data_len_ar, $header_ar, $data_ar)>
111  
112 Print table.
113 Returns text.
114  
115 =back
116  
117 =head1 EXAMPLE1
118  
119 # Pragmas.
120 use strict;
121 use warnings;
122  
123 # Module.
124 use Encode qw(encode_utf8);
125 use Map::Tube::Text::Table::Utils qw(table);
126  
127 # Get table.
128 my $table = table('Title', [1, 2, 3], ['A', 'BB', 'CCC'], [
129 ['E', 'A', 'A'],
130 ['A', 'Ga', 'Acv'],
131 ]);
132  
133 # Print table.
134 print encode_utf8($table);
135  
136 # Output:
137 # ┌──────────────┐
138 # │ Title │
139 # ├───┬────┬─────┤
140 # │ A │ BB │ CCC │
141 # ├───┼────┼─────┤
142 # │ E │ A │ A │
143 # │ A │ Ga │ Acv │
144 # └───┴────┴─────┘
145  
146 =head1 DEPENDENCIES
147  
148 L<Exporter>,
149 L<List::Util>,
150 L<Readonly>,
151 L<Text::UnicodeBox>,
152 L<Text::UnicodeBox::Control>.
153  
154 =head1 SEE ALSO
155  
156 L<Map::Tube>,
157 L<Map::Tube::Text::Table>,
158 L<Task::Map::Tube>.
159  
160 =head1 REPOSITORY
161  
162 L<https://github.com/tupinek/Map-Tube-Text-Table>
163  
164 =head1 AUTHOR
165  
166 Michal Špaček L<mailto:skim@cpan.org>
167  
168 L<http://skim.cz>
169  
170 =head1 LICENSE AND COPYRIGHT
171  
172 © 2014-2015 Michal Špaček
173 Artistic License
174 BSD 2-Clause License
175  
176 =head1 VERSION
177  
178 0.04
179  
180 =cut
« Table.pm ↑ Summary