Pad/Help.pm

filter: Statementstmt 93.4% 241 / 258 Branchbran 88.5% 62 / 70 Conditioncond 76.0% 19 / 25 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 89.6% 26 / 29 Podpod 28.5% 6 / 21 totaltotal 86.3% 360 / 417 CC 54 SCAR 15.9
CC
54
Cov
91%
optionBlocks
10.0
detectWidth
8.5
headerBlock
7.0
CRAP 4.9
« Pad/Util.pm ↑ Summary Pad/Registry.pm »
1 10 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 class Getopt::Pad::Help :strict(params) {
5 10 ▶ use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶ use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶ use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶ use Getopt::Pad::Util qw(useColor);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶ use Text::Wrap ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 our $VERSION = '0.05';
12  
13 10 field $level :param;
14 field $version :param = undef;
15 10 field $programName :param = undef;
16 10 field $width :param = undef;
17 10 field $color :param = undef;
18 10 field $handle :param = \*STDOUT;
19  
20 10 field @commandPath;
21 10 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 73 ▶ 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
7030
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 311 ▶ 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
30704
MC/DC50%1/2
$color //= useColor($handle)
$color useColor($handle)
Truth table66%2/3
A //= B
A= $colorB= useColor($handle)
ABresult
1X1
011
000
33  
34 330 ▶ method labelWidth() {
Subroutine labelWidth: called CC=2 SCAR=6.9
Pod undocumented
35 330 ▶ 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
311190
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 19 my @labels = ((map { $self->optionLabel($_) } grep { !$_->hidden } $level->options), (map { $self->argLabel($_) } $level->args), $level->commandNames);
37 19 3 + max(0, map { length } @labels) + 5;
38 };
39 10 }
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 10 }
56  
57 213 ▶ method paint($text, $ansiCode) {
Subroutine paint: called CC=2 SCAR=6.9
Pod undocumented
58 213 ▶ return $self->color ? sprintf("\e[%sm%s\e[0m", $ansiCode, $text) : $text;
Branch: 1 branch
BranchTrueFalseTotal
$self->color ? : 48 165 213
59 10 }
60  
61 0 ▶ method printHelp() {
Subroutine printHelp: not called CC=1 SCAR=6.9
Pod documented
62 0 print {$handle} $self->renderHelp;
63 0 return $self;
64 10 }
65  
66 0 ▶ method printVersion() {
Subroutine printVersion: not called CC=1 SCAR=6.9
Pod documented
67 0 print {$handle} $self->renderVersion, "\n";
68 0 return $self;
69 10 }
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 10 }
75  
76 21 ▶ method renderHelp() {
Subroutine renderHelp: called CC=5 SCAR=16.1
Pod documented
77 21 my @visibleOptions = grep { !$_->hidden } $level->options;
78 21 my @args = $level->args;
79  
80 21 my @blocks = ($self->headerBlock(\@visibleOptions, \@args));
81 21 ▶ push @blocks, $self->argsBlock(\@args) if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 5 16 21
82 21 ▶ push @blocks, $self->optionBlocks(\@visibleOptions) if @visibleOptions;
Branch: 1 branch
BranchTrueFalseTotal
if @visibleOptions 21 0 21
83 21 ▶ push @blocks, $self->commandsBlock if $level->hasCommands;
Branch: 1 branch
BranchTrueFalseTotal
if $level->hasCommands 2 19 21
84 21 ▶ push @blocks, $self->examplesBlock if $level->examples;
Branch: 1 branch
BranchTrueFalseTotal
if $level->examples 3 18 21
85  
86 21 return join("\n\n", @blocks) . "\n";
87 10 }
88  
89 10 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 21 ▶ method headerBlock($options, $args) {
Subroutine headerBlock: called CC=7 SCAR=19.5
Pod undocumented
101 21 my @tokens = ($programName, @commandPath);
102 21 ▶ push @tokens, '[options]' if $options->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @$options 21 0 21
103 21 ▶ push @tokens, '<command>' if $level->hasCommands;
Branch: 1 branch
BranchTrueFalseTotal
if $level->hasCommands 2 19 21
104 21 foreach my $arg ($args->@*) {
105 8 my $token = $arg->short;
106 8 ▶ $token .= '...' if $arg->multiple;
Branch: 1 branch
BranchTrueFalseTotal
if $arg->multiple 0 8 8
107 8 ▶ $token = sprintf('[%s]', $token) if !$arg->required;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->required 4 4 8
108 8 push @tokens, $token;
109 }
110  
111 21 my $block = $self->paint('# ' . join(' ', @tokens), $palette{usage});
112 21 ▶ $block .= "\n" . $self->paint('# ' . $level->description, $palette{description}) if $level->description ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $level->description ne "" 5 16 21
113 21 return $block;
114 10 }
115  
116 52 ▶ method sectionHeader($title) {
Subroutine sectionHeader: called CC=1 SCAR=0.0
Pod undocumented
117 52 return $self->paint('## ' . $title, $palette{section});
118 10 }
119  
120 5 ▶ method argsBlock($args) {
Subroutine argsBlock: called CC=5 SCAR=16.1
Pod undocumented
121 5 my @lines = ($self->sectionHeader('Arguments'));
122 5 foreach my $arg ($args->@*) {
123 8 my @parts;
124 8 ▶ push @parts, ['[REQ]', $palette{annotation}] if $arg->required;
Branch: 1 branch
BranchTrueFalseTotal
if $arg->required 4 4 8
125 8 ▶ push @parts, [$arg->help, undef] if $arg->help ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $arg->help ne "" 6 2 8
126 8 ▶ push @parts, [sprintf('[%s]', $arg->typeLabel), $palette{typeLabel}] if defined $arg->typeLabel;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg->typeLabel 6 2 8
127 8 push @lines, $self->entryLines($self->argLabel($arg), \@parts);
128 }
129 5 return join("\n", @lines);
130 10 }
131  
132 21 ▶ method optionBlocks($options) {
Subroutine optionBlocks: called CC=10 SCAR=23.0
Pod undocumented
133 21 my %grouped;
134 21 push $grouped{$_->group}->@*, $_ foreach $options->@*;
135  
136 21 my @blocks;
137 21 foreach my $group (sort keys %grouped) {
138 45 my @lines = ($self->sectionHeader($group));
139 45 foreach my $option ($grouped{$group}->@*) {
140 86 my @parts;
141 86 ▶ push @parts, ['[REQ]', $palette{annotation}] if $option->required;
Branch: 1 branch
BranchTrueFalseTotal
if $option->required 3 83 86
142 86 push @parts, map { [sprintf('[%s]', $_), $palette{annotation}] } $option->type->constraintNotes;
143 86 ▶ push @parts, [$option->help, undef] if $option->help ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $option->help ne "" 63 23 86
144 86 ▶ push @parts, [sprintf('[%s]', $option->typeLabel), $palette{typeLabel}] if defined $option->typeLabel;
Branch: 1 branch
BranchTrueFalseTotal
if defined $option->typeLabel 13 73 86
145 86 push @lines, $self->entryLines($self->optionLabel($option), \@parts);
146  
147 86 my $subIndent = ' ' x ($self->labelWidth + 4);
148 push @lines, $subIndent . $self->paint('Valid', $palette{subKey})
149 86 ▶ . ' = [ ' . join(', ', map { $self->paint($_, $palette{validValue}) } $option->valid->@*) . ' ]'
Branch: 1 branch
BranchTrueFalseTotal
if ref $option->valid eq "ARRAY" 26 60 86
150 if ref $option->valid eq 'ARRAY';
151 # An undefined default is the same as none to the reader of the help.
152 push @lines, $subIndent . $self->paint('Default', $palette{subKey})
153 . ' = ' . $self->paint($self->stringifyDefault($option->default), $palette{defaultValue})
154 86 ▶ if $option->hasDefault && defined $option->default;
Branch: 1 branch
BranchTrueFalseTotal
if $option->hasDefault and defined $option->default 15 71 86
Condition100%3/3
$option->hasDefault and defined $option->default
!ll&&!rl&&r
68315
MC/DC100%2/2
$option->hasDefault and defined $option->default
$option->hasDefault defined $option->default
Truth table100%3/3
A and B
A= $option->hasDefaultB= defined $option->default
ABresult
0X0
100
111
155 }
156 45 push @blocks, join("\n", @lines);
157 }
158 21 return @blocks;
159 10 }
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 10 }
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 10 }
179  
180 166 ▶ method optionLabel($option) {
Subroutine optionLabel: called CC=6 SCAR=17.9
Pod undocumented
181 166 ▶ my $label = sprintf($option->negatable ? '--[no-]%s' : '--%s', $option->name);
Branch: 1 branch
BranchTrueFalseTotal
$option->negatable ? : 10 156 166
182 166 ▶ return $label . ' <key=value>' if $option->hash;
Branch: 1 branch
BranchTrueFalseTotal
if $option->hash 12 154 166
183 154 ▶ return $label . ' <N.key=value>' if $option->objectlist;
Branch: 1 branch
BranchTrueFalseTotal
if $option->objectlist 6 148 154
184 148 ▶ return $label . ' <a,b,...>' if $option->csv;
Branch: 1 branch
BranchTrueFalseTotal
if $option->csv 6 142 148
185 142 ▶ return $label . ' <>' if $option->type->takesValue;
Branch: 1 branch
BranchTrueFalseTotal
if $option->type->takesValue 124 18 142
186 18 return $label;
187 10 }
188  
189 16 ▶ method argLabel($arg) {
Subroutine argLabel: called CC=1 SCAR=0.0
Pod undocumented
190 16 return sprintf('<%s>', $arg->short);
191 10 }
192  
193 # $parts is an arrayref of [text, ansiCode] pairs; wrapping happens on the
194 # plain text and colors are substituted in afterwards, so escape codes
195 # never distort the width computation or the column alignment.
196 98 ▶ method entryLines($label, $parts) {
Subroutine entryLines: called CC=3 SCAR=11.0
Pod undocumented
197 98 my $text = join(' ', grep { length } map { $_->[0] } $parts->@*);
198 98 my $padded = sprintf(' %-*s', $self->labelWidth - 3, $label);
199  
200 98 my $block;
201 98 ▶ if ($text eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($text eq '') { } 25 73 98
202 25 $block = $padded =~ s/\s+$//r;
203 }
204 else {
205 # Text::Wrap widens its columns to fit the indent itself, with a
206 # warning; do it up front so narrow terminals stay quiet.
207 73 local $Text::Wrap::columns = max($self->width, $self->labelWidth + 2);
208 73 local $Text::Wrap::huge = 'overflow';
209 73 local $Text::Wrap::unexpand = 0;
210 73 $block = Text::Wrap::wrap($padded, ' ' x $self->labelWidth, $text);
211 }
212  
213 98 ▶ return $block if !$self->color;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->color 82 16 98
214 16 return $self->paintParts($block, $parts, length $padded);
215 10 }
216  
217 # Parts are painted in occurrence order, each searched only past the previous
218 # one, so a part whose text also appears in an earlier part never steals the
219 # paint. A part the wrapping broke across lines is painted word by word.
220 16 ▶ method paintParts($block, $parts, $searchFrom) {
Subroutine paintParts: called CC=6 SCAR=17.9
Pod undocumented
221 16 foreach my $part (grep { length $_->[0] } $parts->@*) {
222 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
223 26 foreach my $piece (@pieces) {
224 42 my $at = index($block, $piece, $searchFrom);
225 42 ▶ next if $at < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $at < 0 0 42 42
226 42 ▶ my $painted = defined $part->[1] ? $self->paint($piece, $part->[1]) : $piece;
Branch: 1 branch
BranchTrueFalseTotal
defined $part->[1] ? : 12 30 42
227 42 substr($block, $at, length $piece) = $painted;
228 42 $searchFrom = $at + length $painted;
229 }
230 }
231 16 return $block;
232 10 }
233  
234 21 ▶ method stringifyDefault($default) {
Subroutine stringifyDefault: called CC=3 SCAR=11.0
Pod undocumented
235 21 ▶ return $default if !ref $default;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $default 9 12 21
236 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
237 3 return join(', ', map { $self->stringifyEntry($default->[$_], $_) } 0 .. $#$default);
238 10 }
239  
240 # A list entry: the value itself, or for an objectlist the entry's pairs
241 # prefixed with its index.
242 6 ▶ method stringifyEntry($entry, $index) {
Subroutine stringifyEntry: called CC=2 SCAR=7.0
Pod undocumented
243 6 ▶ return $entry if ref $entry ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $entry ne "HASH" 0 6 6
244 6 return $self->stringifyDefault({ map { (sprintf('%d.%s', $index, $_) => $entry->{$_}) } keys $entry->%* });
245 10 }
246 }
247  
248 10 1;
249  
250 __END__
« Pad/Util.pm ↑ Summary Pad/Registry.pm »