blib/lib/App/RecordStream/Operation/totable.pm

filter: Statementstmt 89.2% 100 / 112 Branchbran 66.6% 24 / 36 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 8 totaltotal 81.8% 144 / 176 CC 27 SCAR 21.6
CC
27
Cov
84%
format_field
24.3
stream_done
9.0
format_row
7.0
CRAP 8.7
« blib/lib/App/RecordStream/KeySpec.pm ↑ Summary blib/lib/App/RecordStream/KeyGroups.pm »
1 package App::RecordStream::Operation::totable;
2  
3 7 our $VERSION = "4.0.23";
4  
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use base qw(App::RecordStream::Accumulator App::RecordStream::Operation);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶use App::RecordStream::OutputStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub init {
13 12 ▶ my $this = shift;
Subroutine init: called CC=2 SCAR=6.9
Pod undocumented
14 12 my $args = shift;
15  
16 12 my $no_header = 0;
17 12 my $delimiter = "\t";
18 12 my $spreadsheet = 0;
19 12 my $clear = 0;
20  
21 12 my $key_groups = App::RecordStream::KeyGroups->new();
22  
23 my $spec = {
24 "no-header|n" => \$no_header,
25 3 ▶ "key|k|field|f=s" => sub { $key_groups->add_groups($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 12 "delim|d=s" => \$delimiter,
27 "clear" => \$clear,
28 "spreadsheet|s" => \$spreadsheet,
29 };
30  
31 12 $this->parse_options($args, $spec);
32  
33 11 ▶ if ( ! $key_groups->has_any_group() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key_groups->has_any_group) 8 3 11
34 8 $key_groups->add_groups('!.!returnrefs!sort');
35 }
36  
37 11 $this->{'NO_HEADER'} = $no_header;
38 11 $this->{'KEY_GROUPS'} = $key_groups;
39 11 $this->{'DELIMITER'} = $delimiter;
40 11 $this->{'SPREADSHEET'} = $spreadsheet;
41 11 $this->{'CLEAR'} = $clear;
42 }
43  
44 sub stream_done {
45 9 ▶ my $this = shift;
Subroutine stream_done: called CC=9 SCAR=22.0
Pod undocumented
46  
47 9 my $records = $this->get_records();
48 9 my $key_group = $this->{'KEY_GROUPS'};
49 9 my %widths;
50 9 my $fields = [];
51  
52 9 foreach my $record (@$records) {
53 47 my $specs = $key_group->get_keyspecs_for_record($record);
54  
55 47 foreach my $field (@$specs) {
56 77 ▶ if(!exists($widths{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $widths{$field}) 15 62 77
57 15 $widths{$field} = 0;
58 15 push @$fields, $field;
59 }
60  
61 77 $widths{$field} = max($widths{$field}, length($this->extract_field($record, $field)));
62 }
63 }
64  
65 9 my $no_header = $this->{'NO_HEADER'};
66 9 ▶ if(!$no_header) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_header) 8 1 9
67 8 foreach my $field (keys(%widths)) {
68 13 $widths{$field} = max($widths{$field}, length($field));
69 }
70 }
71  
72 9 $this->{'FIELDS'} = $fields;
73  
74 9 ▶ if(!$no_header) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_header) 8 1 9
75 $this->push_line(
76 $this->format_row(
77 $fields,
78 \%widths,
79 13 ▶ sub { return $_[1]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
80 8 ""
81 )
82 );
83  
84 8 ▶ if ( ! $this->{'SPREADSHEET'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"SPREADSHEET"}) 6 2 8
85 $this->push_line(
86 $this->format_row(
87 $fields,
88 \%widths,
89 9 ▶ sub { return ("-" x $widths{$_[1]}); },
Subroutine __ANON__: called CC=1 SCAR=0.0
90 6 ""
91 )
92 );
93 }
94 }
95  
96 9 my %last = map { $_ => "" } (keys(%widths));
97 9 foreach my $record (@$records) {
98 47 $this->push_line(
99 $this->format_row(
100 $fields,
101 \%widths,
102 \&format_field,
103 [$record, \%last]
104 )
105 );
106 }
107 }
108  
109 sub format_field
110 {
111 77 ▶ my ($this, $field, $thunk) = @_;
Subroutine format_field: called CC=8 SCAR=31.9
Pod undocumented
112 77 my ($r, $lastr) = @$thunk;
113  
114 77 my $value = ${$r->guess_key_from_spec($field)};
115 77 ▶ $value = '' if ( ! defined $value );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 77 77
116  
117 77 ▶ if ( ref($value) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 0 77 77
118 {
119 0 $value = App::RecordStream::OutputStream::hashref_string($value);
120 }
121  
122 77 ▶ if($this->{'CLEAR'})
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"CLEAR"}) 0 77 77
123 {
124 0 ▶ if($value eq $lastr->{$field})
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq $lastr->{$field}) { } 0 0 0
125 {
126 # This column matches the "last" value so we clear the cell.
127 0 $value = "";
128 }
129 else
130 {
131 # This column did not match so we do not clear the cell. We also
132 # invalidate all "last" field values to the right of this column.
133 0 my $startInvalidating = 0;
134 0 for(@{$this->{'FIELDS'}})
135 {
136 0 ▶ if($_ eq $field)
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq $field) { } 0 0 0
elsif ($startInvalidating) { } 0 0 0
137 {
138 0 $startInvalidating = 1;
139 }
140 elsif($startInvalidating)
141 {
142 0 $lastr->{$_} = "";
143 }
144 }
145 0 $lastr->{$field} = $value;
146 }
147 }
148  
149 77 return $value;
150 }
151  
152  
153 sub format_row {
154 61 ▶ my ($this, $fieldsr, $widthsr, $format_fieldr, $thunk) = @_;
Subroutine format_row: called CC=7 SCAR=19.5
Pod undocumented
155  
156 61 my $first = 1;
157 61 my $row_string = "";
158  
159 61 foreach my $field (@$fieldsr) {
160 99 my $field_string = $format_fieldr->($this, $field, $thunk);
161  
162 99 ▶ unless ( defined $field_string ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field_string) 0 99 99
163 0 $field_string = '';
164 }
165  
166 99 ▶ if ( (! $this->{'SPREADSHEET'}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $this->{"SPREADSHEET"} and length $field_string < $widthsr->{$field}) 35 64 99
Condition100%3/3
not $this->{"SPREADSHEET"} and length $field_string < $widthsr->{$field}
!ll&&!rl&&r
244035
MC/DC100%2/2
not $this->{"SPREADSHEET"} and length $field_string < $widthsr->{$field}
not $this->{"SPREADSHEET"} length $field_string < $widthsr->{$field}
Truth table100%3/3
not(A) and B
A= not $this->{"SPREADSHEET"}B= length $field_string < $widthsr->{$field}
ABresult
0X0
100
111
167 (length($field_string) < $widthsr->{$field})) {
168  
169 35 $field_string .= " " x ($widthsr->{$field} - length($field_string));
170 }
171  
172 99 ▶ if($first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) { } 61 38 99
173 61 $first = 0;
174 }
175 else {
176 38 ▶ $row_string .= ($this->{'SPREADSHEET'}) ? $this->{'DELIMITER'} : " ";
Branch: 1 branch
BranchTrueFalseTotal
$this->{'SPREADSHEET'} ? : 12 26 38
177 }
178  
179 99 $row_string .= $field_string;
180 }
181  
182 61 return $row_string;
183 }
184  
185 # Max helper function
186 sub max {
187 90 ▶ my $max = shift;
Subroutine max: called CC=3 SCAR=11.0
Pod undocumented
188  
189 90 foreach my $value (@_) {
190 90 ▶ if($value > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > $max) 23 67 90
191 23 $max = $value;
192 }
193 }
194  
195 90 return $max;
196 }
197  
198 sub extract_field {
199 77 ▶ my $this = shift;
Subroutine extract_field: called CC=3 SCAR=11.3
Pod undocumented
200 77 my $record = shift;
201 77 my $field = shift;
202  
203 77 my $value = ${$record->guess_key_from_spec($field)};
204 77 ▶ $value = '' if ( ! defined $value );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 77 77
205  
206 77 ▶ if ( ref($value) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 0 77 77
207 {
208 0 $value = App::RecordStream::OutputStream::hashref_string($value);
209 }
210  
211 77 return $value;
212 }
213  
214 sub add_help_types {
215 12 ▶ my $this = shift;
Subroutine add_help_types: called CC=1 SCAR=0.0
Pod undocumented
216 12 $this->use_help_type('keyspecs');
217 12 $this->use_help_type('keygroups');
218 12 $this->use_help_type('keys');
219 }
220  
221 sub usage {
222 1 ▶ my $this = shift;
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
223  
224 1 my $options = [
225 ['no-header|n', 'Do not print column headers'],
226 ['key|k <field name>', 'May be comma separated, may be specified multiple times. Specifies the fields to put in the table. May be a keyspec or a keygroup, see --help-keys'],
227 ['spreadsheet', 'Print out in a format suitable for excel. 1. Does not print line of -s after header 2. Separates by single character rather than series of spaces'],
228 ['delimiter <string>', 'Only useful with --spreadsheet, delimit with <string> rather than the default of a tab'],
229 ['clear', 'Put blanks in cells where all of the row so far matches the row above.'],
230 ];
231  
232 1 my $args_string = $this->options_string($options);
233  
234 1 return <<USAGE;
235 Usage: recs-totable <args> [<files>]
236 __FORMAT_TEXT__
237 Pretty prints a table of records to the screen. Will read in the entire
238 record stream to determine column size, and number of columns
239 __FORMAT_TEXT__
240  
241 $args_string
242  
243 Examples:
244 Display a table
245 recs-totable
246 Display only one field
247 recs-totable -f foo
248 Display two fields without a header
249 recs-totable -f foo -f bar --no-header
250 USAGE
251 }
252  
253 7 1;
« blib/lib/App/RecordStream/KeySpec.pm ↑ Summary blib/lib/App/RecordStream/KeyGroups.pm »