blib/lib/App/Pod.pm

filter: Statementstmt 91.8% 475 / 517 Branchbran 77.9% 134 / 172 Conditioncond 54.0% 20 / 37 MC/DCmcdc 29.1% 7 / 24 Subroutinesub 96.5% 83 / 86 Podpod 100% 15 / 15 totaltotal 86.2% 734 / 851 CC 116 SCAR 15.2
CC
116
Cov
87%
trim
10.0
show_methods
9.1
_init
9.0
CRAP 4.6
↑ Summary
1 package App::Pod;
2  
3 3 ▶use v5.24; # Postfix deref :)
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Pod::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Module::CoreList();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Getopt::Long qw( GetOptions );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Module::Functions qw( get_full_functions );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::HomeDir qw( home );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Basename qw( basename );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use File::Spec::Functions qw( catfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use List::Util qw( first max );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Mojo::File qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Mojo::JSON qw( j );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Perl::OSType qw( os_type );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Term::ANSIColor qw( colored );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Carp qw( cluck );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use open qw( :std :utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use subs qw( _sayt uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # Catch eval warnings better.
23 0 ▶$SIG{__WARN__} = sub { cluck shift };
Subroutine __ANON__: not called CC=1 SCAR=1.2
24  
25 =head1 LOGO
26  
27 ~ __ ~
28 ~ ____ ____ ____/ / ~
29 ~ / __ \/ __ \/ __ / ~
30 ~ / /_/ / /_/ / /_/ / ~
31 ~ / .___/\____/\__,_/ ~
32 ~ /_/ ~
33  
34 =head1 NAME
35  
36 App::Pod - Quickly show available class methods and documentation.
37  
38 =cut
39  
40 3 our $VERSION = '0.40';
41  
42  
43 =head1 SYNOPSIS
44  
45 View summary of Mojo::UserAgent:
46  
47 % pod Mojo::UserAgent
48  
49 View summary of a specific method.
50  
51 % pod Mojo::UserAgent get
52  
53 Edit the module
54  
55 % pod Mojo::UserAgent -e
56  
57 Edit the module and jump to the specific method definition right away.
58 (Press "n" to next match if neeeded).
59  
60 % pod Mojo::UserAgent get -e
61  
62 Run perldoc on the module (for convenience).
63  
64 % pod Mojo::UserAgent -d
65  
66 List all available methods.
67 If no methods are found normally, then this will automatically be enabled.
68 (pod was made to work with Mojo pod styling).
69  
70 % pod Mojo::UserAgent -a
71  
72 List all Module::Build actions.
73  
74 % pod Module::Build --query head1=ACTIONS/item-text
75  
76 Can do the same stuff with a file
77  
78 % pod my.pod --query head1
79  
80 Show help.
81  
82 % pod
83 % pod -h
84  
85  
86 =head1 DESCRIPTION
87  
88 Basically, this is a tool that can quickly summarize the contents of a perl module.
89  
90 =head1 SUBROUTINES/METHODS
91  
92 =cut
93  
94 #
95 # Method maker
96 #
97  
98 =head2 _has
99  
100 Generates class accessor methods (like Mojo::Base::attr)
101  
102 =cut
103  
104 3 ▶sub _has {
Subroutine _has: called CC=3 SCAR=11.0
105 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
106 3 for my $attr ( @_ ) {
107 1077 ▶ *$attr = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
108 1077 ▶ return $_[0]{$attr} if @_ == 1; # Get: return $self-<{$attr}
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 698 379 1077
109 379 $_[0]{$attr} = $_[1]; # Set: $self->{$attr} = $val
110 379 $_[0]; # return $self
111 }
112 48 ▶ if not defined &$attr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &$attr 48 0 48
113 }
114 }
115  
116 3 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
117 3 _has qw(
118 _class
119 _args
120 _method
121 _opts
122 _core_flags
123 _non_main_flags
124 _cache_from_file
125 _cache_pod
126 _cache_path
127 _cache_name_and_summary
128 _cache_version
129 _cache_isa
130 _cache_events
131 _cache_methods
132 _cache_method_and_doc
133 _dirty_cache
134 );
135 }
136  
137 #
138 # Debug
139 #
140  
141 2 ▶sub _dumper {
Subroutine _dumper: called CC=2 SCAR=7.6
142 2 require Data::Dumper;
143 2 my $data = Data::Dumper
144 ->new( [@_] )
145 ->Indent( 1 )
146 ->Sortkeys( 1 )
147 ->Terse( 1 )
148 ->Useqq( 1 )
149 ->Dump;
150 2 ▶ return $data if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 2 0 2
151 0 say $data;
152 }
153  
154 #
155 # Run
156 #
157  
158 =head2 run
159  
160 Run the main program.
161  
162 use App::Pod;
163 App::Pod->run;
164  
165 Or just use the included script:
166  
167 % pod
168  
169 =cut
170  
171 32 ▶sub run {
Subroutine run: called CC=5 SCAR=16.1
Pod documented
172 32 my $self = __PACKAGE__->_new;
173  
174 32 ▶ return if $self->_process_core_flags;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_process_core_flags 4 28 32
175 28 ▶ return if $self->_abort;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_abort 10 18 28
176  
177 18 ▶ if ( $self->_non_main_flags->@* ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_non_main_flags}) { } 12 6 18
178 12 $self->_process_non_main;
179 }
180 else {
181 6 $self->_process_main;
182 }
183  
184 18 $self->_dump();
185 18 ▶ $self->store_cache if $self->_dirty_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_dirty_cache 8 10 18
186 }
187  
188 32 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
189 32 my ( $class ) = @_;
190 32 my $self = bless {}, $class;
191  
192 32 $self->_init;
193  
194 32 $self;
195 }
196  
197 32 ▶sub _init {
Subroutine _init: called CC=9 SCAR=22.0
198 32 my ( $self ) = @_;
199  
200 # Show help when no input.
201 32 ▶ @ARGV = ( "--help" ) if not @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 1 31 32
202  
203 32 my $o = _get_opts();
204 32 my ( $class, @args ) = @ARGV;
205  
206 32 $self->_opts( $o );
207 32 $self->_class( $class );
208 32 $self->_args( \@args );
209 32 $self->_method( $args[0] );
210  
211 32 my @core_flags;
212 my @non_main_flags;
213  
214 32 for ( $self->_define_spec() ) {
215  
216 # We are using the option and it has a handler.
217 384 ▶ next unless $o->{ $_->{name} } and $_->{handler};
Branch: 1 branch
BranchTrueFalseTotal
unless $$o{$_->{"name"}} and $_->{"handler"} 353 31 384
Condition100%3/3
$$o{$_->{"name"}} and $_->{"handler"}
!ll&&!rl&&r
345831
MC/DC100%2/2
$$o{$_->{"name"}} and $_->{"handler"}
$$o{$_->{"name"}} $_->{"handler"}
Truth table100%3/3
A and B
A= $$o{$_->{"name"}}B= $_->{"handler"}
ABresult
0X0
100
111
218  
219 31 ▶ if ( $_->{core} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{'core'}) { } 9 22 31
220 9 push @core_flags, $_;
221 }
222 else {
223 22 push @non_main_flags, $_;
224 }
225 }
226  
227 # Core flags.
228 # These do not need any error checks
229 # and will be processed early.
230 32 $self->_core_flags( \@core_flags );
231  
232 # Non main flags.
233 # These are features separate from the main program.
234 32 $self->_non_main_flags( \@non_main_flags );
235  
236 # Explicitly force getting the real data.
237 32 ▶ $self->_dirty_cache( 1 ) if $o->{flush_cache};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"flush_cache"} 1 31 32
238  
239 # Not sure how to handle colors in windows.
240 32 ▶ $self->_no_colors() if $self->_opts->{no_colors} or os_type eq "Windows";
Branch: 1 branch
BranchTrueFalseTotal
if $self->_opts->{"no_colors"} or os_type() eq "Windows" 1 31 32
Condition66%2/3
$self->_opts->{"no_colors"} or os_type() eq "Windows"
l!l&&r!l&&!r
1031
MC/DC50%1/2
$self->_opts->{"no_colors"} or os_type() eq "Windows"
$self->_opts->{"no_colors"} os_type() eq "Windows"
Truth table66%2/3
A or B
A= $self->_opts->{"no_colors"}B= os_type() eq "Windows"
ABresult
1X1
011
000
241 }
242  
243 1 ▶sub _no_colors {
Subroutine _no_colors: called CC=2 SCAR=6.9
244 1 my @colors = qw(
245 _red
246 _yellow
247 _green
248 _grey
249 _neon
250 _reset
251 );
252  
253 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
254 3 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
255  
256 # Pass through the args.
257 1 for my $color ( @colors ) {
258 6 ▶ *$color = sub { "@_" };
Subroutine __ANON__: called CC=1 SCAR=0.0
259 }
260 }
261  
262 18 ▶sub _dump {
Subroutine _dump: called CC=7 SCAR=19.7
263 18 my ( $self ) = @_;
264 18 ▶ my $dump = $self->_opts->{dump} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dump = $self->_opts->{"dump"} or return 2 16 18
265 2 my $data;
266  
267 2 ▶ if ( $dump >= 2 ) { # Dump all.
Branch: 2 branches
BranchTrueFalseTotal
if ($dump >= 2) { } 0 2 2
elsif ($dump >= 1) { } 2 0 2
268 0 $data = $self;
269 }
270 elsif ( $dump >= 1 ) { # Skip lol and tree.
271 2 $data = {%$self}; # Shallow copy.
272 2 for ( keys %$data ) { # Keep the dump simple.
273 18 ▶ delete $data->{$_} if /^_cache_/ and !/path/;
Branch: 1 branch
BranchTrueFalseTotal
if /^_cache_/u and not /path/u 4 14 18
Condition100%3/3
/^_cache_/u and not /path/u
!ll&&!rl&&r
1224
MC/DC100%2/2
/^_cache_/u and not /path/u
/^_cache_/u not /path/u
Truth table100%3/3
A and not(B)
A= /^_cache_/uB= not /path/u
ABresult
0X0
100
111
274 }
275 }
276  
277 2 say "self=" . _dumper $data;
278 }
279  
280 # Spec
281  
282 72 ▶sub _define_spec {
Subroutine _define_spec: called CC=2 SCAR=6.9
283 72 my @spec = (
284  
285 # If given a handler, will be auto processed.
286 # Core options will be processed early.
287  
288 # Core.
289 {
290 spec => "help|h",
291 description => "Show this help section.",
292 handler => "_show_help",
293 core => 1,
294 },
295 {
296 spec => "version|v",
297 description => "Show this tool version.",
298 handler => "_show_version",
299 core => 1,
300 },
301 {
302 spec => "tool_options|to",
303 description => "List tool options.",
304 handler => "list_tool_options",
305 core => 1,
306 },
307  
308 # Non main.
309 {
310 spec => "class_options|co",
311 description => "Class events and methods.",
312 handler => "list_class_options",
313 },
314 {
315 spec => "doc|d",
316 description => "View class documentation.",
317 handler => "doc_class",
318 },
319 {
320 spec => "edit|e",
321 description => "Edit the source code.",
322 handler => "edit_class",
323 },
324 {
325 spec => "query|q=s",
326 description => "Run a pod query.",
327 handler => "query_class",
328 },
329 {
330 spec => "dump|dd+",
331 description => "Dump extra info (adds up).",
332 core => 1,
333 },
334 {
335 spec => "all|a",
336 description => "Show all class functions.",
337 },
338 {
339 spec => "no_colors",
340 description => "Do not output colors.",
341 },
342 {
343 spec => "no_error",
344 description => "Suppress some error message.",
345 },
346 {
347 spec => "flush_cache|f",
348 description => "Flush cache file(s).",
349 },
350 );
351  
352 # Add the name.
353 72 for ( @spec ) {
354 864 $_->{name} = $_->{spec} =~ s/\|.+//r;
355 }
356  
357 72 @spec;
358 }
359  
360 38 ▶sub _get_spec_list {
Subroutine _get_spec_list: called CC=1 SCAR=0.0
361 38 map { $_->{spec} } _define_spec();
362 }
363  
364 32 ▶sub _get_opts {
Subroutine _get_opts: called CC=2 SCAR=7.1
365 32 my $opts = {};
366  
367 32 ▶ GetOptions( $opts, _get_spec_list() ) or die "$!\n";
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions($opts, _get_spec_list()) or die "$!\n" 32 0 32
368  
369 32 $opts;
370 }
371  
372 79 ▶sub _get_pod {
Subroutine _get_pod: called CC=2 SCAR=6.9
373 79 my ( $self ) = @_;
374  
375 # Use in-memory cache if present.
376 79 my $pod = $self->_cache_pod;
377 79 ▶ return $pod if $pod;
Branch: 1 branch
BranchTrueFalseTotal
if $pod 58 21 79
378  
379 # Otherwise, make a new Pod::Query object.
380 21 $pod = Pod::Query->new( $self->_class );
381  
382 # Cache it in-memory.
383 21 $self->_cache_pod( $pod );
384  
385 21 $pod;
386 }
387  
388 #
389 # Core
390 #
391  
392 32 ▶sub _process_core_flags {
Subroutine _process_core_flags: called CC=4 SCAR=13.9
393 32 my ( $self ) = @_;
394  
395 32 for ( $self->_core_flags->@* ) {
396 9 ▶ say "Processing: $_->{name}" if $self->_opts->{dump};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_opts->{"dump"} 0 9 9
397 9 my $handler = $_->{handler};
398 9 ▶ return 1 if $self->$handler;
Branch: 1 branch
BranchTrueFalseTotal
if $self->$handler 4 5 9
399 }
400  
401 28 return 0;
402 }
403  
404 # Help
405  
406 2 ▶sub _show_help {
Subroutine _show_help: called CC=1 SCAR=0.0
407 2 my ( $self ) = @_;
408  
409 2 say $self->_process_template(
410 $self->_define_help_template,
411 $self->_build_help_options,
412 );
413  
414 2 return 1;
415 }
416  
417 2 ▶sub _define_help_template {
Subroutine _define_help_template: called CC=1 SCAR=0.0
418 2 <<"HELP";
419  
420 ##_neon:Syntax:
421 <SCRIPT> module_name [method_name] [options]
422  
423 ##_neon:Options:
424 <OPTIONS>
425  
426 ##_neon:Examples:
427 ##_grey:# All or a method
428 <SCRIPT> Mojo::UserAgent
429 <SCRIPT> Mojo::UserAgent prepare
430  
431 ##_grey:# Documentation
432 <SCRIPT> Mojo::UserAgent -d
433  
434 ##_grey:# Edit class or method
435 <SCRIPT> Mojo::UserAgent -e
436 <SCRIPT> Mojo::UserAgent prepare -e
437  
438 ##_grey:# List all methods
439 <SCRIPT> Mojo::UserAgent --class_options
440  
441 ##_grey:# List all Module::Build actions.
442 <SCRIPT> Module::Build --query head1=ACTIONS/item-text
443 HELP
444 }
445  
446 2 ▶sub _process_template {
Subroutine _process_template: called CC=2 SCAR=6.9
447 2 my ( $self, $template, $options ) = @_;
448 2 my $script = _yellow( "pod" );
449  
450 2 for ( $template ) {
451  
452 # Color.
453 2 s/ ^ \s* \K \#\#([\w_]+): (.*) / qq($1("$2")) /gmxee;
454  
455 # Expand <SCRIPT> tags.
456 2 s/<SCRIPT>/$script/g;
457  
458 # Expand <OPTIONS> tags.
459 2 s/<OPTIONS>/$options/g;
460 }
461  
462 2 $template;
463 }
464  
465 2 ▶sub _build_help_options {
Subroutine _build_help_options: called CC=1 SCAR=0.0
466 my @all = map {
467 2 my $opt = $_->{spec};
468 24 my $desc = $_->{description};
469 24 $opt =~ s/\+$//; # Trailing plus in option.
470 24 $opt =~ s/=\w+$//g; # Option parameter.
471 24 $opt =~ s/\|/, /g;
472 24 $opt =~ s/ (?=\b\w{2}) /--/gx; # Long opts
473 24 $opt =~ s/ (?=\b\w\b) /-/gx; # Short opts
474 24 my $colored_opt = _green( $opt );
475 24 [ $colored_opt, _grey( $desc ), length $colored_opt ];
476 } _define_spec();
477  
478 2 my $max = max map { $_->[2] } @all;
479 2 my $indent = " " x 2;
480  
481 my $options =
482 join "\n$indent",
483 2 map { sprintf "%-${max}s - %s", @$_[ 0, 1 ] } @all;
484  
485 2 $options;
486 }
487  
488 # Version
489  
490 1 ▶sub _show_version {
Subroutine _show_version: called CC=1 SCAR=0.0
491 1 my ( $self ) = @_;
492 1 my $version = $self->VERSION;
493  
494 1 say "pod (App::Pod) $version";
495  
496 1 return 1;
497 }
498  
499 # List
500  
501 =head2 list_tool_options
502  
503 Returns a list of the possible command line options
504 to this tool.
505  
506 =cut
507  
508 6 ▶sub list_tool_options {
Subroutine list_tool_options: called CC=3 SCAR=11.0
Pod documented
509 6 my ( $self ) = @_;
510  
511 say
512 6 for sort
513 132 ▶ map { length( $_ ) == 1 ? "-$_" : "--$_"; }
Branch: 1 branch
BranchTrueFalseTotal
length $_ == 1 ? : 42 90 132
514 132 map { s/\+$//r } # Options which are additive.
515 132 map { s/=\w+$//r } # Options which take values.
516 72 map { split /\|/ } _get_spec_list();
517  
518 # Abort if not using also --class_options.
519 6 not $self->_opts->{class_options};
520 }
521  
522 #
523 # Abort
524 #
525  
526 28 ▶sub _abort {
Subroutine _abort: called CC=7 SCAR=20.4
527 28 my ( $self ) = @_;
528 28 my $class = $self->_class;
529  
530 28 ▶ if ( not $class ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 6 22 28
531 6 ▶ if ( not $self->_opts->{no_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_opts->{"no_error"}) 4 2 6
532 4 say "";
533 4 say _red( "Class name not provided!" );
534 4 say _reset( "" );
535 }
536 6 return 1;
537 }
538  
539 # No wierd class names.
540 22 ▶ if ( $class !~ m{ ^ [-~\w_:\\/. ]+ $ }x ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class =~ m[ ^ [-~\w_:\\/. ]+ $ ]ux) 0 22 22
541 0 ▶ if ( not $self->_opts->{no_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_opts->{"no_error"}) 0 0 0
542 0 say "";
543 0 say _red( "Invalid class name: $class" );
544 0 say _reset( "" );
545 }
546 0 return 1;
547 }
548  
549 # Make sure the path is not empty (error signal from Pod::Query).
550 22 ▶ if ( not $self->_get_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_get_path) 4 18 22
551 4 ▶ if ( not $self->_opts->{no_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_opts->{"no_error"}) 3 1 4
552 3 say "";
553 3 say _red( "Class not found: $class" );
554 3 say _reset( "" );
555 }
556 4 return 1;
557 }
558  
559 18 return 0;
560 }
561  
562 37 ▶sub _get_path {
Subroutine _get_path: called CC=5 SCAR=16.1
563 37 my ( $self ) = @_;
564  
565 # Use in-memory cache if present.
566 37 my $mem_cache = $self->_cache_path;
567 37 ▶ return $mem_cache if $mem_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $mem_cache 15 22 37
568  
569 # Use disk cache if present.
570 22 my $disk_cache = $self->retrieve_cache;
571 22 ▶ if ( $disk_cache and $disk_cache->{path} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($disk_cache and $disk_cache->{"path"}) 11 11 22
Condition66%2/3
$disk_cache and $disk_cache->{"path"}
!ll&&!rl&&r
01111
MC/DC50%1/2
$disk_cache and $disk_cache->{"path"}
$disk_cache $disk_cache->{"path"}
Truth table66%2/3
A and B
A= $disk_cacheB= $disk_cache->{"path"}
ABresult
0X0
100
111
572 11 $self->_cache_path( $disk_cache->{path} );
573 11 return $disk_cache->{path};
574 }
575  
576 # Otherwise, get the class path.
577 11 ▶ local $Pod::Query::DEBUG_FIND_DUMP = 1 if $self->_opts->{dump};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_opts->{"dump"} 0 11 11
578 11 my $path = $self->_get_pod->path;
579  
580 # Cache it in-memory.
581 11 $self->_cache_path( $path );
582  
583 # Flag that disk cache should be stored later.
584 11 $self->_dirty_cache( 1 );
585  
586 11 $path;
587 }
588  
589 #
590 # Non Main
591 #
592  
593 12 ▶sub _process_non_main {
Subroutine _process_non_main: called CC=5 SCAR=16.1
594 12 my ( $self ) = @_;
595 12 ▶ say "_process_non_main()" if $self->_opts->{dump};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_opts->{"dump"} 2 10 12
596  
597 12 for ( $self->_non_main_flags->@* ) {
598 12 ▶ say "Processing: $_->{name}" if $self->_opts->{dump};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_opts->{"dump"} 2 10 12
599 12 my $handler = $_->{handler};
600 12 ▶ return 1 if $self->$handler;
Branch: 1 branch
BranchTrueFalseTotal
if $self->$handler 0 12 12
601 }
602 }
603  
604 # List
605  
606 =head2 list_class_options
607  
608 Shows a list of all the available class options
609 which may be methods, events, etc.
610  
611 (This is handy for making tab completion based on
612 a class.)
613  
614 =cut
615  
616 2 ▶sub list_class_options {
Subroutine list_class_options: called CC=3 SCAR=11.0
Pod documented
617 2 my ( $self ) = @_;
618  
619 # Use cache if available.
620 2 my $cache = $self->retrieve_cache();
621  
622 # Make class specific cache if missing.
623 2 ▶ if ( $cache->{class} ne $self->_class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->{"class"} ne $self->_class) 1 1 2
624 1 $cache = $self->store_cache;
625 }
626  
627 # Show possible options
628 2 say for $cache->{options}->@*;
629 }
630  
631 # Edit
632  
633 =head2 edit_class
634  
635 Edit a class using vim.
636 Can optionally just to a specific keyword.
637  
638 =cut
639  
640 0 ▶sub edit_class {
Subroutine edit_class: not called CC=2 SCAR=17.9
Pod documented
641 0 my ( $self ) = @_;
642 0 my $path = $self->_get_path;
643 0 my $method = $self->_method;
644 0 my $cmd = "vim $path";
645  
646 0 ▶ if ( $method ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method) 0 0 0
647 0 my $m = "<\\zs$method\\ze>";
648 0 my $sub = "<sub $m";
649 0 my $monkey = "<monkey_patch>.+$m";
650 0 my $list = "^ +$m +\\=\\>";
651 0 my $qw = "<qw>.+$m";
652 0 my $emit = "<(emit|on)\\($m";
653 0 $cmd .= " '+/\\v$sub|$monkey|$list|$qw|$emit'";
654 }
655  
656 0 exec $cmd;
657 }
658  
659 # Doc
660  
661 =head2 doc_class
662  
663 Show the documentation for a module using perldoc.
664  
665 =cut
666  
667 0 ▶sub doc_class {
Subroutine doc_class: not called CC=1 SCAR=6.9
Pod documented
668 0 my ( $self ) = @_;
669 0 my $class = $self->_class;
670 0 my @args = $self->_args->@*;
671  
672 0 exec "perldoc @args $class";
673 }
674  
675 # Query
676  
677 =head2 query_class
678  
679 Run a pod query using Pod::Query.
680  
681 Use --dump option to show the data structure.
682 (For debugging use).
683  
684 =cut
685  
686 10 ▶sub query_class {
Subroutine query_class: called CC=3 SCAR=11.0
Pod documented
687 10 my ( $self ) = @_;
688  
689 10 ▶ local $Pod::Query::DEBUG_FIND_DUMP = 1 if $self->_opts->{dump};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_opts->{"dump"} 2 8 10
690  
691 10 say for $self->_get_pod->find( $self->_opts->{query} );
692 }
693  
694 #
695 # Main
696 #
697  
698 6 ▶sub _process_main {
Subroutine _process_main: called CC=3 SCAR=11.0
699 6 my ( $self ) = @_;
700 6 ▶ say "_process_main()" if $self->_opts->{dump};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_opts->{"dump"} 0 6 6
701  
702 # Go on.
703 6 $self->show_header;
704 6 ▶ if ( $self->_method ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_method) { } 1 5 6
705 1 $self->show_method_doc;
706 }
707 else {
708 5 $self->show_inheritance;
709 5 $self->show_events;
710 5 $self->show_methods;
711 }
712 }
713  
714 # Header
715  
716 15 ▶sub _get_name_and_summary {
Subroutine _get_name_and_summary: called CC=5 SCAR=17.1
717 15 my ( $self ) = @_;
718  
719 # Use in-memory cache if present.
720 15 my $mem_cache = $self->_cache_name_and_summary;
721 15 ▶ return $mem_cache if $mem_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $mem_cache 6 9 15
722  
723 # Use disk cache if present.
724 9 my $disk_cache = $self->retrieve_cache;
725 9 ▶ if ( $disk_cache and $disk_cache->{name_and_summary} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($disk_cache and $disk_cache->{"name_and_summary"}) 0 9 9
Condition33%1/3
$disk_cache and $disk_cache->{"name_and_summary"}
!ll&&!rl&&r
090
MC/DC0%0/2
$disk_cache and $disk_cache->{"name_and_summary"}
$disk_cache $disk_cache->{"name_and_summary"}
Truth table33%1/3
A and B
A= $disk_cacheB= $disk_cache->{"name_and_summary"}
ABresult
0X0
100
111
726 0 $self->_cache_name_and_summary( $disk_cache->{name_and_summary} );
727 0 return $disk_cache->{name_and_summary};
728 }
729  
730 # Otherwise, get all class events.
731 9 ▶ local $Pod::Query::DEBUG_FIND_DUMP = 1 if $self->_opts->{dump};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_opts->{"dump"} 0 9 9
732 9 my $title = $self->_get_pod->find_title;
733 9 my $name_and_summary = [ split /\s*-\s*/, $title, 2 ];
734  
735 # Cache it in-memory.
736 9 $self->_cache_name_and_summary( $name_and_summary );
737  
738 # Flag that disk cache should be stored later.
739 9 $self->_dirty_cache( 1 );
740  
741 9 $name_and_summary;
742 }
743  
744 15 ▶sub _get_version {
Subroutine _get_version: called CC=6 SCAR=19.2
745 15 my ( $self ) = @_;
746  
747 # Use in-memory cache if present.
748 15 my $version_cache = $self->_cache_version;
749 15 ▶ return $version_cache if $version_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $version_cache 0 15 15
750  
751 # Use disk cache if present.
752 15 my $disk_cache = $self->retrieve_cache;
753 15 ▶ if ( $disk_cache and $disk_cache->{version} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($disk_cache and $disk_cache->{"version"}) 0 15 15
Condition33%1/3
$disk_cache and $disk_cache->{"version"}
!ll&&!rl&&r
0150
MC/DC0%0/2
$disk_cache and $disk_cache->{"version"}
$disk_cache $disk_cache->{"version"}
Truth table33%1/3
A and B
A= $disk_cacheB= $disk_cache->{"version"}
ABresult
0X0
100
111
754 0 $self->_cache_version( $disk_cache->{version} );
755 0 return $disk_cache->{version};
756 }
757  
758 # Otherwise, get the package/class version.
759 15 my $class = $self->_class;
760  
761 # TODO: Be able to get class from path
762 15 ▶ if ( not $self->_get_pod->class_is_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_get_pod->class_is_path) 14 1 15
763 14 eval "require $class";
764 }
765 15 ▶ my $version = $class->VERSION // '';
Condition50%1/2
$class->VERSION // ""
l!l
015
MC/DC0%0/1
$class->VERSION // ""
$class->VERSION
Truth table50%1/2
A
A= $class->VERSION
Aresult
11
00
766  
767 # Cache it in-memory.
768 15 $self->_cache_version( $version );
769  
770 # Flag that disk cache should be stored later.
771 15 $self->_dirty_cache( 1 );
772  
773 15 $version;
774 }
775  
776 =head2 show_header
777  
778 Prints a generic header for a module.
779  
780 =cut
781  
782 6 ▶sub show_header {
Subroutine show_header: called CC=7 SCAR=20.2
Pod documented
783 6 my ( $self ) = @_;
784 6 my $class = $self->_class;
785 6 my $version = $self->_get_version;
786 6 my $class_is_path = $self->_get_pod->class_is_path;
787 6 ▶ my $first_release =
Branch: 1 branch
BranchTrueFalseTotal
$class_is_path ? : 0 6 6
788 $class_is_path ? "" : Module::CoreList->first_release( $class );
789  
790 6 ▶ my @package_line = (
Branch: 3 branches
BranchTrueFalseTotal
$class_is_path ? : 0 6 6
$version ? : 0 6 6
$first_release ? : 0 6 6
791 _grey( "Package:" ),
792 sprintf(
793 "%s%s%s",
794 _yellow( $class_is_path ? "" : $class ),
795 ( $version ? _green( " $version" ) : "" ),
796 (
797 $first_release
798 ? _grey( " (since perl " )
799 . _green( $first_release )
800 . _grey( ")" )
801 : ""
802 ),
803 ),
804 );
805 6 my @path_line = ( _grey( "Path:" ), _grey( $self->_get_path ), );
806  
807 6 my $max = max map { length } $package_line[0], $path_line[0];
808 6 my $format = "%-${max}s %s";
809  
810 6 say "";
811 6 _sayt sprintf( $format, @package_line );
812 6 _sayt sprintf( $format, @path_line );
813  
814 6 say "";
815 6 my ( $name, $summary ) = $self->_get_name_and_summary->@*;
816 6 ▶ return unless $name and $summary;
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $summary 0 6 6
Condition33%1/3
$name and $summary
!ll&&!rl&&r
006
MC/DC0%0/2
$name and $summary
$name $summary
Truth table33%1/3
A and B
A= $nameB= $summary
ABresult
0X0
100
111
817  
818 6 _sayt _yellow( $name ) . " - " . _green( $summary );
819 6 say _reset( "" );
820 }
821  
822 # Inheritance
823  
824 14 ▶sub _get_isa {
Subroutine _get_isa: called CC=7 SCAR=19.8
825 14 my ( $self ) = @_;
826  
827 # Use in-memory cache if present.
828 14 my $isa_cache = $self->_cache_isa;
829 14 ▶ return $isa_cache if $isa_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $isa_cache 5 9 14
830  
831 # Use disk cache if present.
832 9 my $disk_cache = $self->retrieve_cache;
833 9 ▶ if ( $disk_cache and $disk_cache->{isa} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($disk_cache and $disk_cache->{"isa"}) 0 9 9
Condition33%1/3
$disk_cache and $disk_cache->{"isa"}
!ll&&!rl&&r
090
MC/DC0%0/2
$disk_cache and $disk_cache->{"isa"}
$disk_cache $disk_cache->{"isa"}
Truth table33%1/3
A and B
A= $disk_cacheB= $disk_cache->{"isa"}
ABresult
0X0
100
111
834 0 $self->_cache_isa( $disk_cache->{isa} );
835 0 return $disk_cache->{isa};
836 }
837  
838 # Otherwise, get all class inheritance.
839 9 my @classes = ( $self->_class );
840 9 my @isa;
841 my %seen;
842  
843 # TODO: Be able to get class from path
844 9 ▶ if ( not $self->_get_pod->class_is_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_get_pod->class_is_path) 8 1 9
845 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
846 8 ▶ while ( my $class = shift @classes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = shift @classes) 14 8 22
847 14 ▶ next if $seen{$class}++; # Already saw it
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$class}++ 0 14 14
848 14 push @isa, $class; # Add to list.
849 14 eval "require $class";
850 14 push @classes, @{"${class}::ISA"};
851 }
852 }
853  
854 # Cache it in-memory.
855 9 $self->_cache_isa( \@isa );
856  
857 # Flag that disk cache should be stored later.
858 9 $self->_dirty_cache( 1 );
859  
860 9 \@isa;
861 }
862  
863 =head2 show_inheritance
864  
865 Show the Inheritance chain of a class/module.
866  
867 =cut
868  
869 5 ▶sub show_inheritance {
Subroutine show_inheritance: called CC=3 SCAR=11.0
Pod documented
870 5 my ( $self ) = @_;
871 5 my $isa = $self->_get_isa;
872 5 my $size = @$isa;
873 5 ▶ return if $size <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $size <= 1 4 1 5
874 1 say _neon( "Inheritance ($size):" );
875 1 say _grey( " $_" ) for @$isa;
876 1 say _reset( "" );
877 }
878  
879 # Events
880  
881 23 ▶sub _get_events {
Subroutine _get_events: called CC=5 SCAR=17.2
882 23 my ( $self ) = @_;
883  
884 # Use in-memory cache if present.
885 23 my $mem_cache = $self->_cache_events;
886 23 ▶ return $mem_cache if $mem_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $mem_cache 14 9 23
887  
888 # Use disk cache if present.
889 9 my $disk_cache = $self->retrieve_cache;
890 9 ▶ if ( $disk_cache and $disk_cache->{events} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($disk_cache and $disk_cache->{"events"}) 0 9 9
Condition33%1/3
$disk_cache and $disk_cache->{"events"}
!ll&&!rl&&r
090
MC/DC0%0/2
$disk_cache and $disk_cache->{"events"}
$disk_cache $disk_cache->{"events"}
Truth table33%1/3
A and B
A= $disk_cacheB= $disk_cache->{"events"}
ABresult
0X0
100
111
891 0 $self->_cache_events( $disk_cache->{events} );
892 0 return $disk_cache->{events};
893 }
894  
895 # Otherwise, get all class events.
896 9 ▶ local $Pod::Query::DEBUG_FIND_DUMP = 1 if $self->_opts->{dump};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_opts->{"dump"} 0 9 9
897 9 my %events = $self->_get_pod->find_events;
898  
899 # Cache it in-memory.
900 9 $self->_cache_events( \%events );
901  
902 # Flag that disk cache should be stored later.
903 9 $self->_dirty_cache( 1 );
904  
905 9 \%events;
906 }
907  
908 9 ▶sub _get_event_names {
Subroutine _get_event_names: called CC=1 SCAR=0.0
909 9 my ( $self ) = @_;
910 9 sort keys $self->_get_events->%*;
911 }
912  
913 =head2 show_events
914  
915 Show any declared class events.
916  
917 =cut
918  
919 5 ▶sub show_events {
Subroutine show_events: called CC=3 SCAR=11.0
Pod documented
920 5 my ( $self ) = @_;
921 5 my $events = $self->_get_events;
922 5 my @names = sort keys %$events;
923 5 my $size = @names;
924 5 ▶ return unless $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 4 1 5
925  
926 1 my $len = max map { length( _green( $_ ) ) } @names;
927 1 my $format = " %-${len}s - %s";
928  
929 1 say _neon( "Events ($size):" );
930 1 for ( @names ) {
931 2 _sayt sprintf $format, _green( $_ ), _grey( $events->{$_} );
932 }
933 1 say _reset( "" );
934 }
935  
936 # Methods
937  
938 23 ▶sub _get_methods {
Subroutine _get_methods: called CC=7 SCAR=20.0
939 23 my ( $self ) = @_;
940  
941 # Use in-memory cache if present.
942 23 my $mem_cache = $self->_cache_methods;
943 23 ▶ return $mem_cache if $mem_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $mem_cache 14 9 23
944  
945 # Use disk cache if present.
946 9 my $disk_cache = $self->retrieve_cache;
947 9 ▶ if ( $disk_cache and $disk_cache->{methods} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($disk_cache and $disk_cache->{"methods"}) 0 9 9
Condition33%1/3
$disk_cache and $disk_cache->{"methods"}
!ll&&!rl&&r
090
MC/DC0%0/2
$disk_cache and $disk_cache->{"methods"}
$disk_cache $disk_cache->{"methods"}
Truth table33%1/3
A and B
A= $disk_cacheB= $disk_cache->{"methods"}
ABresult
0X0
100
111
948 0 $self->_cache_methods( $disk_cache->{methods} );
949 0 return $disk_cache->{methods};
950 }
951  
952 # Otherwise, get all class methods.
953 9 ▶ local $Pod::Query::DEBUG_FIND_DUMP = 1 if $self->_opts->{dump};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_opts->{"dump"} 0 9 9
954 9 my $pod = $self->_get_pod;
955 9 my @method_names;
956  
957 # The provided class is really the path.
958 {
959 9 local *_PodHelper;
960 9 ▶ if ( $pod->class_is_path ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pod->class_is_path) { } 1 8 9
elsif ($self->_import_class) { } 8 0 8
961 1 @method_names = $pod->find( "head2" );
962 }
963 elsif ( $self->_import_class ) {
964 @method_names =
965 8 sort { $a cmp $b }
966 uniq get_full_functions( "_PodHelper" ), # ojo would import here.
967 get_full_functions( $self->_class ); # All else here
968  
969 # TODO: figure out why "local *_Pod" does
970 # not remove the typeglob at the end.
971 # Also "undef *_Pod" does nothing.
972 8 delete @_PodHelper::{ keys %_PodHelper:: };
973 }
974 }
975  
976 my @methods =
977 9 map { [ $_, scalar $pod->find_method_summary( $_ ) ] } @method_names;
978  
979 # Cache it in-memory.
980 9 $self->_cache_methods( \@methods );
981  
982 # Flag that disk cache should be stored later.
983 9 $self->_dirty_cache( 1 );
984  
985 9 \@methods;
986 }
987  
988 8 ▶sub _import_class {
Subroutine _import_class: called CC=2 SCAR=6.9
989 8 my ( $self ) = @_;
990 8 my $class = $self->_class;
991  
992 # Try not to pollute main.
993 # ojo imports its DSL into the current package by default.
994 8 ▶ eval { eval "package _PodHelper; use $class"; };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
995  
996 8 my $import_ok = do {
997 0 ▶ if ( $@ ) { warn $@; 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 8 8
998 8 else { 1 }
999 };
1000  
1001 8 $import_ok;
1002 }
1003  
1004 9 ▶sub _get_method_names {
Subroutine _get_method_names: called CC=1 SCAR=0.0
1005 9 my ( $self ) = @_;
1006 9 my $methods = $self->_get_methods;
1007  
1008 my @names =
1009 444 grep { / ^ [\w_-]+ $ /x } # Normal looking names.
1010 9 map { $_->[0] } @$methods;
1011 }
1012  
1013 =head2 show_methods
1014  
1015 Show all class methods.
1016  
1017 =cut
1018  
1019 5 ▶sub show_methods {
Subroutine show_methods: called CC=9 SCAR=22.1
Pod documented
1020 5 my ( $self ) = @_;
1021  
1022 5 my $all_method_names_and_docs = $self->_get_methods; # 0: name, 1: doc.
1023  
1024 # Skip some methods unless using --all flag.
1025 my %skip_methods =
1026 5 map { $_ => 1 }
1027 qw(
1028 BEGIN
1029 VERSION
1030 ISA
1031 __ANON__
1032 );
1033  
1034 5 ▶ if ( not $self->_opts->{all} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_opts->{"all"}) 4 1 5
1035 @$all_method_names_and_docs =
1036 4 grep { not $skip_methods{ $_->[0] } } @$all_method_names_and_docs;
1037 }
1038  
1039 # Documented methods
1040 5 my @all_method_docs = grep { $_->[1] } @$all_method_names_and_docs;
1041  
1042 # If we have methods, but none are documented (or found).
1043 5 ▶ if ( @$all_method_names_and_docs and not @all_method_docs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$all_method_names_and_docs and not @all_method_docs) 0 5 5
Condition33%1/3
@$all_method_names_and_docs and not @all_method_docs
!ll&&!rl&&r
050
MC/DC0%0/2
@$all_method_names_and_docs and not @all_method_docs
@$all_method_names_and_docs not @all_method_docs
Truth table33%1/3
A and not(B)
A= @$all_method_names_and_docsB= not @all_method_docs
ABresult
0X0
100
111
1044 0 say _grey(
1045 "Warning: All methods are undocumented! (reverting to --all)\n" );
1046 0 $self->_opts->{all} = 1;
1047 }
1048  
1049 my @methods =
1050 5 ▶ $self->_opts->{all} ? @$all_method_names_and_docs : @all_method_docs;
Branch: 1 branch
BranchTrueFalseTotal
$self->_opts->{'all'} ? : 1 4 5
1051 5 my $max = max 0, map { length _green( $_->[0] ) } @methods;
1052 5 my $format_with_desc = " %-${max}s%s";
1053 5 my $format_no_desc = " %s%s";
1054 5 my $size = @methods;
1055 5 say _neon( "Methods ($size):" );
1056  
1057 5 for my $list ( @methods ) {
1058 155 my ( $method, $doc_raw ) = @$list;
1059 155 ▶ my $doc = $doc_raw ? " - $doc_raw" : "";
Branch: 1 branch
BranchTrueFalseTotal
$doc_raw ? : 132 23 155
1060 155 $doc =~ s/\n+/ /g;
1061  
1062 155 ▶ my $format = $doc_raw ? $format_with_desc : $format_no_desc;
Branch: 1 branch
BranchTrueFalseTotal
$doc_raw ? : 132 23 155
1063 155 _sayt sprintf $format, _green( $method ), _grey( $doc );
1064 }
1065  
1066 say _grey( "\nUse --all (or -a) to see all methods." )
1067 5 ▶ unless $self->_opts->{all};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_opts->{"all"} 4 1 5
1068 5 say _reset( "" );
1069 }
1070  
1071 =head2 show_method_doc
1072  
1073 Show documentation for a specific module method.
1074  
1075 =cut
1076  
1077 1 ▶sub show_method_doc {
Subroutine show_method_doc: called CC=3 SCAR=11.1
Pod documented
1078 1 my ( $self ) = @_;
1079  
1080 1 ▶ local $Pod::Query::DEBUG_FIND_DUMP = 1 if $self->_opts->{dump};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_opts->{"dump"} 0 1 1
1081 1 my $doc = $self->_get_pod->find_method( $self->_method );
1082  
1083 # Color.
1084 1 for ( $doc ) {
1085 1 chomp;
1086  
1087 # Headings.
1088 1 s/ ^ \s* \K (\S+:) (?= \s* $ ) / _green($1) /xgem;
1089  
1090 # Comments.
1091 0 s/ (\#.+) / _grey($1) /xge;
1092 }
1093  
1094 1 say $doc;
1095 1 say _reset( "" );
1096 }
1097  
1098 #
1099 # Caching
1100 #
1101  
1102 =head2 define_last_run_cache_file
1103  
1104 Defined where to save the results from the last run.
1105 This is done for performance reasons.
1106  
1107 =cut
1108  
1109 30 ▶sub define_last_run_cache_file {
Subroutine define_last_run_cache_file: called CC=1 SCAR=0.0
Pod documented
1110 30 my ( $self ) = @_;
1111 30 catfile( home(), ".cache", "my_pod_last_run.cache", );
1112 }
1113  
1114 9 ▶sub _get_class_options {
Subroutine _get_class_options: called CC=1 SCAR=0.0
1115 9 my ( $self ) = @_;
1116  
1117 9 [ sort $self->_get_event_names, $self->_get_method_names, ];
1118 }
1119  
1120 =head2 store_cache
1121  
1122 Saves the last class name and its methods/options.
1123  
1124 =cut
1125  
1126 9 ▶sub store_cache {
Subroutine store_cache: called CC=4 SCAR=14.0
Pod documented
1127 9 my ( $self ) = @_;
1128 9 my $cache = {
1129 class => $self->_class,
1130 path => $self->_get_path,
1131 name_and_summary => $self->_get_name_and_summary,
1132 version => $self->_get_version,
1133 isa => $self->_get_isa,
1134 events => $self->_get_events,
1135 methods => $self->_get_methods,
1136 options => $self->_get_class_options,
1137 };
1138 9 my $path = path( $self->define_last_run_cache_file );
1139  
1140 9 ▶ if ( not -e $path->dirname ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $path->dirname) 1 8 9
1141 1 ▶ mkdir $path->dirname or die $!;
Branch: 1 branch
BranchTrueFalseTotal
mkdir $path->dirname or die $! 1 0 1
1142 }
1143  
1144 9 ▶ my $writer = $path->can( "spew" ) ? "spew" : "spurt";
Branch: 1 branch
BranchTrueFalseTotal
$path->can('spew') ? : 9 0 9
1145 9 $path->$writer( j $cache );
1146  
1147 # Reset the flag.
1148 9 $self->_dirty_cache( 0 );
1149  
1150 9 $cache;
1151 }
1152  
1153 =head2 retrieve_cache
1154  
1155 Returns the last stored class cache and its options.
1156  
1157 =cut
1158  
1159 75 ▶sub retrieve_cache {
Subroutine retrieve_cache: called CC=5 SCAR=16.2
Pod documented
1160 75 my ( $self ) = @_;
1161 75 my $empty = { class => "" };
1162 75 ▶ return $empty if $self->_dirty_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_dirty_cache 51 24 75
1163  
1164 # Use in-memory cache if present.
1165 24 my $mem_cache = $self->_cache_from_file;
1166 24 ▶ return $mem_cache if $mem_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $mem_cache 3 21 24
1167  
1168 # Otherwise, go to the actual file.
1169 21 my $file = $self->define_last_run_cache_file;
1170 21 ▶ if ( not -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 21 21
1171 0 $self->_dirty_cache( 1 );
1172 0 return $empty;
1173 }
1174  
1175 # Extract data from file.
1176 21 my $disk_cache = j path( $file )->slurp;
1177  
1178 # Wrong class.
1179 21 ▶ if ( $disk_cache->{class} ne $self->_class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($disk_cache->{"class"} ne $self->_class) 10 11 21
1180 10 $self->_dirty_cache( 1 );
1181 10 return $empty;
1182 }
1183  
1184 # Cache it locally
1185 11 $self->_cache_from_file( $disk_cache );
1186  
1187 11 $disk_cache;
1188 }
1189  
1190 #
1191 # Output
1192 #
1193  
1194 =head2 trim
1195  
1196 Trim a line to fit the terminal width.
1197 Handles also escape codes within the line.
1198  
1199 =cut
1200  
1201 184 ▶sub trim {
Subroutine trim: called CC=10 SCAR=23.0
Pod documented
1202 184 my ( $line ) = @_;
1203 184 state $esc = qr{ \033\[ [\d;]+ m }x;
1204 184 state $data = qr{ (?: (?!$esc) . )++ }x;
1205 184 state $data_or_escape = qr{ (?<data>$data) | (?<esc>$esc) }x;
1206 184 state $term_width = Pod::Query::get_term_width();
1207 184 state $replacement = " ...";
1208 184 state $width_raw = $term_width - length( $replacement );
1209 184 ▶ state $base_width = $width_raw >= 0 ? $width_raw : 0; # To avoid negatives.
Branch: 1 branch
BranchTrueFalseTotal
$width_raw >= 0 ? : 2 0 2
1210  
1211 # Figure out the total len of the line (uncolored).
1212 184 my $total_chars = 0;
1213 184 my @detailed_line_parts;
1214 184 ▶ while ( $line =~ /$data_or_escape/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$data_or_escape/gu) 328 184 512
1215 328 my $part = {%+};
1216 328 ▶ $total_chars += $part->{len} = length( $part->{data} // "" );
Condition100%2/2
$part->{"data"} // ""
l!l
228100
MC/DC100%1/1
$part->{"data"} // ""
$part->{"data"}
Truth table100%2/2
A
A= $part->{"data"}
Aresult
11
00
1217 328 push @detailed_line_parts, $part;
1218 }
1219  
1220 # No need to trim.
1221 184 ▶ return $line if $total_chars <= $term_width;
Branch: 1 branch
BranchTrueFalseTotal
if $total_chars <= $term_width 68 116 184
1222  
1223 # Need to trim.
1224 116 my @parts;
1225 my $size_exceeded;
1226 116 my $so_far_len = 0;
1227 116 for my $part ( @detailed_line_parts ) {
1228  
1229 # Handle escape codes.
1230 195 ▶ if ( not $part->{len} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($part->{"len"}) 54 141 195
1231 54 push @parts, $part->{esc}; # Add escapes back.
1232 54 ▶ last if $size_exceeded; # Done.
Branch: 1 branch
BranchTrueFalseTotal
if $size_exceeded 14 40 54
1233 40 next;
1234 }
1235  
1236 # Handle trailing escapes.
1237 141 ▶ last if $size_exceeded;
Branch: 1 branch
BranchTrueFalseTotal
if $size_exceeded 0 141 141
1238  
1239 # Trim line if it would be too long.
1240 141 ▶ if ( $so_far_len + $part->{len} > $base_width ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($so_far_len + $part->{"len"} > $base_width) 116 25 141
1241 116 $size_exceeded = 1; # Still need to possibly add a trailing escape.
1242  
1243 # Limit line to allowed width.
1244 $part->{data} = substr(
1245 $part->{data},
1246 116 0,
1247 $base_width - $so_far_len, # How much space is left.
1248 ) . $replacement;
1249 }
1250  
1251 141 $so_far_len += $part->{len};
1252 141 push @parts, $part->{data};
1253 }
1254  
1255 116 join "", @parts;
1256 }
1257  
1258 175 ▶sub _sayt {
Subroutine _sayt: called CC=1 SCAR=0.0
1259  
1260 175 say trim( @_ );
1261 }
1262  
1263 16 ▶sub _red {
Subroutine _red: called CC=1 SCAR=0.0
1264  
1265 16 colored( "@_", "RESET RED" );
1266 }
1267  
1268 10 ▶sub _yellow {
Subroutine _yellow: called CC=1 SCAR=0.0
1269  
1270 10 colored( "@_", "RESET YELLOW" );
1271 }
1272  
1273 62 ▶sub _green {
Subroutine _green: called CC=1 SCAR=0.0
1274  
1275 # Reset since last line may be trimmed.
1276 62 colored( "@_", "RESET GREEN" );
1277 }
1278  
1279 54 ▶sub _grey {
Subroutine _grey: called CC=1 SCAR=0.0
1280  
1281 54 colored( "@_", "RESET DARK" );
1282 }
1283  
1284 7 ▶sub _neon {
Subroutine _neon: called CC=1 SCAR=0.0
1285  
1286 7 colored( "@_", "RESET ON_BRIGHT_BLACK" );
1287 }
1288  
1289 6 ▶sub _reset {
Subroutine _reset: called CC=1 SCAR=0.0
1290  
1291 6 colored( "@_", "RESET" );
1292 }
1293  
1294 #
1295 # Misc Support
1296 #
1297  
1298 8 ▶sub uniq(@) {
Subroutine uniq: called CC=1 SCAR=0.0
1299 8 my %h;
1300 8 grep { not $h{$_}++ } @_;
1301 }
1302  
1303 #
1304 # Legacy
1305 #
1306  
1307 =for Legacy
1308 -
1309 - # pod version 0
1310 -
1311 - package UNIVERSAL;
1312 -
1313 - sub dir{
1314 - my ($s) = @_; # class or object
1315 - my $ref = ref $s;
1316 - my $class = $ref ? $ref : $s; # myClass
1317 - my $pkg = $class . "::"; # MyClass::
1318 - my @keys_raw;
1319 - my $is_special_block = qr/^ (?:BEGIN|UNITCHECK|INIT|CHECK|END|import|DESTROY) $/x;
1320 -
1321 - no strict 'refs';
1322 -
1323 - while( my($key,$stash) = each %$pkg){
1324 - # next if $key =~ /$is_special_block/; # Not a special block
1325 - # next if $key =~ /^ _ /x; # Not private method
1326 - next if ref $stash; # Stash name should not be a reference
1327 - next if not defined *$stash{CODE}; # Stash function should be defined
1328 - push @keys_raw, $key;
1329 - }
1330 -
1331 - my @keys = sort @keys_raw;
1332 -
1333 - return @keys if defined wantarray;
1334 -
1335 - say join "\n ", "\n$class", @keys;
1336 - }
1337  
1338 =head1 ENVIRONMENT
1339  
1340 Install bash completion support.
1341  
1342 % apt install bash-completion
1343  
1344 Install tab completion.
1345  
1346 % source bash_completion_pod
1347  
1348 =head1 SEE ALSO
1349  
1350 L<Pod::Query>
1351  
1352 L<Pod::LOL>
1353  
1354 L<Module::Functions>
1355  
1356  
1357 =head1 AUTHOR
1358  
1359 Tim Potapov, C<< <tim.potapov[AT]gmail.com> >>
1360  
1361 =head1 BUGS
1362  
1363 Please report any bugs or feature requests to L<https://github.com/poti1/app-pod/issues>.
1364  
1365  
1366 =head1 SUPPORT
1367  
1368 You can find documentation for this module with the perldoc command.
1369  
1370 perldoc App::Pod
1371  
1372 You can also look for information at:
1373  
1374 L<https://metacpan.org/pod/App::Pod>
1375 L<https://github.com/poti1/app-pod>
1376  
1377  
1378 =head1 ACKNOWLEDGEMENTS
1379  
1380 TBD
1381  
1382  
1383 =head1 LICENSE AND COPYRIGHT
1384  
1385 This software is Copyright (c) 2022 by Tim Potapov.
1386  
1387 This is free software, licensed under:
1388  
1389 The Artistic License 2.0 (GPL Compatible)
1390  
1391  
1392 =cut
1393  
1394 3 1; # End of App::Pod
↑ Summary