blib/lib/Term/Fabulous/Roving.pm

filter: Statementstmt 100% 42 / 42 Branchbran 95.8% 23 / 24 Conditioncond 87.5% 14 / 16 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 94.9% 94 / 99 CC 20 SCAR 27.2
CC
20
Cov
96%
roving_target
20.1
BEGIN
1.0
BEGIN
1.0
CRAP 15.1
« blib/lib/Term/Fabulous/Role/Themed.pm ↑ Summary blib/lib/Term/Fabulous/Render/Target/Mask.pm »
1 package Term::Fabulous::Roving;
2  
3 1 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.05';
9  
10 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 our @EXPORT_OK = qw(roving_target);
12  
13 1 ▶use List::Util qw(first max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Key name => how it moves: a number of entries (page: a page of them),
16 # or to the first or the last entry.
17 1 my %MOVE_BY_KEY = (
18 Left => -1,
19 Up => -1,
20 Right => 1,
21 Down => 1,
22 'Ctrl+PageUp' => -1,
23 'Ctrl+PageDown' => 1,
24 PageUp => 'page_back',
25 PageDown => 'page_forward',
26 Home => 'first',
27 End => 'last',
28 );
29  
30 # The keys each kind of widget answers.
31 1 my %KEYS_OF = (
32 arrows => [qw(Left Up Right Down Home End)],
33 vertical => [qw(Up Down Home End)],
34 list => [qw(Up Down PageUp PageDown Home End)],
35 pages => [ 'Ctrl+PageUp', 'Ctrl+PageDown' ],
36 );
37  
38 1 my %IS_POLICY = map { $_ => 1 } qw(wrap clamp);
39  
40 # The entry a key moves to among the enabled ones, or undef when the key
41 # is none of the kind's or no entry is enabled.
42 28 ▶sub roving_target ( $enabled, $current, $key, %options ) {
Subroutine roving_target: called CC=20 SCAR=30.0
Pod documented
43 28 my @unknown = grep { !/\A(?:keys|policy|page)\z/ } sort keys %options;
44 28 ▶ die "Term::Fabulous::Roving: roving_target does not take @unknown (known: keys, page, policy)" if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 27 28
45 27 ▶ my $keys = $KEYS_OF{ $options{keys} // 'arrows' } // die "Term::Fabulous::Roving: unknown keys '$options{keys}' (known: " . join( ', ', sort keys %KEYS_OF ) . ")";
Condition100%4/4
$KEYS_OF{$options{"keys"} // "arrows"} // die(qq[Term::Fabulous::Roving: unknown keys '$options{"keys"}' (known: ] . join(", ", sort(keys %KEYS_OF)) . ')')
l!l
261
$options{"keys"} // "arrows"
l!l
1017
MC/DC100%1/1
$KEYS_OF{$options{"keys"} // "arrows"} // die(qq[Term::Fabulous::Roving: unknown keys '$options{"keys"}' (known: ] . join(", ", sort(keys %KEYS_OF)) . ')')
$KEYS_OF{$options{"keys"} // "arrows"}
MC/DC100%1/1
$options{"keys"} // "arrows"
$options{"keys"}
Truth table100%2/2
A
A= $KEYS_OF{$options{"keys"} // "arrows"}
Aresult
11
00
Truth table100%2/2
A
A= $options{"keys"}
Aresult
11
00
46 26 ▶ my $policy = $options{policy} // 'wrap';
Condition100%2/2
$options{"policy"} // "wrap"
l!l
719
MC/DC100%1/1
$options{"policy"} // "wrap"
$options{"policy"}
Truth table100%2/2
A
A= $options{"policy"}
Aresult
11
00
47 26 ▶ die "Term::Fabulous::Roving: policy must be wrap or clamp, got '$policy'" unless $IS_POLICY{$policy};
Branch: 1 branch
BranchTrueFalseTotal
unless $IS_POLICY{$policy} 1 25 26
48 25 ▶ die "Term::Fabulous::Roving: the enabled entries must be an array reference" unless ref $enabled eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $enabled eq "ARRAY" 0 25 25
49  
50 25 ▶ return undef unless defined $key && @$enabled && grep { $_ eq $key } @$keys;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and @$enabled and grep {$_ eq $key;} @$keys 7 18 25
Condition100%6/6
defined $key and @$enabled and grep {$_ eq $key;} @$keys
!ll&&!rl&&r
3418
defined $key and @$enabled
!ll&&!rl&&r
1222
MC/DC100%3/3
defined $key and @$enabled and grep {$_ eq $key;} @$keys
defined $key @$enabled grep {$_ eq $key;} @$keys
Truth table100%4/4
A and B and C
A= defined $keyB= @$enabledC= grep {$_ eq $key;} @$keys
ABCresult
0XX0
10X0
1100
1111
51 18 my $move = $MOVE_BY_KEY{$key};
52 18 ▶ return $enabled->[0] if $move eq 'first';
Branch: 1 branch
BranchTrueFalseTotal
if $move eq "first" 1 17 18
53 17 ▶ return $enabled->[-1] if $move eq 'last';
Branch: 1 branch
BranchTrueFalseTotal
if $move eq "last" 1 16 17
54  
55 16 ▶ my $by = $move eq 'page_back' ? -( $options{page} // 1 ) : $move eq 'page_forward' ? $options{page} // 1 : $move;
Branch: 2 branches
BranchTrueFalseTotal
$move eq 'page_back' ? : 1 15 16
$move eq 'page_forward' ? : 2 13 15
Condition50%2/4
$options{"page"} // 1
l!l
10
$options{"page"} // 1
l!l
20
MC/DC0%0/1
$options{"page"} // 1
$options{"page"}
MC/DC0%0/1
$options{"page"} // 1
$options{"page"}
Truth table50%1/2
A
A= $options{"page"}
Aresult
11
00
Truth table50%1/2
A
A= $options{"page"}
Aresult
11
00
56 16 ▶ my $at = defined $current ? first { $enabled->[$_] == $current } 0 .. $#$enabled : undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
defined $current ? : 14 2 16
57 16 ▶ return $by > 0 ? $enabled->[0] : $enabled->[-1] unless defined $at;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $at 3 13 16
$by > 0 ? : 2 1 3
58 13 ▶ return $enabled->[ ( $at + $by ) % @$enabled ] if $policy eq 'wrap';
Branch: 1 branch
BranchTrueFalseTotal
if $policy eq "wrap" 7 6 13
59 6 return $enabled->[ max( 0, min( $#$enabled, $at + $by ) ) ];
60 }
61  
62 1 1;
63  
64 __END__
« blib/lib/Term/Fabulous/Role/Themed.pm ↑ Summary blib/lib/Term/Fabulous/Render/Target/Mask.pm »