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

filter: Statementstmt 91.1% 82 / 90 Branchbran 57.6% 15 / 26 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 86.6% 13 / 15 Podpod 0.0% 0 / 7 totaltotal 80.4% 115 / 143 CC 19 SCAR 15.5
CC
19
Cov
84%
accept_record
9.9
output_value
6.0
init
4.3
CRAP 4.7
« blib/lib/App/RecordStream/Operation/help/FromManual.pm ↑ Summary blib/lib/App/RecordStream/InputStream.pm »
1 package App::RecordStream::Operation::toprettyprint;
2  
3 2 our $VERSION = "4.0.23";
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use base qw(App::RecordStream::Operation);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use App::RecordStream::OutputStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use App::RecordStream::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub init {
13 6 ▶ my $this = shift;
Subroutine init: called CC=4 SCAR=14.6
Pod undocumented
14 6 my $args = shift;
15  
16 6 my $limit = undef;
17 6 my $key_groups = App::RecordStream::KeyGroups->new();
18 6 my $do_not_nest = 0;
19 my $spec = {
20 0 ▶ "1" => sub { $limit = 1; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 4 ▶ "one" => sub { $limit = 1; },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 "n=i" => \$limit,
23 0 ▶ 'keys|k=s' => sub { $key_groups->add_groups($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 6 'nonested' => \$do_not_nest,
25 'aligned:s' => \(my $aligned),
26 };
27  
28 6 $this->parse_options($args, $spec);
29  
30 5 ▶ if ( ! $key_groups->has_any_group() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key_groups->has_any_group) 5 0 5
31 5 $key_groups->add_groups('!.!returnrefs');
32 }
33  
34 5 $this->{'LIMIT'} = $limit;
35 5 $this->{'KEY_GROUPS'} = $key_groups;
36 5 $this->{'NESTED_OUTPUT'} = not $do_not_nest;
37 5 ▶ $this->{'ALIGNED'} = $aligned =~ /^l(eft)?$/i ? 'left' : 'right'
Branch: 2 branches
BranchTrueFalseTotal
if defined $aligned 0 5 5
$aligned =~ /^l(eft)?$/i ? : 0 0 0
38 if defined $aligned;
39 };
40  
41 sub accept_record {
42 10 ▶ my $this = shift;
Subroutine accept_record: called CC=8 SCAR=22.9
Pod undocumented
43 10 my $record = shift;
44  
45 10 my $limit = $this->{'LIMIT'};
46 10 ▶ if ( defined($limit) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $limit) 5 5 10
47 5 ▶ if ( $limit == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit == 0) 1 4 5
48 1 return 0;
49 }
50 4 $this->{'LIMIT'}--;
51 }
52  
53 9 my $specs = $this->{'KEY_GROUPS'}->get_keyspecs_for_record($record);
54  
55 9 ▶ if ($this->{'ALIGNED'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"ALIGNED"}) 0 9 9
56 0 for my $key (@$specs) {
57 0 my $width = length $key;
58 $this->{'FORMAT_KEY_WIDTH'} = $width
59 0 ▶ if $width > $this->{'FORMAT_KEY_WIDTH'};
Branch: 1 branch
BranchTrueFalseTotal
if $width > $this->{"FORMAT_KEY_WIDTH"} 0 0 0
60 }
61 $this->{'FORMAT_KEY_WIDTH'} *= -1
62 0 ▶ if $this->{'ALIGNED'} eq 'left';
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"ALIGNED"} eq "left" 0 0 0
63 }
64  
65 9 $this->push_line('-' x 70);
66 9 foreach my $key (sort @$specs) {
67 21 my $value = ${$record->guess_key_from_spec($key)};
68 21 $this->output_value('', $key, $value);
69 }
70  
71 9 return 1;
72 }
73  
74 sub _format_key {
75 25 ▶ my $this = shift;
Subroutine _format_key: called CC=2 SCAR=7.6
76 25 my $key = shift;
77 25 ▶ return $key unless $this->{'FORMAT_KEY_WIDTH'};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"FORMAT_KEY_WIDTH"} 25 0 25
78 0 return sprintf '%*s', $this->{'FORMAT_KEY_WIDTH'}, $key;
79 }
80  
81 sub output_value {
82 25 ▶ my $this = shift;
Subroutine output_value: called CC=6 SCAR=17.9
Pod undocumented
83 25 my $prefix = shift;
84 25 my $key = shift;
85 25 my $value = shift;
86  
87 25 $key = $this->_format_key($key);
88  
89 25 ▶ if ( (ref($value) eq 'HASH') && $this->{'NESTED_OUTPUT'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'HASH' and $this->{'NESTED_OUTPUT'}) { } 3 22 25
elsif (ref $value eq 'ARRAY') { } 1 21 22
Condition100%3/3
ref $value eq "HASH" and $this->{"NESTED_OUTPUT"}
!ll&&!rl&&r
2113
MC/DC100%2/2
ref $value eq "HASH" and $this->{"NESTED_OUTPUT"}
ref $value eq "HASH" $this->{"NESTED_OUTPUT"}
Truth table100%3/3
A and B
A= ref $value eq "HASH"B= $this->{"NESTED_OUTPUT"}
ABresult
0X0
100
111
90 3 ▶ if ( scalar keys %$value > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$value > 0) { } 2 1 3
91 2 $this->push_line($prefix . "$key = HASH:");
92 2 $this->output_hash($prefix . ' ', $value);
93 }
94 else {
95 1 $this->push_line($prefix . "$key = EMPTY HASH");
96 }
97 }
98 elsif ( ref($value) eq 'ARRAY' ) {
99 1 ▶ if ( scalar @$value > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$value > 0) { } 1 0 1
100 1 $this->push_line($prefix . "$key = ARRAY:");
101 1 $this->output_array($prefix . ' ', $value);
102 }
103 else {
104 0 $this->push_line($prefix . "$key = EMPTY ARAY");
105 }
106 }
107 else {
108 21 my $value_string = App::RecordStream::OutputStream::hashref_string($value);
109 21 $this->push_line($prefix . "$key = $value_string");
110 }
111 }
112  
113 sub output_array {
114 1 ▶ my $this = shift;
Subroutine output_array: called CC=2 SCAR=6.9
Pod undocumented
115 1 my $prefix = shift;
116 1 my $array = shift;
117  
118 1 my $index = 0;
119 1 foreach my $value (sort @$array) {
120 2 $this->output_value($prefix, $index, $value);
121 2 $index++;
122 }
123 }
124  
125 sub output_hash {
126 2 ▶ my $this = shift;
Subroutine output_hash: called CC=2 SCAR=6.9
Pod undocumented
127 2 my $prefix = shift;
128 2 my $hash = shift;
129  
130 2 foreach my $key (sort keys %$hash) {
131 2 my $value = $hash->{$key};
132 2 $this->output_value($prefix, $key, $value);
133 }
134 }
135  
136 sub add_help_types {
137 6 ▶ my $this = shift;
Subroutine add_help_types: called CC=1 SCAR=0.0
Pod undocumented
138 6 $this->use_help_type('keyspecs');
139 6 $this->use_help_type('keygroups');
140 6 $this->use_help_type('keys');
141 }
142  
143 sub usage {
144 1 ▶ my $this = shift;
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
145  
146 1 my $options = [
147 ['1|one', 'Only print the first record'],
148 ['keys', 'Only print out specified keys, Maybe keyspecs may be keygroups, see --help-keys for more information'],
149 ['nonested', 'Do not nest the output of hashes, keep each value on one line'],
150 ['n <n>', 'Only print n records'],
151 ['aligned [r|l|right|left]', 'Format keys to the same width so values are aligned. Keys are right aligned by default, but you may pass a value of "left" to left align keys within the width.'],
152 ];
153  
154 1 my $args_string = $this->options_string($options);
155  
156 1 return <<USAGE;
157 Usage: recs-toprettyprint [files]
158 __FORMAT_TEXT__
159 Pretty print records, one key to a line, with a line of dashes (---)
160 separating records. Especially useful for records with very large amounts of
161 keys
162 __FORMAT_TEXT__
163  
164 Arguments:
165 $args_string
166  
167 Examples
168 # Pretty print records
169 recs-toprettyprint
170  
171 # Find all keys with 'time' in the name or value
172 ... | recs-toprettyprint --one | grep time
173 USAGE
174 }
175  
176 2 1;
« blib/lib/App/RecordStream/Operation/help/FromManual.pm ↑ Summary blib/lib/App/RecordStream/InputStream.pm »