blib/lib/MYDan/VSSH/Comp.pm untested

filter: Statementstmt 0.0% 0 / 308 Branchbran 0.0% 0 / 108 Conditioncond 0.0% 0 / 100 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 524 CC 55 SCAR 76.0
CC
55
Cov
0%
complete
2450.0
get_term_size
56.0
BEGIN
2.0
CRAP 2007.5
1 package MYDan::VSSH::Comp;
2 use base Term::Completion;
3  
4 use strict;
5 use warnings;
6  
7 use MYDan::VSSH::History;
8  
9  
10 sub get_term_size
11 {
12 my ( $r, $c ) = ( 0, 0 );
13 if ( $^O ne "solaris" )
14 {
15 my $size = eval { `stty size` };
16 if ( $size =~ /^\s*(\d+)\s+(\d+)\s*/ )
17 {
18 ( $r, $c ) = ( $1, $2 );
19 }
20 }
21 else
22 {
23 eval {
24 foreach (`stty`)
25 {
26 if (/^rows = (\d+); columns = (\d+)/) {
27 $r = $1;
28 $c = $2;
29 last;
30 }
31 }
32 };
33 }
34  
35 if ( $r == 0 || $c == 0 )
36 {
37 if ( exists $ENV{'LINES'} ) { $r = $ENV{'LINES'}; }
38 else { $r = 80; } # this is often wrong
39 if ( exists $ENV{'COLUMNS'} ) { $c = $ENV{'COLUMNS'}; }
40 else { $c = 80; }
41 }
42  
43 return ( $r, $c );
44 }
45  
46 sub complete
47 {
48 my __PACKAGE__ $this = shift;
49  
50 my $return = $this->{default};
51 my $r = length($return);
52  
53 if(defined $this->{helptext} && !defined $this->{help}) {
54 $this->show_help();
55 }
56  
57 # we grab full control of the terminal, switch off echo
58 $this->set_raw_tty();
59  
60 my $tab_pressed = 0; # repeated tab counter
61 my $choice_num; # selector
62 my @choice_cycle;
63 my $eof = 0;
64  
65 # handle terminal size changes
66 # save any existing signal handler
67  
68  
69 if(exists $SIG{'WINCH'}) {
70 $this->{_sig_winch} = $SIG{WINCH};
71 # set new signal handler
72 local $SIG{'WINCH'} = sub {
73 if($this->{_sig_winch}) {
74 &{$this->{_sig_winch}};
75 }
76 # write new prompt and completion line
77 $this->{out}->print($this->{eol}, $this->{prompt}, $return);
78 };
79 }
80  
81 my ( undef, $col_len) = get_term_size();
82 my $prompt_len = length $this->{prompt};
83 my $last_len = $col_len - $prompt_len -2;
84 my $cut_len = int ( $col_len /3 );
85  
86 my $prompt_l = $this->{prompt};
87 $prompt_l =~ s/sh\$$/<<</;
88  
89 my $danger = 0;
90 my $reverse_prompt = '(reverse-i-search)';
91  
92 # main loop for completion
93 my ( $lr, $reverse, $reverse_2 ) = 0;
94 LOOP: {
95 local $_ = '';
96 $this->{out}->print($this->{prompt}, $return);
97 my $key;
98 GETC: while (defined($key = $this->get_key) && ($_ .= $key, $_ !~ $this->{enter})) {
99 CASE: {
100 # deal with arrow key escape sequences
101 if(/^\x1b([^\[])/ || /^\x1b\[(?:[A-Z]|\d+~)(.)/) {
102 # unknown ESC sequence: just keep the last key typed
103 $_ = $1;
104 redo CASE;
105 }
106  
107 $_ =~ $this->{'reverse'} && do {
108 $this->{out}->print( $this->{del_one} x $col_len , "\033[K", $reverse_prompt." :" );
109 ( $return, $reverse, $reverse_2 ) = ( '', 1, '' );
110 $danger = 1;
111 last CASE;
112 };
113  
114 if( $reverse )
115 {
116 $_ =~ /^[a-zA-Z0-9 -\/]$/ && do {
117 $this->{out}->print( $this->{del_one} x $col_len , "\033[K");
118 for my $his ( reverse @MYDan::VSSH::History::HISTORY )
119 {
120 if( $his =~ /$reverse_2$_/ )
121 {
122 $reverse_2 .= $_;
123 $return = $his;
124 my %tmp;
125 @choice_cycle =
126 grep{ /$reverse_2/ && $tmp{$_} == 1 }
127 map{ $tmp{$_} ++; $_}
128 @MYDan::VSSH::History::HISTORY;
129 $choice_num = $#choice_cycle;
130 last;
131 }
132 }
133
134 my $show = "$reverse_prompt `$reverse_2': $return";
135 my $len = length $show;
136
137 ($len > ( $col_len -2 ) )
138 ? $this->{out}->print( substr( $show , 0 , ( $col_len -2 ) ), ">>")
139 : $this->{out}->print( $show );
140 ( $r, $lr ) = ( length( $return), 0 );
141
142 last CASE;
143 };
144
145 $_ =~ $this->{erase} && do {
146 $this->{out}->print( $this->{del_one} x $col_len , "\033[K");
147 chop $reverse_2;
148 for my $his ( reverse @MYDan::VSSH::History::HISTORY )
149 {
150 if( $his =~ /$reverse_2/ )
151 {
152 $return = $his;
153 my %tmp;
154 @choice_cycle =
155 grep{ /$reverse_2/ && $tmp{$_} == 1 }
156 map{ $tmp{$_} ++; $_}
157 @MYDan::VSSH::History::HISTORY;
158 $choice_num = 0;
159 last;
160 }
161 }
162 my $show = "$reverse_prompt `$reverse_2': $return";
163 my $len = length $show;
164
165 ($len > ( $col_len -2 ) )
166 ? $this->{out}->print( substr( $show , 0 , ( $col_len -2 ) ), ">>")
167 : $this->{out}->print( $show );
168
169 ( $r, $lr ) = ( length( $return), 0 );
170 last CASE;
171 };
172
173 $_ =~ $this->{up} && do {
174 if($choice_num <= 0) {
175 $this->bell();
176 }
177 else{ $choice_num-- };
178 $return ||='';
179 $return = $choice_cycle[$choice_num]||'';
180 $this->{out}->print( $this->{del_one} x $col_len , "\033[K");
181
182 my $show = "$reverse_prompt: $return";
183 my $len = length $show;
184 ($len > ( $col_len -2 ) )
185 ? $this->{out}->print( substr( $show , 0 , ( $col_len -2 ) ), ">>")
186 : $this->{out}->print( $show );
187 ( $r, $lr ) = ( length( $return), 0 );
188 last CASE;
189 };
190
191 $_ =~ $this->{down} && do {
192 if(++$choice_num >= @choice_cycle) {
193 $choice_num = $#choice_cycle;
194 $this->bell();
195 }
196 $return ||='';
197 $this->{out}->print( $this->{del_one} x $col_len , "\033[K");
198 $return = $choice_cycle[$choice_num] ||'';
199
200 my $show = "$reverse_prompt: $return";
201 my $len = length $show;
202 ($len > ( $col_len -2 ) )
203 ? $this->{out}->print( substr( $show , 0, ( $col_len -2 ) ), ">>")
204 : $this->{out}->print( $show );
205 ( $r, $lr ) = ( length( $return), 0 );
206 last CASE;
207 };
208
209 ( $_ =~ $this->{left} || $_ =~ $this->{right} ) && do {
210 $this->{out}->print($this->{del_one} x $col_len, "\033[K");
211  
212 if( $r >= $last_len )
213 {
214 $this->{out}->print(
215 $prompt_l, substr( $return, ( $r - $last_len ) , $last_len ),);
216 }
217 else
218 {
219 $this->{out}->print( $this->{prompt}, $return);
220 }
221
222 $reverse = 0;
223 last CASE;
224 };
225 }
226 else{
227  
228 # (TAB) attempt completion
229 $_ =~ $this->{tab} && do {
230 if($tab_pressed++) {
231 $this->show_choices($return);
232 redo LOOP;
233 }
234 my @match = $this->get_choices($return||'');
235 if (@match == 0) {
236 # sound bell if there is no match
237 $this->bell();
238 } else {
239 my $l = length(my $test = shift(@match));
240 if(@match) {
241 # sound bell if multiple choices
242 $this->bell();
243 }
244 elsif($this->{delim}) {
245 $test .= $this->{delim};
246 $l++;
247 }
248 foreach my $cmp (@match) {
249 until (substr($cmp, 0, $l) eq substr($test, 0, $l)) {
250 $l--;
251 }
252 }
253 my $add = $l - $r;
254 if($add) {
255 $this->{out}->print($test = substr($test, $r, $add));
256 # reset counter if something was added
257 $tab_pressed = 0;
258 $choice_num = undef;
259 $return .= $test;
260 $r += $add;
261 }
262 }
263 last CASE;
264 };
265  
266 $tab_pressed = 0; # reset repeated tab counter
267  
268 # (^D) completion list
269 $_ =~ $this->{list} && do {
270 $this->reset_tty();
271 exit 0;
272 };
273  
274 # on-demand help
275 if(defined $this->{help}) {
276 $_ =~ $this->{help} && do {
277 if(defined $this->{helptext}) {
278 $this->{out}->print($this->{eol});
279 $this->show_help();
280 }
281 redo LOOP;
282 };
283 }
284  
285 # (^U) kill
286 $_ =~ $this->{'kill'} && do {
287 if ($r) {
288 $this->{out}->print($this->{eol});
289 $choice_num = undef;
290 ( $return, $r, $lr ) = ( '', 0, 0 );
291 redo LOOP;
292 }
293 last CASE;
294 };
295  
296 # (^L) clear
297 $_ =~ $this->{'clear'} && do {
298 system 'clear';
299 $this->{out}->print($this->{prompt});
300 ( $return, $r, $lr ) = ( '', 0, 0 );
301 last CASE;
302 };
303 # (^L) quit
304 $_ =~ $this->{'quit'} && do {
305 $this->{out}->print("\n");
306 $return = '';
307 redo LOOP;
308 #last CASE;
309 };
310  
311 $_ =~ $this->{'wipe'} && do {
312 if( $r && $r <= $last_len && !$lr )
313 {
314 $return =~ s/(\s*[^\s]+\s*)$//;
315 $this->{out}->print( $this->{del_one} x length($1));
316 $r = length($return);
317 }
318 last CASE;
319 };
320  
321 $_ =~ $this->{up} && do {
322 @choice_cycle = @MYDan::VSSH::History::HISTORY;
323 last CASE unless @choice_cycle;
324 unless(defined $choice_num) {
325 $choice_num = $#choice_cycle;
326 } else {
327 if($choice_num <= 0) {
328 $this->bell();
329 }
330 else{ $choice_num-- };
331 }
332 $return = $choice_cycle[$choice_num];
333 $lr =0;
334 $r = length($return);
335  
336 $this->{out}->print($this->{del_one} x $col_len , "\033[K" );
337  
338 if( length( $return ) >= $last_len )
339 {
340 $this->{out}->print(
341 $prompt_l,substr( $return, ( length($return) - $last_len ) , $last_len) );
342 }
343 else
344 {
345 $this->{out}->print( $this->{prompt}, $return );
346 }
347 last CASE;
348 };
349  
350 # down (CTRL-N)
351 $_ =~ $this->{down} && do {
352 @choice_cycle = @MYDan::VSSH::History::HISTORY;
353 last CASE unless @choice_cycle;
354 unless(defined $choice_num) {
355 $this->bell();
356 last CASE;
357 } else {
358 if(++$choice_num >= @choice_cycle) {
359 $choice_num = $#choice_cycle;
360 $this->bell();
361 }
362 }
363 #TODO only delete/print differences, not full string
364 $return = $choice_cycle[$choice_num];
365 $lr =0;
366 $r = length($return);
367  
368 $this->{out}->print($this->{del_one} x $col_len, "\033[K");
369  
370 if( length( $return ) >= $last_len )
371 {
372 $this->{out}->print(
373 $prompt_l, substr( $return, ( length($return) - $last_len ) , $last_len) );
374 }
375 else
376 {
377 $this->{out}->print( $this->{prompt}, $return );
378 }
379
380 last CASE;
381 };
382  
383  
384 $_ =~ $this->{left} && do {
385 unless( $lr >= $r )
386 {
387 $lr ++;
388 if( $r >= $last_len )
389 {
390 $this->{out}->print( $this->{del_one} x $col_len, "\033[K" );
391  
392 my $post = $r - $lr;
393 if( $post >= $last_len )
394 {
395 $this->{out}->print(
396 $prompt_l,substr( $return, ( $post - $last_len ) , $last_len),
397 ">>\033[2D" );
398 }
399 else
400 {
401 my $back_cur = $last_len + 2 - $post;
402 $this->{out}->print(
403 $this->{prompt}, substr( $return, 0, $last_len),
404 ">>"."\033[".$back_cur."D" );
405 }
406 }
407 else
408 {
409 $this->{out}->print( "\033[1D" );
410 }
411 }
412 last CASE;
413 };
414  
415 $_ =~ $this->{right} && do {
416 if( $lr )
417 {
418 $lr --;
419 if( $r >= $last_len )
420 {
421 $this->{out}->print( $this->{del_one} x $col_len, "\033[K" );
422 my $post = $r - $lr;
423  
424 if( $post >= $last_len )
425 {
426 $this->{out}->print(
427 $prompt_l, substr( $return, ( $post - $last_len ) , $last_len ),
428 ">>\033[2D");
429 }
430 else
431 {
432 my $back_cur = $last_len + 2 - $post;
433 $this->{out}->print(
434 $this->{prompt}, substr( $return, 0, $last_len),
435 ">>"."\033[".$back_cur."D");
436 }
437 }
438 else
439 {
440 $this->{out}->print("\033[1C");
441 }
442
443 }
444 else
445 {
446 $this->{out}->print( "\033[K" );
447 }
448 last CASE;
449 };
450  
451 # (DEL)
452 $_ =~ $this->{erase} && do {
453 if($r && $r > $lr) {
454  
455 $choice_num = undef;
456  
457 if( $lr )
458 {
459 $this->{out}->print( $this->{del_one} x $col_len, "\033[K" );
460  
461 $danger = 1;
462 my $post = $r - $lr;
463 substr( $return, ( $post -1 ), 1 ) = '';
464 $r--;
465  
466 unless( $r > $last_len )
467 {
468 $this->{out}->print( $this->{prompt}, $return, "\033[".$lr."D");
469 }
470 elsif( $post >= $last_len )
471 {
472 $this->{out}->print(
473 $prompt_l, substr( $return, ( $post - $last_len ) , $last_len ),
474 ">>\033[2D");
475 }
476 else
477 {
478 my $back_cur = $last_len - $post +1;
479 $this->{out}->print(
480 $this->{prompt}, substr( $return, 0, $last_len ),
481 "\033[".$back_cur."D");
482 }
483 }
484 else
485 {
486 chop($return);
487 $r--;
488 if( $r > $last_len )
489 {
490 $this->{out}->print( $this->{del_one} x $col_len, "\033[K" );
491 $this->{out}->print(
492 $prompt_l, substr( $return, ( $r - $last_len ) )
493 );
494 }
495 else
496 {
497 $this->{out}->print( $this->{del_one} );
498 }
499 }
500 }
501 last CASE;
502 };
503  
504
505 ord >= 32 && do {
506 if( $lr )
507 {
508 $danger = 1;
509 $this->{out}->print($this->{del_one} x $col_len, "\033[K");
510 my $post = $r - $lr;
511 substr( $return, $post, 0 ) = $_;
512  
513 if( $r >= $last_len )
514 {
515 if( $post >= $last_len )
516 {
517 $this->{out}->print(
518 $prompt_l, substr( $return, ( $post - $last_len ) , $last_len ),
519 ">>\033[2D");
520 }
521 else
522 {
523 my $back_cur = $last_len + 2 - $post;
524 $this->{out}->print(
525 $this->{prompt}, substr( $return, 1, $last_len ),
526 ">>"."\033[".$back_cur."D");
527 }
528 }
529 else
530 {
531 $this->{out}->print( $this->{prompt}, $return, "\033[".$lr."D");
532 }
533 }
534 else
535 {
536 if( $r >= $last_len )
537 {
538 $this->{out}->print(
539 $this->{del_one} x $col_len, $prompt_l,
540 substr( $return, ( $r - $last_len +1 ) )
541 );
542 }
543  
544 $return .= $_;
545 $this->{out}->print($_);
546 }
547  
548 $r++;
549 $choice_num = undef;
550 last CASE;
551 };
552  
553 $_ !~ /^\x1b/ && do {
554 # sound bell and reset any unknown key
555 $this->bell();
556 $_ = '';
557 };
558 }#last else
559 next GETC; # nothing matched - get new character
560 } # :ESAC
561 $_ = '';
562 } # while getc != enter
563 $this->{out}->print($this->{eol});
564 $return = $this->post_process($return);
565 # only validate if we had input
566 my $match = defined($key) ? $this->validate($return) : $return;
567 unless(defined $match) {
568 redo LOOP;
569 }
570 $return = $match;
571 } # end LOOP
572  
573 $this->reset_tty;
574 delete $this->{_sig_winch};
575  
576 return ( $return, $danger );
577 }
578  
579 1;