Kit/Form.pm

filter: Statementstmt 100% 162 / 162 Branchbran 95.1% 97 / 102 Conditioncond 85.1% 46 / 54 MC/DCmcdc 75.0% 24 / 32 Subroutinesub 100% 17 / 17 Podpod 87.5% 7 / 8 totaltotal 94.1% 353 / 375 CC 72 SCAR 27.8
CC
72
Cov
96%
step
31.0
lines
14.0
show_value
10.4
CRAP 16.1
« Kit/Save.pm ↑ 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.06';
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 actions/] => (
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 22 ▶sub BUILD {
Subroutine BUILD: called CC=9 SCAR=22.1
Pod undocumented
51 22 my ($self) = @_;
52 22 ▶ $self->fields([]) unless $self->fields;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->fields 0 22 22
53 22 ▶ $self->actions([]) unless $self->actions;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->actions 20 2 22
54 22 ▶ $self->values({}) unless $self->values;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->values 19 3 22
55 22 ▶ $self->title('') unless defined $self->title;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->title 0 22 22
56 22 ▶ $self->action('Play') unless defined $self->action && length $self->action;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->action and length $self->action 21 1 22
Condition66%2/3
defined $self->action and length $self->action
!ll&&!rl&&r
2101
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
57 22 ▶ $self->legend([
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legend 18 4 22
58 'left and right change a value, enter edits a text, backspace clears,',
59 'enter on ' . $self->action . ' starts, escape goes back',
60 ]) unless $self->legend;
61 22 ▶ $self->at(scalar @{ $self->rows }) unless defined $self->at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->at 16 6 22
62 22 return $self;
63 }
64  
65 212 ▶sub rows {
Subroutine rows: called CC=2 SCAR=6.9
Pod documented
66 212 my ($self) = @_;
67 return [
68 212 ▶ @{$self->fields},
Branch: 1 branch
BranchTrueFalseTotal
$self->extra ? : 209 3 212
69 ($self->extra
70 ? { name => 'extra', type => 'extra', help => 'anything else, as on the command line' }
71 : ()),
72 ];
73 }
74  
75 329 ▶sub show_value {
Subroutine show_value: called CC=10 SCAR=23.4
Pod documented
76 329 my ($self, $field, $value) = @_;
77 329 my $type = $field->{type};
78 329 ▶ return $value ? 'on' : 'off' if $type eq 'flag';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "flag" 33 296 329
$value ? : 1 32 33
79 296 ▶ return !defined $value ? $OWN : $value ? 'on' : 'off' if $type eq 'bool';
Branch: 3 branches
BranchTrueFalseTotal
if $type eq "bool" 33 263 296
!defined($value) ? : 32 1 33
$value ? : 0 1 1
80 263 ▶ return defined $value && length $value ? $value : '(none)' if $type eq 'extra';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "extra" 76 187 263
defined $value && length $value ? : 0 76 76
Condition33%1/3
defined $value and length $value
!ll&&!rl&&r
7600
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
81 187 ▶ return defined $value ? $value : $OWN;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 8 179 187
82 }
83  
84 78 ▶sub lines {
Subroutine lines: called CC=14 SCAR=26.4
Pod documented
85 78 my ($self) = @_;
86 78 my $rows = $self->rows;
87 78 my $values = $self->values;
88 78 my $at = $self->at;
89 78 my @lines;
90 78 ▶ push @lines, $self->title, '' if length $self->title;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->title 76 2 78
91 78 for my $index (0 .. $#{$rows}) {
92 329 my $field = $rows->[$index];
93 329 my $value = $values->{ $field->{name} };
94 329 my $shown = $self->show_value($field, $value);
95 329 ▶ if ($index == $at) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == $at) 24 305 329
96 24 my $line = sprintf '%-11s %-18s %s', $field->{name}, $shown, $field->{help};
97 24 $line =~ s/\s+$//;
98 24 push @lines, ' ' . Game::Terminal::Kit::Widget::paint($self, "> $line", '1;7');
99 24 next;
100 }
101 305 ▶ my $set = $field->{type} eq 'flag' ? $value : defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
$field->{'type'} eq 'flag' ? : 33 272 305
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
26804
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
102 my $line = ' ' . Game::Terminal::Kit::Widget::paint($self, sprintf('%-11s', $field->{name}), $INK{key}) . ' '
103 . Game::Terminal::Kit::Widget::paint($self, sprintf('%-18s', $shown), $set ? $INK{set} : $INK{own}) . ' '
104 305 ▶ . $field->{help};
Branch: 1 branch
BranchTrueFalseTotal
$set ? : 5 300 305
105 305 $line =~ s/\s+$//;
106 305 push @lines, $line;
107 }
108 78 push @lines, '', $at == @{$rows}
109 ? ' ' . Game::Terminal::Kit::Widget::paint($self, '> ' . $self->action, '1;7')
110 78 ▶ : ' ' . Game::Terminal::Kit::Widget::paint($self, $self->action, $INK{action});
Branch: 1 branch
BranchTrueFalseTotal
$at == @{$rows;} ? : 52 26 78
111 78 my $next = @{$rows} + 1;
112 78 for my $action (@{ $self->actions }) {
113 6 ▶ my $label = defined $action->{label} ? $action->{label} : $action->{name};
Branch: 1 branch
BranchTrueFalseTotal
defined $action->{'label'} ? : 2 4 6
114 6 ▶ push @lines, $at == $next++
Branch: 1 branch
BranchTrueFalseTotal
$at == $next++ ? : 2 4 6
115 ? ' ' . Game::Terminal::Kit::Widget::paint($self, "> $label", '1;7')
116 : " $label";
117 }
118 78 ▶ my @said = $self->log ? $self->log->lines : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->log ? : 50 28 78
119 78 ▶ push @lines, '', @said if @said;
Branch: 1 branch
BranchTrueFalseTotal
if @said 43 35 78
120 146 push @lines, '', map { Game::Terminal::Kit::Widget::paint($self, $_, $INK{legend}) } @{$self->legend}
121 78 ▶ if @{$self->legend};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->legend;} 73 5 78
122 78 return @lines;
123 }
124  
125 27 ▶sub _say {
Subroutine _say: called CC=2 SCAR=6.9
126 27 my ($self, $text) = @_;
127 27 ▶ $self->log->note($text) if $self->log;
Branch: 1 branch
BranchTrueFalseTotal
if $self->log 24 3 27
128 27 return;
129 }
130  
131 35 ▶sub _keep {
Subroutine _keep: called CC=2 SCAR=6.9
132 35 my ($self, $name, $value) = @_;
133 35 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 29 6 35
134 29 $self->values->{$name} = $value;
135 } else {
136 6 delete $self->values->{$name};
137 }
138 35 return;
139 }
140  
141 12 ▶sub set {
Subroutine set: called CC=6 SCAR=17.9
Pod documented
142 12 my ($self, $name, $text) = @_;
143 12 my ($field) = grep { $_->{name} eq $name } @{ $self->rows };
144 12 ▶ return 0 unless $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 1 11 12
145 11 ▶ if ($field->{type} eq 'extra') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{"type"} eq "extra") 2 9 11
146 2 ▶ $text = '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 2 2
147 2 $text =~ s/^\s+|\s+$//g;
148 2 ▶ $self->_keep($name, length $text ? $text : undef);
Branch: 1 branch
BranchTrueFalseTotal
length $text ? : 1 1 2
149 2 return 1;
150 }
151 9 my ($value, $error) = Game::Terminal::Kit::Options::check_value($field, $text);
152 9 ▶ if (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 3 6 9
153 3 $self->_say($error);
154 3 return 0;
155 }
156 6 $self->_keep($name, $value);
157 6 return 1;
158 }
159  
160 13 ▶sub argv {
Subroutine argv: called CC=1 SCAR=0.0
Pod documented
161 13 my ($self) = @_;
162 13 return Game::Terminal::Kit::Options::argv_for($self->fields, $self->values);
163 }
164  
165 119 ▶sub step {
Subroutine step: called CC=31 SCAR=34.4
Pod documented
166 119 my ($self, $key) = @_;
167 119 ▶ 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 116 119
Condition66%4/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
30116
not defined $key or $key eq "eof"
l!l&&r!l&&!r
03116
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
168 116 ▶ return ('back') if $key eq 'escape' || $key eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "escape" or $key eq "q" 7 109 116
Condition100%3/3
$key eq "escape" or $key eq "q"
l!l&&r!l&&!r
43109
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
169 109 ▶ return ('chosen', $self->values) if $key eq 'p';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "p" 3 106 109
170  
171 106 my $rows = $self->rows;
172 106 my $count = @{$rows} + 1 + @{ $self->actions };
173 106 my $at = $self->at;
174 106 ▶ 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") 14 92 106
Condition100%6/6
$key eq "up" or $key eq "k" or $key eq "back_tab"
l!l&&r!l&&!r
11392
$key eq "up" or $key eq "k"
l!l&&r!l&&!r
8395
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
175 14 $self->at(($at - 1) % $count);
176 14 return ('moved');
177 }
178 92 ▶ 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") 22 70 92
Condition100%6/6
$key eq "down" or $key eq "j" or $key eq "tab"
l!l&&r!l&&!r
19370
$key eq "down" or $key eq "j"
l!l&&r!l&&!r
16373
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
179 22 $self->at(($at + 1) % $count);
180 22 return ('moved');
181 }
182 70 ▶ if ($at == @{$rows}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at == @{$rows;}) 31 39 70
183 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
184 21 $self->_say('That key does nothing here');
185 21 return ('none');
186 }
187 39 ▶ if ($at > @{$rows}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at > @{$rows;}) 4 35 39
188 4 ▶ return ('key', $self->actions->[ $at - @{$rows} - 1 ]{name}) if $key eq 'enter' || $key eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "enter" or $key eq " " 3 1 4
Condition100%3/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
211
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
189 1 $self->_say('That key does nothing here');
190 1 return ('none');
191 }
192  
193 35 my $field = $rows->[$at];
194 35 my $name = $field->{name};
195 35 my $type = $field->{type};
196 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
197 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
198 2 $self->_keep($name, undef);
199 2 return ('changed');
200 }
201 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
202 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
203 1 $self->_say("$name is typed: press enter");
204 1 return ('none');
205 }
206 $self->_keep($name, Game::Terminal::Kit::Options::step_value(
207 19 ▶ $field, $self->values->{$name}, $key eq 'right' ? 1 : -1,
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'right' ? : 17 2 19
208 ));
209 19 return ('changed');
210 }
211 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
212 12 ▶ return ('edit', $field) if $typed;
Branch: 1 branch
BranchTrueFalseTotal
if $typed 6 6 12
213 6 $self->_keep($name, Game::Terminal::Kit::Options::step_value($field, $self->values->{$name}, 1));
214 6 return ('changed');
215 }
216 1 $self->_say('That key does nothing here');
217 1 return ('none');
218 }
219  
220 7 ▶sub run {
Subroutine run: called CC=4 SCAR=13.9
Pod documented
221 7 my ($self, $keys, $screen) = @_;
222 7 while (1) {
223 10 my ($event, $value) = my @result = Game::Terminal::Kit::Widget::run($self, $keys, $screen);
224 10 ▶ return @result unless $event eq 'edit';
Branch: 1 branch
BranchTrueFalseTotal
unless $event eq "edit" 6 4 10
225 4 my $line = Game::Terminal::Kit::Line->new(
226 prompt => "$value->{name}: ",
227 head => [ $self->lines, '' ],
228 );
229 4 my ($answer, $text) = $line->run($keys, $screen);
230 4 ▶ return ('quit') if $answer eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $answer eq "quit" 1 3 4
231 3 ▶ $self->set($value->{name}, $text) if $answer eq 'chosen';
Branch: 1 branch
BranchTrueFalseTotal
if $answer eq "chosen" 2 1 3
232 }
233 }
234  
235 3 1;
236  
237 __END__
« Kit/Save.pm ↑ Summary Kit/Picker.pm »