| 1 | package Term::Completion; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | require Exporter; | |||||||||||||||||||||||||||
| 9 | 3 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 10 | 3 | our @EXPORT_OK = qw(Complete); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | our $VERSION = '1.00'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | our %DEFAULTS = ( | |||||||||||||||||||||||||||
| 15 | # input/output channels | ||||||||||||||||||||||||||||
| 16 | in => \*STDIN, | ||||||||||||||||||||||||||||
| 17 | out => \*STDOUT, | ||||||||||||||||||||||||||||
| 18 | # key definitions | ||||||||||||||||||||||||||||
| 19 | tab => qr/\t/, | ||||||||||||||||||||||||||||
| 20 | list => qr/\cd/, | ||||||||||||||||||||||||||||
| 21 | 'kill' => qr/\cu/, | ||||||||||||||||||||||||||||
| 22 | erase => qr/[\177\010]/, # BS and DEL | ||||||||||||||||||||||||||||
| 23 | wipe => qr/\cw/, | ||||||||||||||||||||||||||||
| 24 | enter => qr/[\r\n]/, | ||||||||||||||||||||||||||||
| 25 | up => qr/\cp|\x1b\[[AD]/, # CTRL-p, up arrow, left arrow | ||||||||||||||||||||||||||||
| 26 | down => qr/\cn|\x1b\[[BC]/, # CTRL-n, down arrow, right arrow | ||||||||||||||||||||||||||||
| 27 | # key definitions for paging | ||||||||||||||||||||||||||||
| 28 | quit => qr/[\ccq]/, # CTRL-C or q | ||||||||||||||||||||||||||||
| 29 | # output parameters | ||||||||||||||||||||||||||||
| 30 | prompt => '', | ||||||||||||||||||||||||||||
| 31 | columns => 80, # default, if no Term::Size available | ||||||||||||||||||||||||||||
| 32 | rows => 24, | ||||||||||||||||||||||||||||
| 33 | bell => "\a", | ||||||||||||||||||||||||||||
| 34 | page_str => '--more--', | ||||||||||||||||||||||||||||
| 35 | eol => "\r\n", | ||||||||||||||||||||||||||||
| 36 | del_one => "\b \b", | ||||||||||||||||||||||||||||
| 37 | # help | ||||||||||||||||||||||||||||
| 38 | help => undef, | ||||||||||||||||||||||||||||
| 39 | helptext => undef, | ||||||||||||||||||||||||||||
| 40 | # default: empty list of choices | ||||||||||||||||||||||||||||
| 41 | choices => [], | ||||||||||||||||||||||||||||
| 42 | default => '' | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # selection which TTY handler to use | ||||||||||||||||||||||||||||
| 46 | sub import | ||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||
| 48 | 4 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=9 SCAR=24.1
| |||||||||||||||||||||||||||||
| 49 | 4 | my @syms; | |||||||||||||||||||||||||||
| 50 | # TODO Win32? | ||||||||||||||||||||||||||||
| 51 | 4 | ▶ | my $termhandler = ($^O !~ /interix/i ? 'Term::Completion::_readkey' : | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 'Term::Completion::_POSIX'); | ||||||||||||||||||||||||||||
| 53 | 4 | foreach(@_) { | |||||||||||||||||||||||||||
| 54 | 3 | ▶ | if(/^:posix$/) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 55 | 1 | $termhandler = 'Term::Completion::_POSIX'; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | elsif(/^:stty$/) { | ||||||||||||||||||||||||||||
| 58 | 0 | $termhandler = 'Term::Completion::_stty'; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | elsif(/^:readkey$/) { | ||||||||||||||||||||||||||||
| 61 | 1 | $termhandler = 'Term::Completion::_readkey'; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | ▶ | elsif(/^:DEFAULT$/ || !/^:/) { | |||||||||||||||||||||||||||
|
Condition33%1/3
/^:DEFAULT$/ or not /^:/
MC/DC0%0/2
/^:DEFAULT$/ or not /^:/
/^:DEFAULT$/
not /^:/
Truth table33%1/3
A or not(B)
A= /^:DEFAULT$/B= not /^:/
| |||||||||||||||||||||||||||||
| 64 | 0 | push(@syms, $_); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | else { | ||||||||||||||||||||||||||||
| 67 | 1 | croak __PACKAGE__ . " does not export '$_'"; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 3 | eval "require $termhandler;"; | |||||||||||||||||||||||||||
| 71 | 3 | ▶ | if($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | croak "Cannot initialize ".__PACKAGE__.", error occurred while loading auxiliary class $termhandler:\n$@"; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 3 | push(@ISA, $termhandler); | |||||||||||||||||||||||||||
| 75 | 3 | $class->export_to_level(1, $class, @syms); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub _get_defaults | ||||||||||||||||||||||||||||
| 79 | { | ||||||||||||||||||||||||||||
| 80 | 2 | ▶ | my %def = %DEFAULTS; | ||||||||||||||||||||||||||
|
Subroutine
_get_defaults: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | 2 | delete @def{qw(columns rows)}; | |||||||||||||||||||||||||||
| 82 | 2 | return %def; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | sub new | ||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||
| 87 | 2 | ▶ | my __PACKAGE__ $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 2 | ▶ | if(ref $class) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | $class = ref $class; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 2 | my %args = @_; | |||||||||||||||||||||||||||
| 93 | 2 | my $this = bless({$class->_get_defaults, %args}, $class); | |||||||||||||||||||||||||||
| 94 | 2 | return $this; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | #sub DESTROY | ||||||||||||||||||||||||||||
| 98 | #{ | ||||||||||||||||||||||||||||
| 99 | # my __PACKAGE__ $this = shift; | ||||||||||||||||||||||||||||
| 100 | # 1; | ||||||||||||||||||||||||||||
| 101 | #} | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # old style interface | ||||||||||||||||||||||||||||
| 104 | sub Complete | ||||||||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | my $prompt = shift; | ||||||||||||||||||||||||||
|
Subroutine
Complete: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | $prompt = '' unless defined $prompt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | my @choices; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | if (ref $_[0] || $_[0] =~ /^\*/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $_[0] or $_[0] =~ /^\*/
MC/DC0%0/2
ref $_[0] or $_[0] =~ /^\*/
ref $_[0]
$_[0] =~ /^\*/
Truth table0%0/3
A or B
A= ref $_[0]B= $_[0] =~ /^\*/
| |||||||||||||||||||||||||||||
| 111 | 0 | @choices = sort @{$_[0]}; | |||||||||||||||||||||||||||
| 112 | } else { | ||||||||||||||||||||||||||||
| 113 | 0 | @choices = sort(@_); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | __PACKAGE__->new( | |||||||||||||||||||||||||||
| 117 | prompt => $prompt, | ||||||||||||||||||||||||||||
| 118 | choices => \@choices | ||||||||||||||||||||||||||||
| 119 | )->complete; | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # sub get_key | ||||||||||||||||||||||||||||
| 123 | # virtual - defined in tty driver classes | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | sub show_help | ||||||||||||||||||||||||||||
| 126 | { | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | my __PACKAGE__ $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
show_help: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | my $text = $this->{helptext} || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$this->{'helptext'} || ''
MC/DC0%0/1
$this->{'helptext'} || ''
$this->{'helptext'}
Truth table0%0/2
A
A= $this->{'helptext'}
| |||||||||||||||||||||||||||||
| 129 | 0 | $text =~ s/\r?\n|\n?\r/$this->{eol}/g; | |||||||||||||||||||||||||||
| 130 | 0 | $this->{out}->print($text); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | sub complete | ||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | my __PACKAGE__ $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
complete: not called CC=44 SCAR=75.9
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | my $return = $this->{default}; | |||||||||||||||||||||||||||
| 138 | 0 | my $r = length($return); | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if(defined $this->{helptext} && !defined $this->{help}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $this->{"helptext"} and not defined $this->{"help"}
MC/DC0%0/2
defined $this->{"helptext"} and not defined $this->{"help"}
defined $this->{"helptext"}
not defined $this->{"help"}
Truth table0%0/3
A and not(B)
A= defined $this->{"helptext"}B= not defined $this->{"help"}
| |||||||||||||||||||||||||||||
| 141 | 0 | $this->show_help(); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # we grab full control of the terminal, switch off echo | ||||||||||||||||||||||||||||
| 145 | 0 | $this->set_raw_tty(); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | my $tab_pressed = 0; # repeated tab counter | |||||||||||||||||||||||||||
| 148 | 0 | my $choice_num; # selector | |||||||||||||||||||||||||||
| 149 | my @choice_cycle; | ||||||||||||||||||||||||||||
| 150 | 0 | my $eof = 0; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # handle terminal size changes | ||||||||||||||||||||||||||||
| 153 | # save any existing signal handler | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | if(exists $SIG{'WINCH'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | $this->{_sig_winch} = $SIG{WINCH}; | |||||||||||||||||||||||||||
| 156 | # set new signal handler | ||||||||||||||||||||||||||||
| 157 | local $SIG{'WINCH'} = sub { | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | if($this->{_sig_winch}) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | &{$this->{_sig_winch}}; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | # write new prompt and completion line | ||||||||||||||||||||||||||||
| 162 | 0 | $this->{out}->print($this->{eol}, $this->{prompt}, $return); | |||||||||||||||||||||||||||
| 163 | 0 | }; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # main loop for completion | ||||||||||||||||||||||||||||
| 167 | LOOP: { | ||||||||||||||||||||||||||||
| 168 | 0 | local $_ = ''; | |||||||||||||||||||||||||||
| 169 | 0 | $this->{out}->print($this->{prompt}, $return); | |||||||||||||||||||||||||||
| 170 | 0 | my $key; | |||||||||||||||||||||||||||
| 171 | 0 | ▶ | GETC: while (defined($key = $this->get_key) && ($_ .= $key, $_ !~ $this->{enter})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined($key = $this->get_key) and $_ .= $key, !($_ =~ /$this->{"enter"}/)
MC/DC0%0/2
defined($key = $this->get_key) and $_ .= $key, !($_ =~ /$this->{"enter"}/)
defined($key = $this->get_key)
$_ .= $key, !($_ =~ /$this->{"enter"}/)
Truth table0%0/3
A and B
A= defined($key = $this->get_key)B= $_ .= $key, !($_ =~ /$this->{"enter"}/)
| |||||||||||||||||||||||||||||
| 172 | CASE: { | ||||||||||||||||||||||||||||
| 173 | # deal with arrow key escape sequences | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | if(/^\x1b([^\[])/ || /^\x1b\[(?:[A-Z]|\d+~)(.)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
/^\x1b([^\[])/ or /^\x1b\[(?:[A-Z]|\d+~)(.)/
MC/DC0%0/2
/^\x1b([^\[])/ or /^\x1b\[(?:[A-Z]|\d+~)(.)/
/^\x1b([^\[])/
/^\x1b\[(?:[A-Z]|\d+~)(.)/
Truth table0%0/3
A or B
A= /^\x1b([^\[])/B= /^\x1b\[(?:[A-Z]|\d+~)(.)/
| |||||||||||||||||||||||||||||
| 175 | # unknown ESC sequence: just keep the last key typed | ||||||||||||||||||||||||||||
| 176 | 0 | $_ = $1; | |||||||||||||||||||||||||||
| 177 | 0 | redo CASE; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | # (TAB) attempt completion | ||||||||||||||||||||||||||||
| 181 | 0 | ▶ | $_ =~ $this->{tab} && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | if($tab_pressed++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | $this->show_choices($return); | |||||||||||||||||||||||||||
| 184 | 0 | redo LOOP; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | 0 | my @match = $this->get_choices($return); | |||||||||||||||||||||||||||
| 187 | 0 | ▶ | if (@match == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | # sound bell if there is no match | ||||||||||||||||||||||||||||
| 189 | 0 | $this->bell(); | |||||||||||||||||||||||||||
| 190 | } else { | ||||||||||||||||||||||||||||
| 191 | 0 | my $l = length(my $test = shift(@match)); | |||||||||||||||||||||||||||
| 192 | 0 | ▶ | if(@match) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 193 | # sound bell if multiple choices | ||||||||||||||||||||||||||||
| 194 | 0 | $this->bell(); | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | elsif($this->{delim}) { | ||||||||||||||||||||||||||||
| 197 | 0 | $test .= $this->{delim}; | |||||||||||||||||||||||||||
| 198 | 0 | $l++; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | 0 | foreach my $cmp (@match) { | |||||||||||||||||||||||||||
| 201 | 0 | ▶ | until (substr($cmp, 0, $l) eq substr($test, 0, $l)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 0 | $l--; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | 0 | my $add = $l - $r; | |||||||||||||||||||||||||||
| 206 | 0 | ▶ | if($add) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 0 | $this->{out}->print($test = substr($test, $r, $add)); | |||||||||||||||||||||||||||
| 208 | # reset counter if something was added | ||||||||||||||||||||||||||||
| 209 | 0 | $tab_pressed = 0; | |||||||||||||||||||||||||||
| 210 | 0 | $choice_num = undef; | |||||||||||||||||||||||||||
| 211 | 0 | $return .= $test; | |||||||||||||||||||||||||||
| 212 | 0 | $r += $add; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | 0 | last CASE; | |||||||||||||||||||||||||||
| 216 | }; | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 0 | $tab_pressed = 0; # reset repeated tab counter | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | # (^D) completion list | ||||||||||||||||||||||||||||
| 221 | 0 | ▶ | $_ =~ $this->{list} && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 0 | $this->show_choices($return); | |||||||||||||||||||||||||||
| 223 | 0 | redo LOOP; | |||||||||||||||||||||||||||
| 224 | }; | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | # on-demand help | ||||||||||||||||||||||||||||
| 227 | 0 | ▶ | if(defined $this->{help}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 0 | ▶ | $_ =~ $this->{help} && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 0 | ▶ | if(defined $this->{helptext}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 0 | $this->{out}->print($this->{eol}); | |||||||||||||||||||||||||||
| 231 | 0 | $this->show_help(); | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | 0 | redo LOOP; | |||||||||||||||||||||||||||
| 234 | }; | ||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | # (^U) kill | ||||||||||||||||||||||||||||
| 238 | 0 | ▶ | $_ =~ $this->{'kill'} && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 0 | ▶ | if ($r) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | # start over on a new line | ||||||||||||||||||||||||||||
| 241 | 0 | $r = 0; | |||||||||||||||||||||||||||
| 242 | 0 | $return = ""; | |||||||||||||||||||||||||||
| 243 | 0 | $this->{out}->print($this->{eol}); | |||||||||||||||||||||||||||
| 244 | 0 | $choice_num = undef; | |||||||||||||||||||||||||||
| 245 | 0 | redo LOOP; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | 0 | last CASE; | |||||||||||||||||||||||||||
| 248 | }; | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | # (DEL) || (BS) erase | ||||||||||||||||||||||||||||
| 251 | 0 | ▶ | $_ =~ $this->{erase} && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 0 | ▶ | if($r) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | 0 | $this->{out}->print($this->{del_one}); | |||||||||||||||||||||||||||
| 254 | 0 | chop($return); | |||||||||||||||||||||||||||
| 255 | 0 | $r--; | |||||||||||||||||||||||||||
| 256 | 0 | $choice_num = undef; | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | 0 | last CASE; | |||||||||||||||||||||||||||
| 259 | }; | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | # ^W wipe until separator | ||||||||||||||||||||||||||||
| 262 | 0 | ▶ | $_ =~ $this->{wipe} && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 0 | ▶ | if($r) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 0 | my $sep = ''; | |||||||||||||||||||||||||||
| 265 | 0 | ▶ | $sep = $this->{sep} if defined $this->{sep}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | 0 | ▶ | $sep .= $this->{delim} if defined $this->{delim}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 0 | ▶ | if(length($sep) && $return =~ s/((?:^|[$sep$sep]+)[^$sep$sep]*[$sep$sep]*)$//s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $sep and $return =~ s/((?:^|[$sep$sep]+)[^$sep$sep]*[$sep$sep]*)$//s
MC/DC0%0/2
length $sep and $return =~ s/((?:^|[$sep$sep]+)[^$sep$sep]*[$sep$sep]*)$//s
length $sep
$return =~ s/((?:^|[$sep$sep]+)[^$sep$sep]*[$sep$sep]*)$//s
Truth table0%0/3
A and B
A= length $sepB= $return =~ s/((?:^|[$sep$sep]+)[^$sep$sep]*[$sep$sep]*)$//s
| |||||||||||||||||||||||||||||
| 268 | 0 | my $cut = $1; | |||||||||||||||||||||||||||
| 269 | 0 | $this->{out}->print($this->{del_one} x length($cut)); | |||||||||||||||||||||||||||
| 270 | 0 | $r = length($return); | |||||||||||||||||||||||||||
| 271 | 0 | $choice_num = undef; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | 0 | last CASE; | |||||||||||||||||||||||||||
| 275 | }; | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | # up (CTRL-P) | ||||||||||||||||||||||||||||
| 278 | 0 | ▶ | $_ =~ $this->{up} && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | 0 | ▶ | unless(defined $choice_num) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 0 | @choice_cycle = $this->get_choices($return); | |||||||||||||||||||||||||||
| 281 | 0 | ▶ | if(defined $choice_cycle[$#choice_cycle]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 0 | $choice_num = $#choice_cycle; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | } else { | ||||||||||||||||||||||||||||
| 285 | 0 | ▶ | if($choice_num <= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 0 | $choice_num = @choice_cycle; # TODO get_choices returns number in scalar context? | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | 0 | $choice_num--; | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | #TODO only delete/print differences, not full string | ||||||||||||||||||||||||||||
| 291 | 0 | ▶ | unless(defined $choice_num) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 0 | $this->bell(); | |||||||||||||||||||||||||||
| 293 | } else { | ||||||||||||||||||||||||||||
| 294 | 0 | $this->{out}->print($this->{del_one} x length($return)); | |||||||||||||||||||||||||||
| 295 | 0 | $return = $choice_cycle[$choice_num]; | |||||||||||||||||||||||||||
| 296 | 0 | $this->{out}->print($return); | |||||||||||||||||||||||||||
| 297 | 0 | $r = length($return); | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | 0 | last CASE; | |||||||||||||||||||||||||||
| 300 | }; | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | # down (CTRL-N) | ||||||||||||||||||||||||||||
| 303 | 0 | ▶ | $_ =~ $this->{down} && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 304 | 0 | ▶ | unless(defined $choice_num) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | 0 | @choice_cycle = $this->get_choices($return); | |||||||||||||||||||||||||||
| 306 | 0 | ▶ | if(defined $choice_cycle[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | 0 | $choice_num = 0; | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | } else { | ||||||||||||||||||||||||||||
| 310 | 0 | ▶ | if(++$choice_num >= @choice_cycle) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | 0 | $choice_num = 0; | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | #TODO only delete/print differences, not full string | ||||||||||||||||||||||||||||
| 315 | 0 | ▶ | unless(defined $choice_num) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | 0 | $this->bell(); | |||||||||||||||||||||||||||
| 317 | } else { | ||||||||||||||||||||||||||||
| 318 | 0 | $this->{out}->print($this->{del_one} x length($return)); | |||||||||||||||||||||||||||
| 319 | 0 | $return = $choice_cycle[$choice_num]; | |||||||||||||||||||||||||||
| 320 | 0 | $this->{out}->print($return); | |||||||||||||||||||||||||||
| 321 | 0 | $r = length($return); | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | 0 | last CASE; | |||||||||||||||||||||||||||
| 324 | }; | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | # printable char | ||||||||||||||||||||||||||||
| 327 | 0 | ▶ | ord >= 32 && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | 0 | $return .= $_; | |||||||||||||||||||||||||||
| 329 | 0 | $r++; | |||||||||||||||||||||||||||
| 330 | 0 | $this->{out}->print($_); | |||||||||||||||||||||||||||
| 331 | 0 | $choice_num = undef; | |||||||||||||||||||||||||||
| 332 | 0 | last CASE; | |||||||||||||||||||||||||||
| 333 | }; | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 0 | ▶ | $_ !~ /^\x1b/ && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | # sound bell and reset any unknown key | ||||||||||||||||||||||||||||
| 337 | 0 | $this->bell(); | |||||||||||||||||||||||||||
| 338 | 0 | $_ = ''; | |||||||||||||||||||||||||||
| 339 | }; | ||||||||||||||||||||||||||||
| 340 | 0 | next GETC; # nothing matched - get new character | |||||||||||||||||||||||||||
| 341 | } # :ESAC | ||||||||||||||||||||||||||||
| 342 | 0 | $_ = ''; | |||||||||||||||||||||||||||
| 343 | } # while getc != enter | ||||||||||||||||||||||||||||
| 344 | 0 | $this->{out}->print($this->{eol}); | |||||||||||||||||||||||||||
| 345 | 0 | $return = $this->post_process($return); | |||||||||||||||||||||||||||
| 346 | # only validate if we had input | ||||||||||||||||||||||||||||
| 347 | 0 | ▶ | my $match = defined($key) ? $this->validate($return) : $return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 0 | ▶ | unless(defined $match) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 349 | 0 | redo LOOP; | |||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | 0 | $return = $match; | |||||||||||||||||||||||||||
| 352 | } # end LOOP | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 0 | $this->reset_tty; | |||||||||||||||||||||||||||
| 355 | 0 | delete $this->{_sig_winch}; | |||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 0 | return $return; | |||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | sub validate | ||||||||||||||||||||||||||||
| 361 | { | ||||||||||||||||||||||||||||
| 362 | 0 | ▶ | my __PACKAGE__ $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
validate: not called CC=27 SCAR=66.3
Pod
documented
| |||||||||||||||||||||||||||||
| 363 | 0 | my $return = shift; | |||||||||||||||||||||||||||
| 364 | 0 | ▶ | unless($this->{validate}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 365 | 0 | return $return; | |||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||
| 367 | 0 | elsif(ref $this->{validate}) { | |||||||||||||||||||||||||||
| 368 | # arrayref with message to print and code ref | ||||||||||||||||||||||||||||
| 369 | 0 | my ($msg, $cb) = @{$this->{validate}}; | |||||||||||||||||||||||||||
| 370 | 0 | my $match = &$cb($return); | |||||||||||||||||||||||||||
| 371 | 0 | ▶ | unless(defined $match) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | 0 | $this->{out}->print($msg,$this->{eol}); | |||||||||||||||||||||||||||
| 373 | 0 | return; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | 0 | return $match; | |||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | # we may have several validation options | ||||||||||||||||||||||||||||
| 379 | 0 | my @vals = split(/[\s,]+/, $this->{validate}); | |||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 0 | VALIDATE_OPTIONS: foreach my $val (@vals) { | |||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 0 | ▶ | if($val eq 'lowercase') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 0 | $return = lc($return); | |||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 0 | ▶ | if($val eq 'uppercase') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 388 | 0 | $return = uc($return); | |||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 0 | ▶ | if($val eq 'match_one') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 392 | 0 | my @choices = $this->get_choices(''); | |||||||||||||||||||||||||||
| 393 | 0 | my @matches = grep(/^\Q$return\E/, @choices); | |||||||||||||||||||||||||||
| 394 | MATCH: { | ||||||||||||||||||||||||||||
| 395 | 0 | ▶ | if(@matches == 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 396 | # unique match at beginning | ||||||||||||||||||||||||||||
| 397 | 0 | $return = $matches[0]; | |||||||||||||||||||||||||||
| 398 | 0 | last MATCH; | |||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | elsif(@matches == 0) { | ||||||||||||||||||||||||||||
| 401 | 0 | @matches = grep(/\Q$return\E/, @choices); | |||||||||||||||||||||||||||
| 402 | 0 | ▶ | if(@matches == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 403 | # unique match anywhere | ||||||||||||||||||||||||||||
| 404 | 0 | $return = $matches[0]; | |||||||||||||||||||||||||||
| 405 | 0 | last MATCH; | |||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | 0 | $this->{out}->print("ERROR: Answer '$return' does not match a unique item!",$this->{eol}); | |||||||||||||||||||||||||||
| 409 | 0 | $return = undef; | |||||||||||||||||||||||||||
| 410 | 0 | last VALIDATE_OPTIONS; | |||||||||||||||||||||||||||
| 411 | } # MATCH | ||||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 0 | ▶ | if($val eq 'nonempty') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 415 | 0 | ▶ | unless(length $return) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 416 | 0 | $this->{out}->print("ERROR: Empty input not allowed!",$this->{eol}); | |||||||||||||||||||||||||||
| 417 | 0 | $return = undef; | |||||||||||||||||||||||||||
| 418 | 0 | last VALIDATE_OPTIONS; | |||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | 0 | ▶ | if($val eq 'nonblank') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 423 | 0 | ▶ | unless(length $return && $return =~ /\S/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $return and $return =~ /\S/
MC/DC0%0/2
length $return and $return =~ /\S/
length $return
$return =~ /\S/
Truth table0%0/3
A and B
A= length $returnB= $return =~ /\S/
| |||||||||||||||||||||||||||||
| 424 | 0 | $this->{out}->print("ERROR: Blank input not allowed!",$this->{eol}); | |||||||||||||||||||||||||||
| 425 | 0 | $return = undef; | |||||||||||||||||||||||||||
| 426 | 0 | last VALIDATE_OPTIONS; | |||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | 0 | ▶ | if($val eq 'fromchoices') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 431 | 0 | ▶ | if(length($return) && !grep($return eq $_, $this->get_choices(''))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $return and not grep(($return eq $_), $this->get_choices(""))
MC/DC0%0/2
length $return and not grep(($return eq $_), $this->get_choices(""))
length $return
not grep(($return eq $_), $this->get_choices(""))
Truth table0%0/3
A and not(B)
A= length $returnB= not grep(($return eq $_), $this->get_choices(""))
| |||||||||||||||||||||||||||||
| 432 | 0 | $this->{out}->print("ERROR: You must choose one item from the list!",$this->{eol}); | |||||||||||||||||||||||||||
| 433 | 0 | $return = undef; | |||||||||||||||||||||||||||
| 434 | 0 | last VALIDATE_OPTIONS; | |||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | 0 | ▶ | if($val eq 'numeric') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 439 | 0 | ▶ | unless($return =~ /^-?(?:\.\d+|\d+\.?\d*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 440 | 0 | $this->{out}->print("ERROR: Value must be numeric!",$this->{eol}); | |||||||||||||||||||||||||||
| 441 | 0 | $return = undef; | |||||||||||||||||||||||||||
| 442 | 0 | last VALIDATE_OPTIONS; | |||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | 0 | ▶ | if($val eq 'integer') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 447 | 0 | ▶ | unless($return =~ /^-?\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 448 | 0 | $this->{out}->print("ERROR: Value must be an integer number!",$this->{eol}); | |||||||||||||||||||||||||||
| 449 | 0 | $return = undef; | |||||||||||||||||||||||||||
| 450 | 0 | last VALIDATE_OPTIONS; | |||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | 0 | ▶ | if($val eq 'nonzero') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 455 | 0 | ▶ | if($return == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 456 | 0 | $this->{out}->print("ERROR: Value must be a non-zero value!",$this->{eol}); | |||||||||||||||||||||||||||
| 457 | 0 | $return = undef; | |||||||||||||||||||||||||||
| 458 | 0 | last VALIDATE_OPTIONS; | |||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | 0 | ▶ | if($val eq 'positive') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 463 | 0 | ▶ | unless($return > 0.0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 464 | 0 | $this->{out}->print("ERROR: Value must be a positive value!",$this->{eol}); | |||||||||||||||||||||||||||
| 465 | 0 | $return = undef; | |||||||||||||||||||||||||||
| 466 | 0 | last VALIDATE_OPTIONS; | |||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | } # end validation options | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | # TODO die on unknown validate option? | ||||||||||||||||||||||||||||
| 473 | 0 | return $return; | |||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | sub bell | ||||||||||||||||||||||||||||
| 477 | { | ||||||||||||||||||||||||||||
| 478 | 0 | ▶ | my __PACKAGE__ $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
bell: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 479 | 0 | my $bell = $this->{bell}; | |||||||||||||||||||||||||||
| 480 | 0 | ▶ | $this->{out}->print($bell) if $bell; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | sub get_choices | ||||||||||||||||||||||||||||
| 484 | { | ||||||||||||||||||||||||||||
| 485 | 0 | ▶ | my __PACKAGE__ $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_choices: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 486 | 0 | ▶ | grep(defined && /^\Q$_[0]/,@{$this->{choices}}); | ||||||||||||||||||||||||||
|
Condition0%0/3
defined $_ && /^\Q$_[0]\E/
MC/DC0%0/2
defined $_ && /^\Q$_[0]\E/
defined $_
/^\Q$_[0]\E/
Truth table0%0/3
A && B
A= defined $_B= /^\Q$_[0]\E/
| |||||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | sub show_choices | ||||||||||||||||||||||||||||
| 490 | { | ||||||||||||||||||||||||||||
| 491 | 0 | ▶ | my __PACKAGE__ $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
show_choices: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 492 | 0 | my $return = shift; | |||||||||||||||||||||||||||
| 493 | # start new line - cursor was on input line | ||||||||||||||||||||||||||||
| 494 | 0 | $this->{out}->print($this->{eol}); | |||||||||||||||||||||||||||
| 495 | 0 | $this->_show_choices($this->get_choices($return)); | |||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | sub _show_choices { | ||||||||||||||||||||||||||||
| 499 | 0 | ▶ | my __PACKAGE__ $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
_show_choices: not called CC=18 SCAR=58.3
| |||||||||||||||||||||||||||||
| 500 | 0 | my @choices = @_; | |||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | 0 | my $eol = $this->{eol}; | |||||||||||||||||||||||||||
| 503 | 0 | ▶ | unless(@choices) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 504 | 0 | return 1; | |||||||||||||||||||||||||||
| 505 | } | ||||||||||||||||||||||||||||
| 506 | 0 | ▶ | if(defined $this->{columns} && $this->{columns} == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $this->{"columns"} and $this->{"columns"} == 0
MC/DC0%0/2
defined $this->{"columns"} and $this->{"columns"} == 0
defined $this->{"columns"}
$this->{"columns"} == 0
Truth table0%0/3
A and B
A= defined $this->{"columns"}B= $this->{"columns"} == 0
| |||||||||||||||||||||||||||||
| 507 | # poor man's solution: | ||||||||||||||||||||||||||||
| 508 | 0 | $this->{out}->print(join($eol, @choices), $eol); | |||||||||||||||||||||||||||
| 509 | 0 | return 1; | |||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | # find width of widest entry | ||||||||||||||||||||||||||||
| 513 | 0 | my $MAXWIDTH = 0; | |||||||||||||||||||||||||||
| 514 | 0 | ▶ | grep(length > $MAXWIDTH && ($MAXWIDTH = length), @choices); | ||||||||||||||||||||||||||
|
Condition0%0/3
length $_ > $MAXWIDTH && ($MAXWIDTH = length $_)
MC/DC0%0/2
length $_ > $MAXWIDTH && ($MAXWIDTH = length $_)
length $_ > $MAXWIDTH
($MAXWIDTH = length $_)
Truth table0%0/3
A && B
A= length $_ > $MAXWIDTHB= ($MAXWIDTH = length $_)
| |||||||||||||||||||||||||||||
| 515 | 0 | $MAXWIDTH++; # add one for a blank between the columns | |||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | 0 | ▶ | if(exists $SIG{'WINCH'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 518 | 0 | $this->{_winch} = 0; | |||||||||||||||||||||||||||
| 519 | local $SIG{'WINCH'} = sub { | ||||||||||||||||||||||||||||
| 520 | 0 | ▶ | $this->{_winch}++; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 521 | 0 | ▶ | if($this->{_sig_winch}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 522 | 0 | return &{$this->{_sig_winch}}; | |||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||
| 524 | 0 | }; | |||||||||||||||||||||||||||
| 525 | } | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | 0 | my ($COLUMNS,$ROWS) = ($this->{columns}, $this->{rows}); | |||||||||||||||||||||||||||
| 528 | START_PAGING: { | ||||||||||||||||||||||||||||
| 529 | 0 | ▶ | ($COLUMNS,$ROWS) = $this->get_term_size() | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$COLUMNS and $ROWS
MC/DC0%0/2
$COLUMNS and $ROWS
$COLUMNS
$ROWS
Truth table0%0/3
A and B
A= $COLUMNSB= $ROWS
| |||||||||||||||||||||||||||||
| 530 | unless $COLUMNS && $ROWS; | ||||||||||||||||||||||||||||
| 531 | 0 | my $maxwidth = $MAXWIDTH; | |||||||||||||||||||||||||||
| 532 | 0 | ▶ | my $columns = $maxwidth >= $COLUMNS ? 1 : int($COLUMNS / $maxwidth); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | ## if there's enough margin to intersperse among the columns, do so. | ||||||||||||||||||||||||||||
| 535 | 0 | $maxwidth += int(($COLUMNS % $maxwidth) / $columns); | |||||||||||||||||||||||||||
| 536 | 0 | my $lines = int((@choices + $columns - 1) / $columns); | |||||||||||||||||||||||||||
| 537 | 0 | ▶ | $columns-- while ((($lines * $columns) - @choices + 1) > $lines); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | 0 | my $i = 0; | |||||||||||||||||||||||||||
| 540 | 0 | my $page_lines = 0; | |||||||||||||||||||||||||||
| 541 | 0 | ▶ | for (my $l = 0; $l < $lines; $l++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 542 | 0 | my @line; | |||||||||||||||||||||||||||
| 543 | 0 | ▶ | for(my $c = 0; $c < $columns && $i<@choices; $c++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$c < $columns and $i < @choices
MC/DC0%0/2
$c < $columns and $i < @choices
$c < $columns
$i < @choices
Truth table0%0/3
A and B
A= $c < $columnsB= $i < @choices
| |||||||||||||||||||||||||||||
| 544 | 0 | push(@line, sprintf("%-${maxwidth}s", $_[$i++])); | |||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||
| 546 | # no paging if ROWS were set to 0 | ||||||||||||||||||||||||||||
| 547 | 0 | ▶ | if($ROWS && ++$page_lines == $ROWS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ROWS and ++$page_lines == $ROWS
MC/DC0%0/2
$ROWS and ++$page_lines == $ROWS
$ROWS
++$page_lines == $ROWS
Truth table0%0/3
A and B
A= $ROWSB= ++$page_lines == $ROWS
| |||||||||||||||||||||||||||||
| 548 | 0 | $this->{out}->print($this->{page_str}); | |||||||||||||||||||||||||||
| 549 | 0 | my $c = $this->get_key; | |||||||||||||||||||||||||||
| 550 | # delete pager line | ||||||||||||||||||||||||||||
| 551 | 0 | $this->{out}->print($this->{del_one} x length($this->{page_str})); | |||||||||||||||||||||||||||
| 552 | 0 | ▶ | if($c =~ $this->{quit}) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 553 | 0 | return 1; | |||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||
| 555 | elsif($this->{_winch}) { | ||||||||||||||||||||||||||||
| 556 | # winch signaled, restart paging | ||||||||||||||||||||||||||||
| 557 | 0 | $this->{_winch} = 0; | |||||||||||||||||||||||||||
| 558 | 0 | $this->bell(); | |||||||||||||||||||||||||||
| 559 | 0 | $this->{out}->print($this->{eol}); | |||||||||||||||||||||||||||
| 560 | 0 | $COLUMNS = $ROWS = undef; | |||||||||||||||||||||||||||
| 561 | 0 | redo START_PAGING; | |||||||||||||||||||||||||||
| 562 | } | ||||||||||||||||||||||||||||
| 563 | elsif($c =~ $this->{enter}) { | ||||||||||||||||||||||||||||
| 564 | 0 | $page_lines--; | |||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||
| 566 | else { | ||||||||||||||||||||||||||||
| 567 | 0 | $page_lines = 0; | |||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||
| 569 | } | ||||||||||||||||||||||||||||
| 570 | 0 | $this->{out}->print(@line, $eol); | |||||||||||||||||||||||||||
| 571 | } # end loop over lines | ||||||||||||||||||||||||||||
| 572 | } # end START_PAGING | ||||||||||||||||||||||||||||
| 573 | 0 | 1; | |||||||||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | sub post_process | ||||||||||||||||||||||||||||
| 577 | { | ||||||||||||||||||||||||||||
| 578 | 0 | ▶ | my __PACKAGE__ $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
post_process: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 579 | 0 | my $return = shift; | |||||||||||||||||||||||||||
| 580 | 0 | $return =~ s/^\s+|\s+$//sg; | |||||||||||||||||||||||||||
| 581 | 0 | $return; | |||||||||||||||||||||||||||
| 582 | } | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | 3 | 1; | |||||||||||||||||||||||||||
| 585 | __END__ | ||||||||||||||||||||||||||||