Kit/Form.pm

filter: Statementstmt 100% 148 / 148 Branchbran 94.5% 87 / 92 Conditioncond 84.3% 43 / 51 MC/DCmcdc 73.3% 22 / 30 Subroutinesub 100% 17 / 17 Podpod 87.5% 7 / 8 totaltotal 93.6% 324 / 346 CC 65 SCAR 26.5
CC
65
Cov
96%
step
28.0
lines
11.0
show_value
10.4
CRAP 14.2
↑ Summary Kit/Picker.pm »
1 package Game::Terminal::Kit::Form;
2  
3 3 ▶use 5.014;
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  
7 3 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Game::Terminal::Kit::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Game::Terminal::Kit::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Game::Terminal::Kit::Widget;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '0.02';
13  
14 3 my $OWN = "the game\x27s own";
15  
16 3 my %INK = (
17 key => '1',
18 set => '38;5;114',
19 own => '2',
20 action => '1;32',
21 legend => '2',
22 );
23  
24 has [qw/title action/] => (
25 is => 'rw',
26 isa => Str,
27 default => ''
28 3 );
29  
30 has [qw/fields legend/] => (
31 is => 'rw',
32 isa => ArrayRef
33 3 );
34  
35 has values => (
36 is => 'rw',
37 isa => HashRef
38 3 );
39  
40 has extra => (
41 is => 'rw',
42 isa => Bool,
43 default => 1
44 3 );
45  
46 has [qw/at log screen/] => (
47 is => 'rw'
48 3 );
49  
50 20 ▶sub BUILD {
Subroutine BUILD: called CC=8 SCAR=20.9
Pod undocumented
51 20 my ($self) = @_;
52 20 ▶ $self->fields([]) unless $self->fields;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->fields 0 20 20
53 20 ▶ $self->values({}) unless $self->values;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->values 17 3 20
54 20 ▶ $self->title('') unless defined $self->title;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->title 0 20 20
55 20 ▶ $self->action('Play') unless defined $self->action && length $self->action;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->action and length $self->action 19 1 20
Condition66%2/3
defined $self->action and length $self->action
!ll&&!rl&&r
1901
MC/DC50%1/2
defined $self->action and length $self->action
defined $self->action length $self->action
Truth table66%2/3
A and B
A= defined $self->actionB= length $self->action
ABresult
0X0
100
111
56 20 ▶ $self->legend([
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legend 17 3 20
57 'left and right change a value, enter edits a text, backspace clears,',
58 'enter on ' . $self->action . ' starts, escape goes back',
59 ]) unless $self->legend;
60 20 ▶ $self->at(scalar @{ $self->rows }) unless defined $self->at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->at 14 6 20
61 20 return $self;
62 }
63  
64 197 ▶sub rows {
Subroutine rows: called CC=2 SCAR=6.9
Pod documented
65 197 my ($self) = @_;
66 return [
67 197 ▶ @{$self->fields},
Branch: 1 branch
BranchTrueFalseTotal
$self->extra ? : 194 3 197
68 ($self->extra
69 ? { name => 'extra', type => 'extra', help => 'anything else, as on the command line' }
70 : ()),
71 ];
72 }
73  
74 305 ▶sub show_value {
Subroutine show_value: called CC=10 SCAR=23.4
Pod documented
75 305 my ($self, $field, $value) = @_;
76 305 my $type = $field->{type};
77 305 ▶ return $value ? 'on' : 'off' if $type eq 'flag';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "flag" 29 276 305
$value ? : 1 28 29
78 276 ▶ return !defined $value ? $OWN : $value ? 'on' : 'off' if $type eq 'bool';
Branch: 3 branches
BranchTrueFalseTotal
if $type eq "bool" 29 247 276
!defined($value) ? : 28 1 29
$value ? : 0 1 1
79 247 ▶ return defined $value && length $value ? $value : '(none)' if $type eq 'extra';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "extra" 72 175 247
defined $value && length $value ? : 0 72 72
Condition33%1/3
defined $value and length $value
!ll&&!rl&&r
7200
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table33%1/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
80 175 ▶ return defined $value ? $value : $OWN;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 8 167 175
81 }
82  
83 74 ▶sub lines {
Subroutine lines: called CC=11 SCAR=24.0
Pod documented
84 74 my ($self) = @_;
85 74 my $rows = $self->rows;
86 74 my $values = $self->values;
87 74 my $at = $self->at;
88 74 my @lines;
89 74 ▶ push @lines, $self->title, '' if length $self->title;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->title 72 2 74
90 74 for my $index (0 .. $#{$rows}) {
91 305 my $field = $rows->[$index];
92 305 my $value = $values->{ $field->{name} };
93 305 my $shown = $self->show_value($field, $value);
94 305 ▶ if ($index == $at) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == $at) 24 281 305
95 24 my $line = sprintf '%-11s %-18s %s', $field->{name}, $shown, $field->{help};
96 24 $line =~ s/\s+$//;
97 24 push @lines, ' ' . Game::Terminal::Kit::Widget::paint($self, "> $line", '1;7');
98 24 next;
99 }
100 281 ▶ my $set = $field->{type} eq 'flag' ? $value : defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
$field->{'type'} eq 'flag' ? : 29 252 281
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
24804
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
101 my $line = ' ' . Game::Terminal::Kit::Widget::paint($self, sprintf('%-11s', $field->{name}), $INK{key}) . ' '
102 . Game::Terminal::Kit::Widget::paint($self, sprintf('%-18s', $shown), $set ? $INK{set} : $INK{own}) . ' '
103 281 ▶ . $field->{help};
Branch: 1 branch
BranchTrueFalseTotal
$set ? : 5 276 281
104 281 $line =~ s/\s+$//;
105 281 push @lines, $line;
106 }
107 74 push @lines, '', $at == @{$rows}
108 ? ' ' . Game::Terminal::Kit::Widget::paint($self, '> ' . $self->action, '1;7')
109 74 ▶ : ' ' . Game::Terminal::Kit::Widget::paint($self, $self->action, $INK{action});
Branch: 1 branch
BranchTrueFalseTotal
$at == @{$rows;} ? : 50 24 74
110 74 ▶ my @said = $self->log ? $self->log->lines : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->log ? : 50 24 74
111 74 ▶ push @lines, '', @said if @said;
Branch: 1 branch
BranchTrueFalseTotal
if @said 43 31 74
112 142 push @lines, '', map { Game::Terminal::Kit::Widget::paint($self, $_, $INK{legend}) } @{$self->legend}
113 74 ▶ if @{$self->legend};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->legend;} 71 3 74
114 74 return @lines;
115 }
116  
117 26 ▶sub _say {
Subroutine _say: called CC=2 SCAR=6.9
118 26 my ($self, $text) = @_;
119 26 ▶ $self->log->note($text) if $self->log;
Branch: 1 branch
BranchTrueFalseTotal
if $self->log 24 2 26
120 26 return;
121 }
122  
123 35 ▶sub _keep {
Subroutine _keep: called CC=2 SCAR=6.9
124 35 my ($self, $name, $value) = @_;
125 35 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 29 6 35
126 29 $self->values->{$name} = $value;
127 } else {
128 6 delete $self->values->{$name};
129 }
130 35 return;
131 }
132  
133 12 ▶sub set {
Subroutine set: called CC=6 SCAR=17.9
Pod documented
134 12 my ($self, $name, $text) = @_;
135 12 my ($field) = grep { $_->{name} eq $name } @{ $self->rows };
136 12 ▶ return 0 unless $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 1 11 12
137 11 ▶ if ($field->{type} eq 'extra') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{"type"} eq "extra") 2 9 11
138 2 ▶ $text = '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 2 2
139 2 $text =~ s/^\s+|\s+$//g;
140 2 ▶ $self->_keep($name, length $text ? $text : undef);
Branch: 1 branch
BranchTrueFalseTotal
length $text ? : 1 1 2
141 2 return 1;
142 }
143 9 my ($value, $error) = Game::Terminal::Kit::Options::check_value($field, $text);
144 9 ▶ if (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 3 6 9
145 3 $self->_say($error);
146 3 return 0;
147 }
148 6 $self->_keep($name, $value);
149 6 return 1;
150 }
151  
152 13 ▶sub argv {
Subroutine argv: called CC=1 SCAR=0.0
Pod documented
153 13 my ($self) = @_;
154 13 return Game::Terminal::Kit::Options::argv_for($self->fields, $self->values);
155 }
156  
157 110 ▶sub step {
Subroutine step: called CC=28 SCAR=33.3
Pod documented
158 110 my ($self, $key) = @_;
159 110 ▶ 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" 3 107 110
Condition66%4/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
30107
not defined $key or $key eq "eof"
l!l&&r!l&&!r
03107
MC/DC33%1/3
not defined $key or $key eq "eof" or $key eq "interrupt"
not defined $key $key eq "eof" $key eq "interrupt"
Truth table50%2/4
not(A) or B or C
A= not defined $keyB= $key eq "eof"C= $key eq "interrupt"
ABCresult
1XX1
01X1
0011
0000
160 107 ▶ return ('back') if $key eq 'escape' || $key eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "escape" or $key eq "q" 7 100 107
Condition100%3/3
$key eq "escape" or $key eq "q"
l!l&&r!l&&!r
43100
MC/DC100%2/2
$key eq "escape" or $key eq "q"
$key eq "escape" $key eq "q"
Truth table100%3/3
A or B
A= $key eq "escape"B= $key eq "q"
ABresult
1X1
011
000
161 100 ▶ return ('chosen', $self->values) if $key eq 'p';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "p" 3 97 100
162  
163 97 my $rows = $self->rows;
164 97 my $count = @{$rows} + 1;
165 97 my $at = $self->at;
166 97 ▶ if ($key eq 'up' || $key eq 'k' || $key eq 'back_tab') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "up" or $key eq "k" or $key eq "back_tab") 13 84 97
Condition100%6/6
$key eq "up" or $key eq "k" or $key eq "back_tab"
l!l&&r!l&&!r
10384
$key eq "up" or $key eq "k"
l!l&&r!l&&!r
7387
MC/DC100%3/3
$key eq "up" or $key eq "k" or $key eq "back_tab"
$key eq "up" $key eq "k" $key eq "back_tab"
Truth table100%4/4
A or B or C
A= $key eq "up"B= $key eq "k"C= $key eq "back_tab"
ABCresult
1XX1
01X1
0011
0000
167 13 $self->at(($at - 1) % $count);
168 13 return ('moved');
169 }
170 84 ▶ if ($key eq 'down' || $key eq 'j' || $key eq 'tab') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "down" or $key eq "j" or $key eq "tab") 18 66 84
Condition100%6/6
$key eq "down" or $key eq "j" or $key eq "tab"
l!l&&r!l&&!r
15366
$key eq "down" or $key eq "j"
l!l&&r!l&&!r
12369
MC/DC100%3/3
$key eq "down" or $key eq "j" or $key eq "tab"
$key eq "down" $key eq "j" $key eq "tab"
Truth table100%4/4
A or B or C
A= $key eq "down"B= $key eq "j"C= $key eq "tab"
ABCresult
1XX1
01X1
0011
0000
171 18 $self->at(($at + 1) % $count);
172 18 return ('moved');
173 }
174 66 ▶ if ($at == @{$rows}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at == @{$rows;}) 31 35 66
175 31 ▶ return ('chosen', $self->values) if $key eq 'enter' || $key eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "enter" or $key eq " " 10 21 31
Condition100%3/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
7321
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
176 21 $self->_say('That key does nothing here');
177 21 return ('none');
178 }
179  
180 35 my $field = $rows->[$at];
181 35 my $name = $field->{name};
182 35 my $type = $field->{type};
183 35 ▶ my $typed = $type eq 'str' || $type eq 'extra' || $type eq 'int';
Condition83%5/6
$type eq 'str' || $type eq 'extra' || $type eq 'int'
l!l&&r!l&&!r
32111
$type eq "str" or $type eq "extra"
l!l&&r!l&&!r
3032
MC/DC66%2/3
$type eq 'str' || $type eq 'extra' || $type eq 'int'
$type eq "str" $type eq "extra" $type eq 'int'
Truth table75%3/4
A or B || C
A= $type eq "str"B= $type eq "extra"C= $type eq 'int'
ABCresult
1XX1
01X1
0011
0000
184 35 ▶ if ($key eq 'backspace' || $key eq 'delete') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "backspace" or $key eq "delete") 2 33 35
Condition100%3/3
$key eq "backspace" or $key eq "delete"
l!l&&r!l&&!r
1133
MC/DC100%2/2
$key eq "backspace" or $key eq "delete"
$key eq "backspace" $key eq "delete"
Truth table100%3/3
A or B
A= $key eq "backspace"B= $key eq "delete"
ABresult
1X1
011
000
185 2 $self->_keep($name, undef);
186 2 return ('changed');
187 }
188 33 ▶ if ($key eq 'left' || $key eq 'right') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "left" or $key eq "right") 20 13 33
Condition100%3/3
$key eq "left" or $key eq "right"
l!l&&r!l&&!r
21813
MC/DC100%2/2
$key eq "left" or $key eq "right"
$key eq "left" $key eq "right"
Truth table100%3/3
A or B
A= $key eq "left"B= $key eq "right"
ABresult
1X1
011
000
189 20 ▶ if ($type eq 'str' || $type eq 'extra') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "str" or $type eq "extra") 1 19 20
Condition66%2/3
$type eq "str" or $type eq "extra"
l!l&&r!l&&!r
1019
MC/DC50%1/2
$type eq "str" or $type eq "extra"
$type eq "str" $type eq "extra"
Truth table66%2/3
A or B
A= $type eq "str"B= $type eq "extra"
ABresult
1X1
011
000
190 1 $self->_say("$name is typed: press enter");
191 1 return ('none');
192 }
193 $self->_keep($name, Game::Terminal::Kit::Options::step_value(
194 19 ▶ $field, $self->values->{$name}, $key eq 'right' ? 1 : -1,
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'right' ? : 17 2 19
195 ));
196 19 return ('changed');
197 }
198 13 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 12 1 13
Condition100%3/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
1111
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
199 12 ▶ return ('edit', $field) if $typed;
Branch: 1 branch
BranchTrueFalseTotal
if $typed 6 6 12
200 6 $self->_keep($name, Game::Terminal::Kit::Options::step_value($field, $self->values->{$name}, 1));
201 6 return ('changed');
202 }
203 1 $self->_say('That key does nothing here');
204 1 return ('none');
205 }
206  
207 6 ▶sub run {
Subroutine run: called CC=4 SCAR=13.9
Pod documented
208 6 my ($self, $keys, $screen) = @_;
209 6 while (1) {
210 9 my ($event, $value) = my @result = Game::Terminal::Kit::Widget::run($self, $keys, $screen);
211 9 ▶ return @result unless $event eq 'edit';
Branch: 1 branch
BranchTrueFalseTotal
unless $event eq "edit" 5 4 9
212 4 my $line = Game::Terminal::Kit::Line->new(
213 prompt => "$value->{name}: ",
214 head => [ $self->lines, '' ],
215 );
216 4 my ($answer, $text) = $line->run($keys, $screen);
217 4 ▶ return ('quit') if $answer eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $answer eq "quit" 1 3 4
218 3 ▶ $self->set($value->{name}, $text) if $answer eq 'chosen';
Branch: 1 branch
BranchTrueFalseTotal
if $answer eq "chosen" 2 1 3
219 }
220 }
221  
222 3 1;
223  
224 __END__
↑ Summary Kit/Picker.pm »