Kit/Picker.pm

filter: Statementstmt 100% 152 / 152 Branchbran 93.7% 90 / 96 Conditioncond 91.3% 63 / 69 MC/DCmcdc 87.5% 35 / 40 Subroutinesub 100% 21 / 21 Podpod 92.8% 13 / 14 totaltotal 95.4% 374 / 392 CC 73 SCAR 26.5
CC
73
Cov
96%
step
28.0
rows
19.0
BUILD
7.0
CRAP 14.2
« Kit/Form.pm ↑ Summary Kit/Options.pm »
1 package Game::Terminal::Kit::Picker;
2  
3 5 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Game::Terminal::Kit::Steer;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Game::Terminal::Kit::Widget;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our $VERSION = '0.02';
12  
13 has [qw/items head legend chosen/] => (
14 is => 'rw',
15 isa => ArrayRef
16 5 );
17  
18 has game_keys => (
19 is => 'rw',
20 isa => HashRef
21 5 );
22  
23 has prefer => (
24 is => 'rw',
25 isa => CodeRef
26 5 );
27  
28 has [qw/at log screen/] => (
29 is => 'rw'
30 5 );
31  
32 has window => (
33 is => 'rw',
34 isa => Int,
35 default => 8
36 5 );
37  
38 has need => (
39 is => 'rw',
40 isa => Int,
41 default => 0
42 5 );
43  
44 has numbered => (
45 is => 'rw',
46 isa => Bool,
47 default => 1
48 5 );
49  
50 has across => (
51 is => 'rw',
52 isa => Bool,
53 default => 0
54 5 );
55  
56 56 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=19.5
Pod undocumented
57 56 my ($self) = @_;
58 56 ▶ $self->items([]) unless $self->items;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->items 0 56 56
59 56 ▶ $self->head([]) unless $self->head;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->head 45 11 56
60 56 ▶ $self->legend([]) unless $self->legend;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legend 41 15 56
61 56 ▶ $self->game_keys({}) unless $self->game_keys;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game_keys 52 4 56
62 56 ▶ $self->chosen([]) unless $self->chosen;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->chosen 56 0 56
63 56 ▶ $self->at($self->resting) unless defined $self->at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->at 54 2 56
64 56 return $self;
65 }
66  
67 65 ▶sub resting {
Subroutine resting: called CC=1 SCAR=0.0
Pod documented
68 65 my ($self) = @_;
69 65 return Game::Terminal::Kit::Steer::rest($self->items, $self->prefer);
70 }
71  
72 11 ▶sub reset {
Subroutine reset: called CC=2 SCAR=7.0
Pod documented
73 11 my ($self, $items) = @_;
74 11 ▶ $self->items($items) if $items;
Branch: 1 branch
BranchTrueFalseTotal
if $items 11 0 11
75 11 $self->chosen([]);
76 11 $self->at($self->resting);
77 11 return $self;
78 }
79  
80 1071 ▶sub skipped {
Subroutine skipped: called CC=3 SCAR=11.0
Pod documented
81 1071 my ($self, $index) = @_;
82 1071 my $item = $self->items->[$index];
83 1071 ▶ return ref $item eq 'HASH' && $item->{skip} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $item eq 'HASH' && $item->{'skip'} ? : 48 1023 1071
Condition100%3/3
ref $item eq "HASH" and $item->{"skip"}
!ll&&!rl&&r
58144248
MC/DC100%2/2
ref $item eq "HASH" and $item->{"skip"}
ref $item eq "HASH" $item->{"skip"}
Truth table100%3/3
A and B
A= ref $item eq "HASH"B= $item->{"skip"}
ABresult
0X0
100
111
84 }
85  
86 1367 ▶sub label_of {
Subroutine label_of: called CC=3 SCAR=11.1
Pod documented
87 1367 my ($self, $index) = @_;
88 1367 my $item = $self->items->[$index];
89 1367 ▶ return ref $item eq 'HASH' ? (defined $item->{label} ? $item->{label} : '') : "$item";
Branch: 2 branches
BranchTrueFalseTotal
ref $item eq 'HASH' ? : 314 1053 1367
defined $item->{'label'} ? : 314 0 314
90 }
91  
92 91 ▶sub value_of {
Subroutine value_of: called CC=3 SCAR=11.0
Pod documented
93 91 my ($self, $index) = @_;
94 91 my $item = $self->items->[$index];
95 91 ▶ return ref $item eq 'HASH' && exists $item->{value} ? $item->{value} : $item;
Branch: 1 branch
BranchTrueFalseTotal
ref $item eq 'HASH' && exists $item->{'value'} ? : 81 10 91
Condition66%2/3
ref $item eq "HASH" and exists $item->{"value"}
!ll&&!rl&&r
10081
MC/DC50%1/2
ref $item eq "HASH" and exists $item->{"value"}
ref $item eq "HASH" exists $item->{"value"}
Truth table66%2/3
A and B
A= ref $item eq "HASH"B= exists $item->{"value"}
ABresult
0X0
100
111
96 }
97  
98 62 ▶sub current {
Subroutine current: called CC=3 SCAR=11.0
Pod documented
99 62 my ($self) = @_;
100 62 my $at = $self->at;
101 62 ▶ return undef unless @{$self->items} && !$self->skipped($at);
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->items;} and not $self->skipped($at) 1 61 62
Condition66%2/3
@{$self->items;} and not $self->skipped($at)
!ll&&!rl&&r
0161
MC/DC50%1/2
@{$self->items;} and not $self->skipped($at)
@{$self->items;} not $self->skipped($at)
Truth table66%2/3
A and not(B)
A= @{$self->items;}B= not $self->skipped($at)
ABresult
0X0
100
111
102 61 return $self->value_of($at);
103 }
104  
105 142 ▶sub rows {
Subroutine rows: called CC=19 SCAR=29.4
Pod documented
106 142 my ($self) = @_;
107 142 my $items = $self->items;
108 142 my $at = $self->at;
109 142 my $window = $self->window;
110 142 my ($first, $last) = (0, $#{$items});
111 142 ▶ if ($window > 0 && @{$items} > $window) {
Branch: 1 branch
BranchTrueFalseTotal
if ($window > 0 and @{$items;} > $window) 24 118 142
Condition100%3/3
$window > 0 and @{$items;} > $window
!ll&&!rl&&r
318724
MC/DC100%2/2
$window > 0 and @{$items;} > $window
$window > 0 @{$items;} > $window
Truth table100%3/3
A and B
A= $window > 0B= @{$items;} > $window
ABresult
0X0
100
111
112 24 $first = $at - int($window / 2);
113 24 ▶ $first = 0 if $first < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $first < 0 5 19 24
114 24 ▶ $first = @{$items} - $window if $first > @{$items} - $window;
Branch: 1 branch
BranchTrueFalseTotal
if $first > @{$items;} - $window 5 19 24
115 24 $last = $first + $window - 1;
116 }
117 142 ▶ my $any_about = grep { ref $_ eq 'HASH' && defined $_->{about} && length $_->{about} } @{$items};
Condition100%6/6
ref $_ eq 'HASH' && defined $_->{'about'} && length $_->{'about'}
!ll&&!rl&&r
790139
ref $_ eq "HASH" and defined $_->{"about"}
!ll&&!rl&&r
67311740
MC/DC100%3/3
ref $_ eq 'HASH' && defined $_->{'about'} && length $_->{'about'}
ref $_ eq "HASH" defined $_->{"about"} length $_->{'about'}
Truth table100%4/4
A and B && C
A= ref $_ eq "HASH"B= defined $_->{"about"}C= length $_->{'about'}
ABCresult
0XX0
10X0
1100
1111
118 142 my ($wide) = sort { $b <=> $a } map { length $self->label_of($_) } 0 .. $#{$items};
119 142 my %taken = map { $_ => 1 } @{$self->chosen};
120  
121 142 my @rows;
122 142 ▶ push @rows, sprintf ' %d further up', $first if $first;
Branch: 1 branch
BranchTrueFalseTotal
if $first 18 124 142
123 142 for my $index ($first .. $last) {
124 537 my $item = $items->[$index];
125 537 my $line = '';
126 537 ▶ $line .= sprintf '%2d. ', $index + 1 if $self->numbered;
Branch: 1 branch
BranchTrueFalseTotal
if $self->numbered 264 273 537
127 537 ▶ $line .= ($taken{$index} ? '[x] ' : '[ ] ') if $self->need > 0;
Branch: 2 branches
BranchTrueFalseTotal
if $self->need > 0 76 461 537
$taken{$index} ? : 16 60 76
128 537 ▶ if ($any_about) {
Branch: 1 branch
BranchTrueFalseTotal
if ($any_about) { } 71 466 537
129 71 ▶ my $about = ref $item eq 'HASH' && defined $item->{about} ? $item->{about} : '';
Branch: 1 branch
BranchTrueFalseTotal
ref $item eq 'HASH' && defined $item->{'about'} ? : 40 31 71
Condition66%2/3
ref $item eq "HASH" and defined $item->{"about"}
!ll&&!rl&&r
03140
MC/DC50%1/2
ref $item eq "HASH" and defined $item->{"about"}
ref $item eq "HASH" defined $item->{"about"}
Truth table66%2/3
A and B
A= ref $item eq "HASH"B= defined $item->{"about"}
ABresult
0X0
100
111
130 71 $line .= sprintf '%-*s %s', $wide, $self->label_of($index), $about;
131 } else {
132 466 $line .= $self->label_of($index);
133 }
134 537 $line =~ s/\s+$//;
135 537 ▶ push @rows, $index == $at && !$self->skipped($index)
Branch: 2 branches
BranchTrueFalseTotal
$index == $at && !$self->skipped($index) ? : 140 397 537
$self->skipped($index) ? : 14 383 397
Condition100%3/3
$index == $at and not $self->skipped($index)
!ll&&!rl&&r
3961140
MC/DC100%2/2
$index == $at and not $self->skipped($index)
$index == $at not $self->skipped($index)
Truth table100%3/3
A and not(B)
A= $index == $atB= not $self->skipped($index)
ABresult
0X0
100
111
136 ? ' ' . Game::Terminal::Kit::Widget::paint($self, "> $line", '1;7')
137 : $self->skipped($index)
138 ? ' ' . Game::Terminal::Kit::Widget::paint($self, $line, '2')
139 : " $line";
140 }
141 142 ▶ push @rows, sprintf ' %d further down', $#{$items} - $last if $last < $#{$items};
Branch: 1 branch
BranchTrueFalseTotal
if $last < $#{$items;} 18 124 142
142 142 return @rows;
143 }
144  
145 112 ▶sub lines {
Subroutine lines: called CC=3 SCAR=11.0
Pod documented
146 112 my ($self) = @_;
147 112 ▶ my @said = $self->log ? $self->log->lines : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->log ? : 43 69 112
148 112 ▶ return (@{$self->head}, (@said ? (@said, '') : ()), $self->rows, @{$self->legend});
Branch: 1 branch
BranchTrueFalseTotal
@said ? : 26 86 112
149 }
150  
151 93 ▶sub move {
Subroutine move: called CC=2 SCAR=6.9
Pod documented
152 93 my ($self, $delta) = @_;
153 my $next = Game::Terminal::Kit::Steer::ring(
154 93 ▶ scalar @{$self->items}, $self->at, $delta, sub { $self->skipped($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
155 93 );
156 93 ▶ return ('none') if $next == $self->at;
Branch: 1 branch
BranchTrueFalseTotal
if $next == $self->at 2 91 93
157 91 $self->at($next);
158 91 return ('moved');
159 }
160  
161 20 ▶sub jump {
Subroutine jump: called CC=5 SCAR=16.4
Pod documented
162 20 my ($self, $index) = @_;
163 20 ▶ return ('none') if $index < 0 || $index > $#{$self->items} || $self->skipped($index);
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 or $index > $#{$self->items;} or $self->skipped($index) 1 19 20
Condition50%3/6
$index < 0 or $index > $#{$self->items;} or $self->skipped($index)
l!l&&r!l&&!r
0119
$index < 0 or $index > $#{$self->items;}
l!l&&r!l&&!r
0020
MC/DC33%1/3
$index < 0 or $index > $#{$self->items;} or $self->skipped($index)
$index < 0 $index > $#{$self->items;} $self->skipped($index)
Truth table50%2/4
A or B or C
A= $index < 0B= $index > $#{$self->items;}C= $self->skipped($index)
ABCresult
1XX1
01X1
0011
0000
164 19 ▶ return ('none') if $index == $self->at;
Branch: 1 branch
BranchTrueFalseTotal
if $index == $self->at 4 15 19
165 15 $self->at($index);
166 15 return ('moved');
167 }
168  
169 34 ▶sub take {
Subroutine take: called CC=6 SCAR=17.9
Pod documented
170 34 my ($self) = @_;
171 34 my $at = $self->at;
172 34 ▶ return ('none') unless @{$self->items} && !$self->skipped($at);
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->items;} and not $self->skipped($at) 2 32 34
Condition100%3/3
@{$self->items;} and not $self->skipped($at)
!ll&&!rl&&r
1132
MC/DC100%2/2
@{$self->items;} and not $self->skipped($at)
@{$self->items;} not $self->skipped($at)
Truth table100%3/3
A and not(B)
A= @{$self->items;}B= not $self->skipped($at)
ABresult
0X0
100
111
173 32 ▶ return ('chosen', $self->value_of($at)) unless $self->need > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->need > 0 25 7 32
174 7 my %taken = map { $_ => 1 } @{$self->chosen};
175 7 ▶ if ($taken{$at}) { delete $taken{$at} } else { $taken{$at} = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($taken{$at}) { } 1 6 7
176 7 my @chosen = sort { $a <=> $b } keys %taken;
177 7 $self->chosen(\@chosen);
178 7 ▶ return ('changed') unless @chosen == $self->need;
Branch: 1 branch
BranchTrueFalseTotal
unless @chosen == $self->need 4 3 7
179 3 return ('chosen', [ map { $self->value_of($_) } @chosen ]);
180 }
181  
182 203 ▶sub step {
Subroutine step: called CC=28 SCAR=33.3
Pod documented
183 203 my ($self, $key) = @_;
184 203 ▶ return ('quit') if !defined $key || $key eq 'eof' || $key eq 'interrupt';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key eq "eof" or $key eq "interrupt" 6 197 203
Condition100%6/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
51197
not defined $key or $key eq "eof"
l!l&&r!l&&!r
14198
MC/DC100%3/3
not defined $key or $key eq "eof" or $key eq "interrupt"
not defined $key $key eq "eof" $key eq "interrupt"
Truth table100%4/4
not(A) or B or C
A= not defined $keyB= $key eq "eof"C= $key eq "interrupt"
ABCresult
1XX1
01X1
0011
0000
185 197 ▶ return ('key', $self->game_keys->{$key}) if exists $self->game_keys->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->game_keys->{$key} 5 192 197
186  
187 192 ▶ my ($back, $on) = $self->across ? ('down', 'up') : ('up', 'down');
Branch: 1 branch
BranchTrueFalseTotal
$self->across ? : 4 188 192
188 192 ▶ return $self->move(-1) if $key eq 'left' || $key eq $back || $key eq 'back_tab';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "left" or $key eq $back or $key eq "back_tab" 46 146 192
Condition100%6/6
$key eq "left" or $key eq $back or $key eq "back_tab"
l!l&&r!l&&!r
415146
$key eq "left" or $key eq $back
l!l&&r!l&&!r
635151
MC/DC100%3/3
$key eq "left" or $key eq $back or $key eq "back_tab"
$key eq "left" $key eq $back $key eq "back_tab"
Truth table100%4/4
A or B or C
A= $key eq "left"B= $key eq $backC= $key eq "back_tab"
ABCresult
1XX1
01X1
0011
0000
189 146 ▶ return $self->move(+1) if $key eq 'right' || $key eq $on || $key eq 'tab';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "right" or $key eq $on or $key eq "tab" 47 99 146
Condition100%6/6
$key eq "right" or $key eq $on or $key eq "tab"
l!l&&r!l&&!r
42599
$key eq "right" or $key eq $on
l!l&&r!l&&!r
636104
MC/DC100%3/3
$key eq "right" or $key eq $on or $key eq "tab"
$key eq "right" $key eq $on $key eq "tab"
Truth table100%4/4
A or B or C
A= $key eq "right"B= $key eq $onC= $key eq "tab"
ABCresult
1XX1
01X1
0011
0000
190  
191 99 my @open = grep { !$self->skipped($_) } 0 .. $#{$self->items};
192 99 ▶ return @open ? $self->jump($open[0]) : ('none') if $key eq 'home' || $key eq 'page_up';
Branch: 2 branches
BranchTrueFalseTotal
if $key eq "home" or $key eq "page_up" 7 92 99
@open ? : 6 1 7
Condition100%3/3
$key eq "home" or $key eq "page_up"
l!l&&r!l&&!r
6192
MC/DC100%2/2
$key eq "home" or $key eq "page_up"
$key eq "home" $key eq "page_up"
Truth table100%3/3
A or B
A= $key eq "home"B= $key eq "page_up"
ABresult
1X1
011
000
193 92 ▶ return @open ? $self->jump($open[-1]) : ('none') if $key eq 'end' || $key eq 'page_down';
Branch: 2 branches
BranchTrueFalseTotal
if $key eq "end" or $key eq "page_down" 8 84 92
@open ? : 8 0 8
Condition100%3/3
$key eq "end" or $key eq "page_down"
l!l&&r!l&&!r
7184
MC/DC100%2/2
$key eq "end" or $key eq "page_down"
$key eq "end" $key eq "page_down"
Truth table100%3/3
A or B
A= $key eq "end"B= $key eq "page_down"
ABresult
1X1
011
000
194 84 ▶ return $self->jump($key - 1) if $self->numbered && $key =~ m/^[1-9]$/ && $key <= @{$self->items};
Branch: 1 branch
BranchTrueFalseTotal
if $self->numbered and $key =~ /^[1-9]$/u and $key <= @{$self->items;} 6 78 84
Condition100%6/6
$self->numbered and $key =~ /^[1-9]$/u and $key <= @{$self->items;}
!ll&&!rl&&r
7446
$self->numbered and $key =~ /^[1-9]$/u
!ll&&!rl&&r
215310
MC/DC100%3/3
$self->numbered and $key =~ /^[1-9]$/u and $key <= @{$self->items;}
$self->numbered $key =~ /^[1-9]$/u $key <= @{$self->items;}
Truth table100%4/4
A and B and C
A= $self->numberedB= $key =~ /^[1-9]$/uC= $key <= @{$self->items;}
ABCresult
0XX0
10X0
1100
1111
195  
196 78 ▶ return $self->take if $key eq 'enter' || $key eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "enter" or $key eq " " 34 44 78
Condition100%3/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
28644
MC/DC100%2/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table100%3/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
197 44 ▶ return ('back') if $key eq 'escape' || $key eq 'backspace';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "escape" or $key eq "backspace" 10 34 44
Condition100%3/3
$key eq "escape" or $key eq "backspace"
l!l&&r!l&&!r
6434
MC/DC100%2/2
$key eq "escape" or $key eq "backspace"
$key eq "escape" $key eq "backspace"
Truth table100%3/3
A or B
A= $key eq "escape"B= $key eq "backspace"
ABresult
1X1
011
000
198 34 ▶ return ('quit') if $key eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "q" 8 26 34
199  
200 $self->log->note('That key does nothing here.'
201 26 ▶ . (exists $self->game_keys->{'?'} ? ' Press ? for the ones that do.' : ''))
Branch: 2 branches
BranchTrueFalseTotal
if $self->log 8 18 26
exists $self->game_keys->{'?'} ? : 8 0 8
202 if $self->log;
203 26 return ('none');
204 }
205  
206 5 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod documented
207 5 my ($self, $keys, $screen) = @_;
208 5 return Game::Terminal::Kit::Widget::run($self, $keys, $screen);
209 }
210  
211 5 1;
212  
213 __END__
« Kit/Form.pm ↑ Summary Kit/Options.pm »