Pad/Help.pm

filter: Statementstmt 94.0% 268 / 285 Branchbran 90.2% 74 / 82 Conditioncond 80.6% 25 / 31 MC/DCmcdc 50.0% 9 / 18 Subroutinesub 90.3% 28 / 31 Podpod 26.0% 6 / 23 totaltotal 87.2% 410 / 470 CC 62 SCAR 15.8
CC
62
Cov
92%
optionBlocks
9.0
detectWidth
8.5
headerBlock
7.0
CRAP 4.9
« Pad/Registry.pm ↑ Summary Pad/Config.pm »
1 11 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 11 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 class Getopt::Pad::Help :strict(params) {
5 11 ▶ use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶ use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶ use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶ use Getopt::Pad::Util qw(useColor optionSpelling encodedFor);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶ use Text::Wrap ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 our $VERSION = '0.06';
12  
13 11 field $level :param;
14 field $version :param = undef;
15 11 field $programName :param = undef;
16 11 field $width :param = undef;
17 11 field $color :param = undef;
18 11 field $handle :param = \*STDOUT;
19  
20 11 field @commandPath;
21 11 field $labelWidth;
22  
23 ADJUST {
24 $programName //= basename($0);
25 @commandPath = split ' ', $level->path;
26 }
27  
28 # The terminal is consulted when something is rendered, not when the
29 # Helper is built: a parse builds one per Level without printing, and
30 # $result->help may run much later, on a terminal of another size.
31 77 ▶ method width() { return $width //= $self->detectWidth }
Subroutine width: called CC=2 SCAR=7.0
Pod documented
Condition66%2/3
$width //= $self->detectWidth
l!l&&r!l&&!r
7430
MC/DC0%0/2
$width //= $self->detectWidth
$width $self->detectWidth
Truth table66%2/3
A //= B
A= $widthB= $self->detectWidth
ABresult
1X1
011
000
32 338 ▶ method color() { return $color //= useColor($handle) }
Subroutine color: called CC=2 SCAR=7.0
Pod documented
Condition66%2/3
$color //= useColor($handle)
l!l&&r!l&&!r
33440
MC/DC0%0/2
$color //= useColor($handle)
$color useColor($handle)
Truth table66%2/3
A //= B
A= $colorB= useColor($handle)
ABresult
1X1
011
000
33  
34 357 ▶ method labelWidth() {
Subroutine labelWidth: called CC=2 SCAR=6.9
Pod undocumented
35 357 ▶ return $labelWidth //= do {
Condition66%2/3
$labelWidth //= do { my(@labels) = (map({$self->optionLabel($_);} grep({not $_->hidden;} $level->options)), map({$self->argLabel($_);} $level->args), $level->commandNames); 3 + &max(0, map({length $_;} @labels)) + 5 }
l!l&&r!l&&!r
336210
MC/DC0%0/2
$labelWidth //= do { my(@labels) = (map({$self->optionLabel($_);} grep({not $_->hidden;} $level->options)), map({$self->argLabel($_);} $level->args), $level->commandNames); 3 + &max(0, map({length $_;} @labels)) + 5 }
$labelWidth do { my(@labels) = (map({$self->optionLabel($_);} grep({not $_->hidden;} $level->options)), map({$self->argLabel($_);} $level->args), $level->commandNames); 3 + &max(0, map({length $_;} @labels)) + 5 }
Truth table66%2/3
A //= B
A= $labelWidthB= do { my(@labels) = (map({$self->optionLabel($_);} grep({not $_->hidden;} $level->options)), map({$self->argLabel($_);} $level->args), $level->commandNames); 3 + &max(0, map({length $_;} @labels)) + 5 }
ABresult
1X1
011
000
36 21 my @labels = ((map { $self->optionLabel($_) } grep { !$_->hidden } $level->options), (map { $self->argLabel($_) } $level->args), $level->commandNames);
37 21 3 + max(0, map { length } @labels) + 5;
38 };
39 11 }
40  
41 3 ▶ method detectWidth() {
Subroutine detectWidth: called CC=6 SCAR=21.4
Pod undocumented
42 3 ▶ return $ENV{COLUMNS} if ($ENV{COLUMNS} // '') =~ /^\d+$/ && $ENV{COLUMNS} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"COLUMNS"} // "") =~ /^\d+$/u and $ENV{"COLUMNS"} > 0 1 2 3
Condition80%4/5
($ENV{"COLUMNS"} // "") =~ /^\d+$/u and $ENV{"COLUMNS"} > 0
!ll&&!rl&&r
201
$ENV{"COLUMNS"} // ""
l!l
12
MC/DC50%1/2
($ENV{"COLUMNS"} // "") =~ /^\d+$/u and $ENV{"COLUMNS"} > 0
$ENV{"COLUMNS"} $ENV{"COLUMNS"} > 0
Truth table66%2/3
A and B
A= $ENV{"COLUMNS"}B= $ENV{"COLUMNS"} > 0
ABresult
0X0
100
111
43  
44 2 ▶ if (-t $handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (-t $handle) 0 2 2
45 0 try {
46 0 require Term::ReadKey;
47 0 ▶ local $SIG{__WARN__} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 my ($columns) = Term::ReadKey::GetTerminalSize($handle);
49 0 ▶ return $columns if $columns;
Branch: 1 branch
BranchTrueFalseTotal
if $columns 0 0 0
50 }
51 catch ($error) { }
52 }
53  
54 2 return 100;
55 11 }
56  
57 230 ▶ method paint($text, $ansiCode) {
Subroutine paint: called CC=2 SCAR=6.9
Pod undocumented
58 230 ▶ return $self->color ? sprintf("\e[%sm%s\e[0m", $ansiCode, $text) : $text;
Branch: 1 branch
BranchTrueFalseTotal
$self->color ? : 48 182 230
59 11 }
60  
61 0 ▶ method printHelp() {
Subroutine printHelp: not called CC=1 SCAR=6.9
Pod documented
62 0 print {$handle} encodedFor($handle, $self->renderHelp);
63 0 return $self;
64 11 }
65  
66 0 ▶ method printVersion() {
Subroutine printVersion: not called CC=1 SCAR=6.9
Pod documented
67 0 print {$handle} encodedFor($handle, $self->renderVersion . "\n");
68 0 return $self;
69 11 }
70  
71 3 ▶ method renderVersion() {
Subroutine renderVersion: called CC=3 SCAR=11.0
Pod documented
72 3 ▶ my $shown = $version // $main::VERSION // 'unknown';
Condition80%4/5
($version // $main::VERSION) // "unknown"
l!l
12
$version // $main::VERSION
l!l&&r!l&&!r
102
MC/DC50%1/2
($version // $main::VERSION) // "unknown"
$version $main::VERSION
Truth table66%2/3
A // B
A= $versionB= $main::VERSION
ABresult
1X1
011
000
73 3 return sprintf('%s %s', $programName, $shown);
74 11 }
75  
76 23 ▶ method renderHelp() {
Subroutine renderHelp: called CC=5 SCAR=16.1
Pod documented
77 23 my @visibleOptions = grep { !$_->hidden } $level->options;
78 23 my @args = $level->args;
79  
80 23 my @blocks = ($self->headerBlock(\@visibleOptions, \@args));
81 23 ▶ push @blocks, $self->argsBlock(\@args) if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 6 17 23
82 23 ▶ push @blocks, $self->optionBlocks(\@visibleOptions) if @visibleOptions;
Branch: 1 branch
BranchTrueFalseTotal
if @visibleOptions 23 0 23
83 23 ▶ push @blocks, $self->commandsBlock if $level->hasCommands;
Branch: 1 branch
BranchTrueFalseTotal
if $level->hasCommands 2 21 23
84 23 ▶ push @blocks, $self->examplesBlock if $level->examples;
Branch: 1 branch
BranchTrueFalseTotal
if $level->examples 3 20 23
85  
86 23 return join("\n\n", @blocks) . "\n";
87 11 }
88  
89 11 my %palette = (
90 usage => '1;31',
91 description => '94',
92 section => '92',
93 annotation => '31',
94 typeLabel => '90',
95 subKey => '33',
96 validValue => '36',
97 defaultValue => '35',
98 );
99  
100 23 ▶ method headerBlock($options, $args) {
Subroutine headerBlock: called CC=7 SCAR=19.5
Pod undocumented
101 23 my @tokens = ($programName, @commandPath);
102 23 ▶ push @tokens, '[options]' if $options->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @$options 23 0 23
103 23 ▶ push @tokens, '<command>' if $level->hasCommands;
Branch: 1 branch
BranchTrueFalseTotal
if $level->hasCommands 2 21 23
104 23 foreach my $arg ($args->@*) {
105 9 my $token = $arg->short;
106 9 ▶ $token .= '...' if $arg->multiple;
Branch: 1 branch
BranchTrueFalseTotal
if $arg->multiple 0 9 9
107 9 ▶ $token = sprintf('[%s]', $token) if !$arg->required;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->required 5 4 9
108 9 push @tokens, $token;
109 }
110  
111 23 my $block = $self->paint('# ' . join(' ', @tokens), $palette{usage});
112 23 ▶ $block .= "\n" . $self->paint('# ' . $level->description, $palette{description}) if $level->description ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $level->description ne "" 5 18 23
113 23 return $block;
114 11 }
115  
116 57 ▶ method sectionHeader($title) {
Subroutine sectionHeader: called CC=1 SCAR=0.0
Pod undocumented
117 57 return $self->paint('## ' . $title, $palette{section});
118 11 }
119  
120 6 ▶ method argsBlock($args) {
Subroutine argsBlock: called CC=5 SCAR=16.1
Pod undocumented
121 6 my @lines = ($self->sectionHeader('Arguments'));
122 6 foreach my $arg ($args->@*) {
123 9 my @parts;
124 9 ▶ push @parts, ['[REQ]', $palette{annotation}] if $arg->required;
Branch: 1 branch
BranchTrueFalseTotal
if $arg->required 4 5 9
125 9 push @parts, map { [sprintf('[%s]', $_), $palette{annotation}] } $arg->type->constraintNotes;
126 9 ▶ push @parts, [$arg->help, undef] if $arg->help ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $arg->help ne "" 6 3 9
127 9 ▶ push @parts, [sprintf('[%s]', $arg->typeLabel), $palette{typeLabel}] if defined $arg->typeLabel;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg->typeLabel 7 2 9
128 9 push @lines, $self->entryLines($self->argLabel($arg), \@parts);
129 }
130 6 return join("\n", @lines);
131 11 }
132  
133 23 ▶ method optionBlocks($options) {
Subroutine optionBlocks: called CC=9 SCAR=22.0
Pod undocumented
134 23 my %grouped;
135 23 push $grouped{$_->group}->@*, $_ foreach $options->@*;
136  
137 23 my @blocks;
138 23 foreach my $group (sort keys %grouped) {
139 49 my @lines = ($self->sectionHeader($group));
140 49 foreach my $option ($grouped{$group}->@*) {
141 95 my @parts;
142 95 ▶ push @parts, ['[REQ]', $palette{annotation}] if $option->required;
Branch: 1 branch
BranchTrueFalseTotal
if $option->required 3 92 95
143 95 push @parts, map { [sprintf('[%s]', $_), $palette{annotation}] } $option->type->constraintNotes;
144 95 ▶ push @parts, [$option->help, undef] if $option->help ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $option->help ne "" 65 30 95
145 95 ▶ push @parts, [sprintf('[%s]', $option->typeLabel), $palette{typeLabel}] if defined $option->typeLabel;
Branch: 1 branch
BranchTrueFalseTotal
if defined $option->typeLabel 14 81 95
146 95 push @lines, $self->entryLines($self->optionLabel($option), \@parts);
147  
148 95 my $subIndent = ' ' x ($self->labelWidth + 4);
149 push @lines, $subIndent . $self->paint('Valid', $palette{subKey})
150 95 ▶ . ' = [ ' . join(', ', map { $self->paint($_, $palette{validValue}) } $option->valid->@*) . ' ]'
Branch: 1 branch
BranchTrueFalseTotal
if ref $option->valid eq "ARRAY" 28 67 95
151 if ref $option->valid eq 'ARRAY';
152 push @lines, $subIndent . $self->paint('Default', $palette{subKey})
153 . ' = ' . $self->paint($self->stringifyDefault($option->presentedDefault), $palette{defaultValue})
154 95 ▶ if $self->showsDefault($option);
Branch: 1 branch
BranchTrueFalseTotal
if $self->showsDefault($option) 17 78 95
155 }
156 49 push @blocks, join("\n", @lines);
157 }
158 23 return @blocks;
159 11 }
160  
161 2 ▶ method commandsBlock() {
Subroutine commandsBlock: called CC=2 SCAR=6.9
Pod undocumented
162 2 my @lines = ($self->sectionHeader('Commands'));
163 2 foreach my $name ($level->commandNames) {
164 4 push @lines, $self->entryLines($name, [[$level->command($name)->description, undef]]);
165 }
166 2 return join("\n", @lines);
167 11 }
168  
169 3 ▶ method examplesBlock() {
Subroutine examplesBlock: called CC=4 SCAR=13.9
Pod undocumented
170 3 my @lines = ($self->paint('# Examples:', $palette{usage}));
171 3 foreach my $example ($level->examples) {
172 3 push @lines, $self->paint('## ' . $example->{text}, $palette{section});
173 3 push @lines, $self->paint('##', $palette{section}) . ' ' . join(' ', $programName, @commandPath, $example->{args});
174 3 push @lines, '';
175 }
176 3 ▶ pop @lines while @lines && $lines[-1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
@lines and $lines[-1] eq "" and do { pop @lines } 3 3 6
Condition66%2/3
@lines and $lines[-1] eq ""
!ll&&!rl&&r
033
MC/DC50%1/2
@lines and $lines[-1] eq ""
@lines $lines[-1] eq ""
Truth table66%2/3
A and B
A= @linesB= $lines[-1] eq ""
ABresult
0X0
100
111
177 3 return join("\n", @lines);
178 11 }
179  
180 # An undefined default, or an empty list or mapping, is the same as none
181 # to the reader of the help.
182 95 ▶ method showsDefault($option) {
Subroutine showsDefault: called CC=7 SCAR=19.5
Pod undocumented
183 95 ▶ return 0 if !$option->hasDefault;
Branch: 1 branch
BranchTrueFalseTotal
unless $option->hasDefault 73 22 95
184  
185 22 my $default = $option->presentedDefault;
186 22 ▶ return 0 if !defined $default;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default 3 19 22
187 19 ▶ return 0 if ref $default eq 'ARRAY' && !$default->@*;
Branch: 1 branch
BranchTrueFalseTotal
if ref $default eq "ARRAY" and not @$default 1 18 19
Condition100%3/3
ref $default eq "ARRAY" and not @$default
!ll&&!rl&&r
1531
MC/DC100%2/2
ref $default eq "ARRAY" and not @$default
ref $default eq "ARRAY" not @$default
Truth table100%3/3
A and not(B)
A= ref $default eq "ARRAY"B= not @$default
ABresult
0X0
100
111
188 18 ▶ return 0 if ref $default eq 'HASH' && !$default->%*;
Branch: 1 branch
BranchTrueFalseTotal
if ref $default eq "HASH" and not %$default 1 17 18
Condition100%3/3
ref $default eq "HASH" and not %$default
!ll&&!rl&&r
1431
MC/DC100%2/2
ref $default eq "HASH" and not %$default
ref $default eq "HASH" not %$default
Truth table100%3/3
A and not(B)
A= ref $default eq "HASH"B= not %$default
ABresult
0X0
100
111
189 17 return 1;
190 11 }
191  
192 184 ▶ method optionLabel($option) {
Subroutine optionLabel: called CC=5 SCAR=16.1
Pod undocumented
193 184 my $label = join(', ', $self->optionSpellings($option));
194 184 ▶ return $label . ' <key=value>' if $option->hash;
Branch: 1 branch
BranchTrueFalseTotal
if $option->hash 14 170 184
195 170 ▶ return $label . ' <N.key=value>' if $option->objectlist;
Branch: 1 branch
BranchTrueFalseTotal
if $option->objectlist 6 164 170
196 164 ▶ return $label . ' <a,b,...>' if $option->csv;
Branch: 1 branch
BranchTrueFalseTotal
if $option->csv 6 158 164
197 158 ▶ return $label . ' <>' if $option->type->takesValue;
Branch: 1 branch
BranchTrueFalseTotal
if $option->type->takesValue 134 24 158
198 24 return $label;
199 11 }
200  
201 # Every name of $option as it is typed, the Primary name first. A
202 # negatable option shows its negation on the first long name, or after
203 # the names when all of them are single letters.
204 184 ▶ method optionSpellings($option) {
Subroutine optionSpellings: called CC=5 SCAR=16.1
Pod undocumented
205 184 my @names = ($option->name, $option->aliases);
206 184 ▶ return map { optionSpelling($_) } @names if !$option->negatable;
Branch: 1 branch
BranchTrueFalseTotal
unless $option->negatable 170 14 184
207  
208 14 my ($negatedName) = grep { length > 1 } @names;
209 14 ▶ my @spellings = map { defined $negatedName && $_ eq $negatedName ? '--[no-]' . $_ : optionSpelling($_) } @names;
Branch: 1 branch
BranchTrueFalseTotal
defined $negatedName && $_ eq $negatedName ? : 12 8 20
Condition100%3/3
defined $negatedName and $_ eq $negatedName
!ll&&!rl&&r
4412
MC/DC100%2/2
defined $negatedName and $_ eq $negatedName
defined $negatedName $_ eq $negatedName
Truth table100%3/3
A and B
A= defined $negatedNameB= $_ eq $negatedName
ABresult
0X0
100
111
210 14 ▶ push @spellings, '--no-' . $option->name if !defined $negatedName;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $negatedName 2 12 14
211 14 return @spellings;
212 11 }
213  
214 18 ▶ method argLabel($arg) {
Subroutine argLabel: called CC=1 SCAR=0.0
Pod undocumented
215 18 return sprintf('<%s>', $arg->short);
216 11 }
217  
218 # $parts is an arrayref of [text, ansiCode] pairs; wrapping happens on the
219 # plain text and colors are substituted in afterwards, so escape codes
220 # never distort the width computation or the column alignment.
221 108 ▶ method entryLines($label, $parts) {
Subroutine entryLines: called CC=3 SCAR=11.0
Pod undocumented
222 108 my $text = join(' ', grep { length } map { $_->[0] } $parts->@*);
223 108 my $padded = sprintf(' %-*s', $self->labelWidth - 3, $label);
224  
225 108 my $block;
226 108 ▶ if ($text eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($text eq '') { } 31 77 108
227 31 $block = $padded =~ s/\s+$//r;
228 }
229 else {
230 # Text::Wrap widens its columns to fit the indent itself, with a
231 # warning; do it up front so narrow terminals stay quiet.
232 77 local $Text::Wrap::columns = max($self->width, $self->labelWidth + 2);
233 77 local $Text::Wrap::huge = 'overflow';
234 77 local $Text::Wrap::unexpand = 0;
235 77 $block = Text::Wrap::wrap($padded, ' ' x $self->labelWidth, $text);
236 }
237  
238 108 ▶ return $block if !$self->color;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->color 92 16 108
239 16 return $self->paintParts($block, $parts, length $padded);
240 11 }
241  
242 # Parts are painted in occurrence order, each searched only past the previous
243 # one, so a part whose text also appears in an earlier part never steals the
244 # paint. A part the wrapping broke across lines is painted word by word.
245 16 ▶ method paintParts($block, $parts, $searchFrom) {
Subroutine paintParts: called CC=6 SCAR=17.9
Pod undocumented
246 16 foreach my $part (grep { length $_->[0] } $parts->@*) {
247 26 ▶ my @pieces = index($block, $part->[0], $searchFrom) < 0 ? grep { length } split(/\s+/, $part->[0]) : ($part->[0]);
Branch: 1 branch
BranchTrueFalseTotal
index($block, $part->[0], $searchFrom) == -1 ? : 3 23 26
248 26 foreach my $piece (@pieces) {
249 42 my $at = index($block, $piece, $searchFrom);
250 42 ▶ next if $at < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $at < 0 0 42 42
251 42 ▶ my $painted = defined $part->[1] ? $self->paint($piece, $part->[1]) : $piece;
Branch: 1 branch
BranchTrueFalseTotal
defined $part->[1] ? : 12 30 42
252 42 substr($block, $at, length $piece) = $painted;
253 42 $searchFrom = $at + length $painted;
254 }
255 }
256 16 return $block;
257 11 }
258  
259 23 ▶ method stringifyDefault($default) {
Subroutine stringifyDefault: called CC=3 SCAR=11.0
Pod undocumented
260 23 ▶ return $default if !ref $default;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $default 11 12 23
261 12 ▶ return join(', ', map { sprintf('%s=%s', $_, $default->{$_}) } sort keys $default->%*) if ref $default eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $default eq "HASH" 9 3 12
262 3 return join(', ', map { $self->stringifyEntry($default->[$_], $_) } 0 .. $#$default);
263 11 }
264  
265 # A list entry: the value itself, or for an objectlist the entry's pairs
266 # prefixed with its index.
267 6 ▶ method stringifyEntry($entry, $index) {
Subroutine stringifyEntry: called CC=2 SCAR=7.0
Pod undocumented
268 6 ▶ return $entry if ref $entry ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $entry ne "HASH" 0 6 6
269 6 return $self->stringifyDefault({ map { (sprintf('%d.%s', $index, $_) => $entry->{$_}) } keys $entry->%* });
270 11 }
271 }
272  
273 11 1;
274  
275 __END__
« Pad/Registry.pm ↑ Summary Pad/Config.pm »