Completion/Multi.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 86 CC 5 SCAR 19.9
CC
5
Cov
0%
validate
20.0
Complete
6.0
_get_defaults
2.0
CRAP 7.3
1 package Term::Completion::Multi;
2
3 use strict;
4 use warnings;
5 use File::Spec;
6
7 our $VERSION = '0.90';
8
9 our @EXPORT_OK = qw(Complete);
10 use base qw(Term::Completion);
11
12 our %DEFAULTS = (
13 delim => ' '
14 );
15
16 sub _get_defaults
17 {
18 return(__PACKAGE__->SUPER::_get_defaults(), %DEFAULTS, delim => $DEFAULTS{delim});
19 }
20
21 sub complete
22 {
23 my __PACKAGE__ $this = shift;
24 my $result = $this->SUPER::complete(@_);
25 my $delim = $this->{delim};
26 return split(/[$delim]+/, $result);
27 }
28
29 sub Complete
30 {
31 my ($prompt,@choices) = @_;
32 $prompt = '' unless defined $prompt;
33 __PACKAGE__->new(prompt => $prompt, choices => \@choices)->complete;
34 }
35
36 sub get_choices
37 {
38 my __PACKAGE__ $this = shift;
39 my $in = shift;
40 my $delim = $this->{delim};
41 my $prefix = '';
42 # remove all up to and including last separator
43 ($in =~ s/^(.*[$delim]+)//) && ($prefix = $1);
44 #... and use that to match the choices
45 map { $prefix.$_ } grep(/^\Q$in/,@{$this->{choices}});
46 }
47
48 sub show_choices
49 {
50 my __PACKAGE__ $this = shift;
51 my $return = shift;
52 # start new line - cursor was on input line
53 $this->{out}->print($this->{eol});
54 my $delim = $this->{delim};
55 $return =~ s/^.*[$delim]//; # delete everything up to last delimiter
56 $this->_show_choices($this->get_choices($return));
57 }
58
59 sub post_process
60 {
61 my __PACKAGE__ $this = shift;
62 my $return = $this->SUPER::post_process(shift);
63 my $delim = $this->{delim};
64 $return =~ s/^[$delim]+|[$delim]+$//g;
65 $return;
66 }
67
68 sub validate
69 {
70 my __PACKAGE__ $this = shift;
71 my $return = shift;
72 unless($this->{validate}) {
73 return $return;
74 }
75 my $ok = 1;
76 my $delim = $this->{delim};
77 my @ok_vals;
78 foreach my $val (split(/[$delim]+/, $return)) {
79 my $return = $this->SUPER::validate($val);
80 unless(defined $return) {
81 $ok = 0;
82 } else {
83 push(@ok_vals, $return);
84 }
85 }
86 return unless $ok;
87 return join($delim, @ok_vals);
88 }
89
90 1;
91
92 __END__