App/Plugin/Term/Meta/Attribute.pm untested

filter: Statementstmt 0.0% 0 / 181 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 339 CC 45 SCAR 64.7
CC
45
Cov
0%
cmd_term_read_string
1122.0
cmd_term_read_bool
42.0
cmd_term_label_full
30.0
CRAP 646.4
1 # ============================================================================
2 package MooseX::App::Plugin::Term::Meta::Attribute;
3 # ============================================================================
4  
5 use utf8;
6 use 5.010;
7  
8 use namespace::autoclean;
9 use Moose::Role;
10  
11 use List::Util qw(first);
12 use Term::ReadKey;
13  
14 has 'cmd_term' => (
15 is => 'ro',
16 isa => 'Bool',
17 default => sub {0},
18 );
19  
20 has 'cmd_term_label' => (
21 is => 'ro',
22 isa => 'Str',
23 predicate => 'has_cmd_term_label',
24 );
25  
26 sub cmd_term_label_full {
27 my ($self) = @_;
28  
29 my $label = $self->cmd_term_label_name;
30 my @tags;
31 if ($self->is_required) {
32 push(@tags,'Required');
33 } else {
34 push(@tags,'Optional');
35 }
36  
37 if ($self->has_type_constraint) {
38 my $type_constraint = $self->type_constraint;
39 if ($type_constraint->is_a_type_of('Bool')) {
40 push(@tags,'Y/N');
41 } else {
42 push(@tags,$self->cmd_type_constraint_description($type_constraint));
43 }
44 }
45 if (scalar @tags) {
46 $label .= ' ('.join(', ',@tags).')';
47 }
48  
49 return $label;
50 }
51  
52 sub cmd_term_label_name {
53 my ($self) = @_;
54  
55 my $label;
56 if ($self->has_cmd_term_label) {
57 return $self->cmd_term_label;
58 } elsif ($self->has_documentation) {
59 return $self->documentation;
60 } else {
61 return $self->name;
62 }
63 }
64  
65 sub cmd_term_read {
66 my ($self) = @_;
67  
68 if ($self->has_type_constraint
69 && $self->type_constraint->is_a_type_of('Bool')) {
70 return $self->cmd_term_read_bool();
71 } else {
72 return $self->cmd_term_read_string();
73 }
74 }
75  
76 sub cmd_term_read_string {
77 my ($self) = @_;
78  
79 my $label = $self->cmd_term_label_full;
80 my ($return,@history,$history_disable,$allowed);
81  
82 binmode STDIN,':encoding(UTF-8)';
83  
84 # Prefill history with enums
85 if ($self->has_type_constraint) {
86 my $type_constraint = $self->type_constraint;
87 if ($type_constraint->isa('Moose::Meta::TypeConstraint::Enum')) {
88 push(@history,@{$self->type_constraint->values});
89 $history_disable = 1
90 } elsif (!$type_constraint->has_coercion) {
91 if ($type_constraint->is_a_type_of('Int')) {
92 $allowed = qr/[0-9]/;
93 } elsif ($type_constraint->is_a_type_of('Num')) {
94 $allowed = qr/[0-9.]/;
95 }
96 }
97 }
98  
99 push(@history,"")
100 unless scalar @history;
101  
102 my $history_index = 0;
103 my $history_add = sub {
104 my $entry = shift;
105 if (! $history_disable
106 && defined $entry
107 && $entry !~ m/^\s*$/
108 && ! first { $entry eq $_ } @history) {
109 push(@history,$entry);
110 }
111 };
112  
113 ReadMode('cbreak'); # change input mode
114 TRY_STRING:
115 while (1) {
116 print "\n"
117 if defined $return
118 && $return !~ /^\s*$/;
119 $return = '';
120  
121 if (defined $Term::ANSIColor::VERSION) {
122 say Term::ANSIColor::color('white bold').$label.' :'.Term::ANSIColor::color('reset');
123 } else {
124 say $label.": ";
125 }
126  
127 1 while defined ReadKey -1; # discard any previous input
128  
129 my $cursor = 0;
130  
131 KEY_STRING:
132 while (1) {
133 my $key = ReadKey 0; # read a single character
134 my $length = length($return);
135 my $key_code = ord($key);
136  
137 if ($key_code == 10) { # Enter
138 print "\n";
139 my $error;
140 if ($return =~ m/^\s*$/) {
141 if ($self->is_required) {
142 $error = 'Value is required';
143 } else {
144 $return = undef;
145 last TRY_STRING;
146 }
147 } else {
148 $error = $self->cmd_type_constraint_check($return);
149 }
150 if ($error) {
151 if (defined $Term::ANSIColor::VERSION) {
152 say Term::ANSIColor::color('bright_red bold').$error.Term::ANSIColor::color('reset');
153 } else {
154 say $error;
155 }
156 $history_add->($return);
157 next TRY_STRING;
158 } else {
159 last TRY_STRING;
160 }
161 } elsif ($key_code == 27) { # Escape sequence
162 my $escape;
163 while (1) { # Read rest of escape sequence
164 my $code = ReadKey -1;
165 last unless defined $code;
166 $escape .= $code;
167 }
168 if (defined $escape) {
169 if ($escape eq '[D') { # Cursor left
170 if ($cursor > 0) {
171 print "\b";
172 $cursor--;
173 }
174 }
175 elsif ($escape eq '[C') { # Cursor right
176 if ($cursor < length($return)) {
177 print substr $return,$cursor,1;
178 $cursor++;
179 }
180 }
181 elsif ($escape eq '[A') { # Cursor up
182 $history_add->($return);
183 print "\b" x $cursor;
184 print " " x length($return);
185 print "\b" x length($return);
186  
187 $history_index ++
188 if defined $history[$history_index]
189 && $history[$history_index] eq $return;
190 $history_index = 0
191 unless defined $history[$history_index];
192  
193 $return = $history[$history_index];
194 $cursor = length($return);
195 print $return;
196 $history_index++;
197 }
198 elsif ($escape eq '[3~') { # Del
199 if ($cursor != length($return)) {
200 substr $return,$cursor,1,'';
201 print substr $return,$cursor;
202 print " ".(("\b") x (length($return) - $cursor + 1));
203 }
204 }
205 elsif ($escape eq 'OH') { # Pos 1
206 print (("\b") x $cursor);
207 $cursor = 0;
208 }
209 elsif ($escape eq 'OF') { # End
210 print substr $return,$cursor;
211 $cursor = length($return);
212 }
213 #else {
214 # print $escape;
215 #}
216 } else {
217 $history_add->($return);
218 next TRY_STRING;
219 }
220  
221 } elsif ($key_code == 127) { # Backspace
222 if ($cursor == 0) { # Ignore first
223 next KEY_STRING;
224 }
225 $cursor--;
226 substr $return,$cursor,1,''; # string
227 print "\b".substr $return,$cursor; # print
228 print " ".(("\b") x (length($return) - $cursor + 1)); # cursor
229 } else { # Character
230 if ($key_code <= 31) { # ignore controll chars
231 print "\a";
232 next KEY_STRING;
233 } elsif (defined $allowed
234 && $key !~ /$allowed/) {
235 print "\a";
236 next KEY_STRING;
237 }
238 substr $return,$cursor,0,$key; # string
239 print substr $return,$cursor; # print
240 $cursor++;
241 print (("\b") x (length($return) - $cursor)); # cursor
242 }
243 }
244 }
245 ReadMode 0;
246  
247 return $return;
248 }
249  
250 sub cmd_term_read_bool {
251 my ($self) = @_;
252  
253 my $label = $self->cmd_term_label_full;
254 my $return;
255  
256 if (defined $Term::ANSIColor::VERSION) {
257 say Term::ANSIColor::color('white bold').$label.' :'.Term::ANSIColor::color('reset');
258 } else {
259 say $label.": ";
260 }
261 ReadMode 4; # change to raw input mode
262 TRY:
263 while (1) {
264 1 while defined ReadKey -1; # discard any previous input
265 my $key = ReadKey 0; # read a single character
266 if ($key =~ /^[yn]$/i) {
267 say uc($key);
268 $return = uc($key) eq 'Y' ? 1:0;
269 last;
270 } elsif ((ord($key) == 10 || ord($key) == 27) && ! $self->is_required) {
271 last;
272 } elsif (ord($key) == 3) {
273 ReadMode 0;
274 kill INT => $$; # Not sure ?
275 }
276 }
277 ReadMode 0;
278  
279 return $return;
280 }
281  
282 around 'cmd_tags_list' => sub {
283 my $orig = shift;
284 my ($self) = @_;
285  
286 my @tags = $self->$orig();
287  
288 push(@tags,'Term')
289 if $self->can('cmd_term')
290 && $self->cmd_term;
291  
292 return @tags;
293 };
294  
295 {
296 package Moose::Meta::Attribute::Custom::Trait::AppTerm;
297  
298 use strict;
299 use warnings;
300  
301 sub register_implementation { return 'MooseX::App::Plugin::Term::Meta::Attribute' }
302 }
303  
304 1;