| 1 | package Term::ReadLine::Tiny; | ||||||||||||||||||||||||||||
| 2 | =head1 NAME | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | Term::ReadLine::Tiny - Tiny implementation of ReadLine | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 VERSION | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | version 1.07 | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | use Term::ReadLine::Tiny; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | $term = Term::ReadLine::Tiny->new(); | ||||||||||||||||||||||||||||
| 15 | while ( defined($_ = $term->readline("Prompt: ")) ) | ||||||||||||||||||||||||||||
| 16 | { | ||||||||||||||||||||||||||||
| 17 | print "$_\n"; | ||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | print "\n"; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | $s = ""; | ||||||||||||||||||||||||||||
| 22 | while ( defined($_ = $term->readkey(1)) ) | ||||||||||||||||||||||||||||
| 23 | { | ||||||||||||||||||||||||||||
| 24 | $s .= $_; | ||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | print "\n$s\n"; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | This package is a native perls implementation of ReadLine that doesn't need any library such as 'Gnu ReadLine'. | ||||||||||||||||||||||||||||
| 31 | Also fully supports UTF-8, details in L<UTF-8 section|https://metacpan.org/pod/Term::ReadLine::Tiny#UTF-8>. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head2 Keys | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | B<C<Enter> or C<^J> or C<^M>:> Gets input line. Returns the line unless C<EOF> or aborting or error, otherwise undef. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | B<C<BackSpace> or C<^H> or C<^?>:> Deletes one character behind cursor. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | B<C<UpArrow>:> Changes line to previous history line. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | B<C<DownArrow>:> Changes line to next history line. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | B<C<RightArrow>:> Moves cursor forward to one character. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | B<C<LeftArrow>:> Moves cursor back to one character. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | B<C<Home> or C<^A>:> Moves cursor to the start of the line. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | B<C<End> or C<^E>:> Moves cursor to the end of the line. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | B<C<PageUp>:> Change line to first line of history. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | B<C<PageDown>:> Change line to latest line of history. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | B<C<Insert>:> Switch typing mode between insert and overwrite. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | B<C<Delete>:> Deletes one character at cursor. Does nothing if no character at cursor. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | B<C<Tab> or C<^I>:> Completes line automatically by history. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | B<C<^D>:> Aborts the operation. Returns C<undef>. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||
| 64 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | use v5.10.1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | use feature qw(switch); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | 1 | ▶ | no if ($] >= 5.018), 'warnings' => 'experimental'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | 1 | require utf8; | |||||||||||||||||||||||||||
| 70 | 1 | require PerlIO; | |||||||||||||||||||||||||||
| 71 | 1 | require Term::ReadLine; | |||||||||||||||||||||||||||
| 72 | 1 | require Term::ReadKey; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | BEGIN | ||||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||
| 77 | 1 | ▶ | require Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | 1 | our $VERSION = '1.07'; | |||||||||||||||||||||||||||
| 79 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 80 | 1 | our @EXPORT = qw(); | |||||||||||||||||||||||||||
| 81 | 1 | our @EXPORT_OK = qw(); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head1 Standard Methods and Functions | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head2 ReadLine() | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | returns the actual package that executes the commands. If this package is used, the value is C<Term::ReadLine::Tiny>. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||||||||||
| 94 | sub ReadLine | ||||||||||||||||||||||||||||
| 95 | { | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | return __PACKAGE__; | ||||||||||||||||||||||||||
|
Subroutine
ReadLine: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head2 new([$appname[, IN[, OUT]]]) | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | returns the handle for subsequent calls to following functions. | ||||||||||||||||||||||||||||
| 102 | Argument I<appname> is the name of the application B<but not supported yet>. | ||||||||||||||||||||||||||||
| 103 | Optionally can be followed by two arguments for IN and OUT filehandles. These arguments should be globs. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | This routine may also get called via C<Term::ReadLine-E<gt>new()> if you have $ENV{PERL_RL} set to 'Tiny'. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =cut | ||||||||||||||||||||||||||||
| 108 | sub new | ||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 0 | my ($appname, $IN, $OUT) = @_; | |||||||||||||||||||||||||||
| 112 | 0 | my $self = {}; | |||||||||||||||||||||||||||
| 113 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | $self->{readmode} = ''; | |||||||||||||||||||||||||||
| 116 | 0 | $self->{history} = []; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | $self->{features} = {}; | |||||||||||||||||||||||||||
| 119 | #$self->{features}->{appname} = $appname; | ||||||||||||||||||||||||||||
| 120 | 0 | $self->{features}->{addhistory} = 1; | |||||||||||||||||||||||||||
| 121 | 0 | $self->{features}->{minline} = 1; | |||||||||||||||||||||||||||
| 122 | 0 | $self->{features}->{autohistory} = 1; | |||||||||||||||||||||||||||
| 123 | 0 | $self->{features}->{gethistory} = 1; | |||||||||||||||||||||||||||
| 124 | 0 | $self->{features}->{sethistory} = 1; | |||||||||||||||||||||||||||
| 125 | 0 | $self->{features}->{changehistory} = 1; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | $self->newTTY($IN, $OUT); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | return $self; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub DESTROY | ||||||||||||||||||||||||||||
| 133 | { | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | if ($self->{readmode}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | { | ||||||||||||||||||||||||||||
| 137 | 0 | Term::ReadKey::ReadMode('restore', $self->{IN}); | |||||||||||||||||||||||||||
| 138 | 0 | $self->{readmode} = ''; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head2 readline([$prompt[, $default]]) | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | interactively gets an input line. Trailing newline is removed. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Returns C<undef> on C<EOF>. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||||||||||
| 149 | sub readline | ||||||||||||||||||||||||||||
| 150 | { | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
readline: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 0 | my ($prompt, $default) = @_; | |||||||||||||||||||||||||||
| 153 | 0 | ▶ | $prompt = "" unless defined($prompt); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | $default = "" unless defined($default); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | my ($in, $out, $history, $minline, $changehistory) = | ||||||||||||||||||||||||||||
| 156 | 0 | ($self->{IN}, $self->{OUT}, $self->{history}, $self->{features}->{minline}, $self->{features}->{changehistory}); | |||||||||||||||||||||||||||
| 157 | 0 | ▶ | unless (-t $in) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | { | ||||||||||||||||||||||||||||
| 159 | 0 | my $line = <$in>; | |||||||||||||||||||||||||||
| 160 | 0 | ▶ | chomp $line if defined $line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | return $line; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 0 | local $\ = undef; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | $self->{readmode} = 'cbreak'; | |||||||||||||||||||||||||||
| 166 | 0 | Term::ReadKey::ReadMode($self->{readmode}, $self->{IN}); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | my @line; | |||||||||||||||||||||||||||
| 169 | 0 | my ($line, $index) = ("", 0); | |||||||||||||||||||||||||||
| 170 | 0 | my $history_index; | |||||||||||||||||||||||||||
| 171 | 0 | my $ins_mode = 0; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | my $write = sub { | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | my ($text, $ins) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 175 | 0 | my $s; | |||||||||||||||||||||||||||
| 176 | 0 | my @a = @line[$index..$#line]; | |||||||||||||||||||||||||||
| 177 | 0 | my $a = substr($line, $index); | |||||||||||||||||||||||||||
| 178 | 0 | @line = @line[0..$index-1]; | |||||||||||||||||||||||||||
| 179 | 0 | $line = substr($line, 0, $index); | |||||||||||||||||||||||||||
| 180 | 0 | print $out " "; | |||||||||||||||||||||||||||
| 181 | 0 | print $out "\e[D"; | |||||||||||||||||||||||||||
| 182 | 0 | print $out "\e[J"; | |||||||||||||||||||||||||||
| 183 | 0 | for my $c (split("", $text)) | |||||||||||||||||||||||||||
| 184 | { | ||||||||||||||||||||||||||||
| 185 | 0 | $s = encode_controlchar($c); | |||||||||||||||||||||||||||
| 186 | 0 | ▶ | unless ($ins) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | { | ||||||||||||||||||||||||||||
| 188 | 0 | print $out $s; | |||||||||||||||||||||||||||
| 189 | 0 | push @line, $s; | |||||||||||||||||||||||||||
| 190 | 0 | $line .= $c; | |||||||||||||||||||||||||||
| 191 | } else | ||||||||||||||||||||||||||||
| 192 | { | ||||||||||||||||||||||||||||
| 193 | 0 | my $i = $index-length($line); | |||||||||||||||||||||||||||
| 194 | 0 | $a[$i] = $s; | |||||||||||||||||||||||||||
| 195 | 0 | substr($a, $i, 1) = $c; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | 0 | $index++; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | 0 | ▶ | unless ($ins) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | { | ||||||||||||||||||||||||||||
| 201 | 0 | $s = join("", @a); | |||||||||||||||||||||||||||
| 202 | 0 | print $out $s; | |||||||||||||||||||||||||||
| 203 | 0 | print $out "\e[D" x length($s); | |||||||||||||||||||||||||||
| 204 | } else | ||||||||||||||||||||||||||||
| 205 | { | ||||||||||||||||||||||||||||
| 206 | 0 | $s = join("", @a); | |||||||||||||||||||||||||||
| 207 | 0 | print $out $s; | |||||||||||||||||||||||||||
| 208 | 0 | print $out "\e[D" x (length($s) - length(join("", @a[0..length($text)-1]))); | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | 0 | push @line, @a; | |||||||||||||||||||||||||||
| 211 | 0 | $line .= $a; | |||||||||||||||||||||||||||
| 212 | 0 | ▶ | if ($index >= length($line)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | { | ||||||||||||||||||||||||||||
| 214 | 0 | print $out " "; | |||||||||||||||||||||||||||
| 215 | 0 | print $out "\e[D"; | |||||||||||||||||||||||||||
| 216 | 0 | print $out "\e[J"; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | 0 | }; | |||||||||||||||||||||||||||
| 219 | my $print = sub { | ||||||||||||||||||||||||||||
| 220 | 0 | ▶ | my ($text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 221 | 0 | $write->($text, $ins_mode); | |||||||||||||||||||||||||||
| 222 | 0 | }; | |||||||||||||||||||||||||||
| 223 | my $set = sub { | ||||||||||||||||||||||||||||
| 224 | 0 | ▶ | my ($text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 225 | 0 | print $out "\e[D" x length(join("", @line[0..$index-1])); | |||||||||||||||||||||||||||
| 226 | 0 | print $out "\e[J"; | |||||||||||||||||||||||||||
| 227 | 0 | @line = (); | |||||||||||||||||||||||||||
| 228 | 0 | $line = ""; | |||||||||||||||||||||||||||
| 229 | 0 | $index = 0; | |||||||||||||||||||||||||||
| 230 | 0 | $write->($text); | |||||||||||||||||||||||||||
| 231 | 0 | }; | |||||||||||||||||||||||||||
| 232 | my $backspace = sub { | ||||||||||||||||||||||||||||
| 233 | 0 | ▶ | return if $index <= 0; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | 0 | my @a = @line[$index..$#line]; | |||||||||||||||||||||||||||
| 235 | 0 | my $a = substr($line, $index); | |||||||||||||||||||||||||||
| 236 | 0 | $index--; | |||||||||||||||||||||||||||
| 237 | 0 | print $out "\e[D" x length($line[$index]); | |||||||||||||||||||||||||||
| 238 | 0 | @line = @line[0..$index-1]; | |||||||||||||||||||||||||||
| 239 | 0 | $line = substr($line, 0, $index); | |||||||||||||||||||||||||||
| 240 | 0 | $write->($a); | |||||||||||||||||||||||||||
| 241 | 0 | print $out "\e[D" x length(join("", @a)); | |||||||||||||||||||||||||||
| 242 | 0 | $index -= scalar(@a); | |||||||||||||||||||||||||||
| 243 | 0 | }; | |||||||||||||||||||||||||||
| 244 | my $delete = sub { | ||||||||||||||||||||||||||||
| 245 | 0 | ▶ | my @a = @line[$index+1..$#line]; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 246 | 0 | my $a = substr($line, $index+1); | |||||||||||||||||||||||||||
| 247 | 0 | @line = @line[0..$index-1]; | |||||||||||||||||||||||||||
| 248 | 0 | $line = substr($line, 0, $index); | |||||||||||||||||||||||||||
| 249 | 0 | $write->($a); | |||||||||||||||||||||||||||
| 250 | 0 | print $out "\e[D" x length(join("", @a)); | |||||||||||||||||||||||||||
| 251 | 0 | $index -= scalar(@a); | |||||||||||||||||||||||||||
| 252 | 0 | }; | |||||||||||||||||||||||||||
| 253 | my $home = sub { | ||||||||||||||||||||||||||||
| 254 | 0 | ▶ | print $out "\e[D" x length(join("", @line[0..$index-1])); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 255 | 0 | $index = 0; | |||||||||||||||||||||||||||
| 256 | 0 | }; | |||||||||||||||||||||||||||
| 257 | my $end = sub { | ||||||||||||||||||||||||||||
| 258 | 0 | ▶ | my @a = @line[$index..$#line]; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 259 | 0 | my $a = substr($line, $index); | |||||||||||||||||||||||||||
| 260 | 0 | @line = @line[0..$index-1]; | |||||||||||||||||||||||||||
| 261 | 0 | $line = substr($line, 0, $index); | |||||||||||||||||||||||||||
| 262 | 0 | $write->($a); | |||||||||||||||||||||||||||
| 263 | 0 | }; | |||||||||||||||||||||||||||
| 264 | my $left = sub { | ||||||||||||||||||||||||||||
| 265 | 0 | ▶ | return if $index <= 0; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | 0 | print $out "\e[D" x length($line[$index-1]); | |||||||||||||||||||||||||||
| 267 | 0 | $index--; | |||||||||||||||||||||||||||
| 268 | 0 | }; | |||||||||||||||||||||||||||
| 269 | my $right = sub { | ||||||||||||||||||||||||||||
| 270 | 0 | ▶ | return if $index >= length($line); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 0 | print $out $line[$index]; | |||||||||||||||||||||||||||
| 272 | 0 | $index++; | |||||||||||||||||||||||||||
| 273 | 0 | ▶ | if ($index >= length($line)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | { | ||||||||||||||||||||||||||||
| 275 | 0 | print $out " "; | |||||||||||||||||||||||||||
| 276 | 0 | print $out "\e[D"; | |||||||||||||||||||||||||||
| 277 | 0 | print $out "\e[J"; | |||||||||||||||||||||||||||
| 278 | } else | ||||||||||||||||||||||||||||
| 279 | { | ||||||||||||||||||||||||||||
| 280 | 0 | print $out $line[$index]; | |||||||||||||||||||||||||||
| 281 | 0 | print $out "\e[D" x length($line[$index]); | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | 0 | }; | |||||||||||||||||||||||||||
| 284 | my $up = sub { | ||||||||||||||||||||||||||||
| 285 | 0 | ▶ | return if $history_index <= 0; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 0 | ▶ | $history->[$history_index] = $line if $changehistory; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | 0 | $history_index--; | |||||||||||||||||||||||||||
| 288 | 0 | $set->($history->[$history_index]); | |||||||||||||||||||||||||||
| 289 | 0 | }; | |||||||||||||||||||||||||||
| 290 | my $down = sub { | ||||||||||||||||||||||||||||
| 291 | 0 | ▶ | return if $history_index >= $#$history; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 0 | ▶ | $history->[$history_index] = $line if $changehistory; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | 0 | $history_index++; | |||||||||||||||||||||||||||
| 294 | 0 | $set->($history->[$history_index]); | |||||||||||||||||||||||||||
| 295 | 0 | }; | |||||||||||||||||||||||||||
| 296 | my $pageup = sub { | ||||||||||||||||||||||||||||
| 297 | 0 | ▶ | return if $history_index <= 0; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 0 | ▶ | $history->[$history_index] = $line if $changehistory; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | 0 | $history_index = 0; | |||||||||||||||||||||||||||
| 300 | 0 | $set->($history->[$history_index]); | |||||||||||||||||||||||||||
| 301 | 0 | }; | |||||||||||||||||||||||||||
| 302 | my $pagedown = sub { | ||||||||||||||||||||||||||||
| 303 | 0 | ▶ | return if $history_index >= $#$history; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 304 | 0 | ▶ | $history->[$history_index] = $line if $changehistory; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | 0 | $history_index = $#$history; | |||||||||||||||||||||||||||
| 306 | 0 | $set->($history->[$history_index]); | |||||||||||||||||||||||||||
| 307 | 0 | }; | |||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 0 | print $prompt; | |||||||||||||||||||||||||||
| 310 | 0 | $set->($default); | |||||||||||||||||||||||||||
| 311 | 0 | push @$history, $line; | |||||||||||||||||||||||||||
| 312 | 0 | $history_index = $#$history; | |||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 0 | my $result = undef; | |||||||||||||||||||||||||||
| 315 | 0 | my ($char, $esc) = ("", undef); | |||||||||||||||||||||||||||
| 316 | 0 | ▶ | while (defined($char = getc($in))) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | { | ||||||||||||||||||||||||||||
| 318 | 0 | ▶ | unless (defined($esc)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 319 | { | ||||||||||||||||||||||||||||
| 320 | 0 | given ($char) | |||||||||||||||||||||||||||
| 321 | { | ||||||||||||||||||||||||||||
| 322 | when (/\e/) | ||||||||||||||||||||||||||||
| 323 | 0 | { | |||||||||||||||||||||||||||
| 324 | 0 | $esc = ""; | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | when (/\x01/) # ^A | ||||||||||||||||||||||||||||
| 327 | 0 | { | |||||||||||||||||||||||||||
| 328 | 0 | $home->(); | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | when (/\x04/) # ^D | ||||||||||||||||||||||||||||
| 331 | 0 | { | |||||||||||||||||||||||||||
| 332 | 0 | $result = undef; | |||||||||||||||||||||||||||
| 333 | 0 | last; | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | when (/\x05/) # ^E | ||||||||||||||||||||||||||||
| 336 | 0 | { | |||||||||||||||||||||||||||
| 337 | 0 | $end->(); | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | when (/\t/) # ^I | ||||||||||||||||||||||||||||
| 340 | 0 | { | |||||||||||||||||||||||||||
| 341 | 0 | ▶ | for (my $i = $history_index; $i >= 0; $i--) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | { | ||||||||||||||||||||||||||||
| 343 | 0 | ▶ | if ($history->[$i] =~ /^$line/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | { | ||||||||||||||||||||||||||||
| 345 | 0 | $set->($history->[$i]); | |||||||||||||||||||||||||||
| 346 | 0 | last; | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | when (/\n|\r/) | ||||||||||||||||||||||||||||
| 351 | 0 | { | |||||||||||||||||||||||||||
| 352 | 0 | print $out $char; | |||||||||||||||||||||||||||
| 353 | 0 | $history->[$#$history] = $line; | |||||||||||||||||||||||||||
| 354 | 0 | ▶ | pop @$history unless defined($minline) and length($line) >= $minline; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $minline and length $line >= $minline
MC/DC0%0/2
defined $minline and length $line >= $minline
defined $minline
length $line >= $minline
Truth table0%0/3
A and B
A= defined $minlineB= length $line >= $minline
| |||||||||||||||||||||||||||||
| 355 | 0 | $result = $line; | |||||||||||||||||||||||||||
| 356 | 0 | last; | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | when (/[\b]|\x7F/) | ||||||||||||||||||||||||||||
| 359 | 0 | { | |||||||||||||||||||||||||||
| 360 | 0 | $backspace->(); | |||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | when (/[\x00-\x1F]|\x7F/) | ||||||||||||||||||||||||||||
| 363 | 0 | { | |||||||||||||||||||||||||||
| 364 | 0 | $print->($char); | |||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | default | ||||||||||||||||||||||||||||
| 367 | 0 | { | |||||||||||||||||||||||||||
| 368 | 0 | $print->($char); | |||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | 0 | next; | |||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||
| 373 | 0 | $esc .= $char; | |||||||||||||||||||||||||||
| 374 | 0 | ▶ | if ($esc =~ /^.\d?\D/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | { | ||||||||||||||||||||||||||||
| 376 | 0 | given ($esc) | |||||||||||||||||||||||||||
| 377 | { | ||||||||||||||||||||||||||||
| 378 | when (/^\[(A|0A)/) | ||||||||||||||||||||||||||||
| 379 | 0 | { | |||||||||||||||||||||||||||
| 380 | 0 | $up->(); | |||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | when (/^\[(B|0B)/) | ||||||||||||||||||||||||||||
| 383 | 0 | { | |||||||||||||||||||||||||||
| 384 | 0 | $down->(); | |||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | when (/^\[(C|0C)/) | ||||||||||||||||||||||||||||
| 387 | 0 | { | |||||||||||||||||||||||||||
| 388 | 0 | $right->(); | |||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | when (/^\[(D|0D)/) | ||||||||||||||||||||||||||||
| 391 | 0 | { | |||||||||||||||||||||||||||
| 392 | 0 | $left->(); | |||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | when (/^\[(H|0H)/) | ||||||||||||||||||||||||||||
| 395 | 0 | { | |||||||||||||||||||||||||||
| 396 | 0 | $home->(); | |||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | when (/^\[(F|0F)/) | ||||||||||||||||||||||||||||
| 399 | 0 | { | |||||||||||||||||||||||||||
| 400 | 0 | $end->(); | |||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | when (/^\[(\d)~/) | ||||||||||||||||||||||||||||
| 403 | 0 | { | |||||||||||||||||||||||||||
| 404 | 0 | given ($1) | |||||||||||||||||||||||||||
| 405 | { | ||||||||||||||||||||||||||||
| 406 | when (1) | ||||||||||||||||||||||||||||
| 407 | 0 | { | |||||||||||||||||||||||||||
| 408 | 0 | $home->(); | |||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | when (2) | ||||||||||||||||||||||||||||
| 411 | 0 | { | |||||||||||||||||||||||||||
| 412 | 0 | $ins_mode = not $ins_mode; | |||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | when (3) | ||||||||||||||||||||||||||||
| 415 | 0 | { | |||||||||||||||||||||||||||
| 416 | 0 | $delete->(); | |||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||
| 418 | when (4) | ||||||||||||||||||||||||||||
| 419 | 0 | { | |||||||||||||||||||||||||||
| 420 | 0 | $end->(); | |||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||
| 422 | when (5) | ||||||||||||||||||||||||||||
| 423 | 0 | { | |||||||||||||||||||||||||||
| 424 | 0 | $pageup->(); | |||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||
| 426 | when (6) | ||||||||||||||||||||||||||||
| 427 | 0 | { | |||||||||||||||||||||||||||
| 428 | 0 | $pagedown->(); | |||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | when (7) | ||||||||||||||||||||||||||||
| 431 | 0 | { | |||||||||||||||||||||||||||
| 432 | 0 | $home->(); | |||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||
| 434 | when (8) | ||||||||||||||||||||||||||||
| 435 | 0 | { | |||||||||||||||||||||||||||
| 436 | 0 | $end->(); | |||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||
| 438 | default | ||||||||||||||||||||||||||||
| 439 | 0 | { | |||||||||||||||||||||||||||
| 440 | #$print->("\e$esc"); | ||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | default | ||||||||||||||||||||||||||||
| 445 | 0 | { | |||||||||||||||||||||||||||
| 446 | #$print->("\e$esc"); | ||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||
| 449 | 0 | $esc = undef; | |||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | 0 | Term::ReadKey::ReadMode('restore', $self->{IN}); | |||||||||||||||||||||||||||
| 454 | 0 | $self->{readmode} = ''; | |||||||||||||||||||||||||||
| 455 | 0 | return $result; | |||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | =head2 addhistory($line1[, $line2[, ...]]) | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | B<AddHistory($line1[, $line2[, ...]])> | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | adds lines to the history of input. | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | =cut | ||||||||||||||||||||||||||||
| 465 | sub addhistory | ||||||||||||||||||||||||||||
| 466 | { | ||||||||||||||||||||||||||||
| 467 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
addhistory: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 468 | 0 | ▶ | if ($self->{features}->{utf8}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 469 | { | ||||||||||||||||||||||||||||
| 470 | 0 | ▶ | for (my $i = 0; $i < @_; $i++) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 471 | { | ||||||||||||||||||||||||||||
| 472 | 0 | utf8::decode($_[$i]); | |||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||
| 475 | 0 | push @{$self->{history}}, @_; | |||||||||||||||||||||||||||
| 476 | 0 | return (@_); | |||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||
| 478 | sub AddHistory | ||||||||||||||||||||||||||||
| 479 | { | ||||||||||||||||||||||||||||
| 480 | 0 | ▶ | return addhistory(@_); | ||||||||||||||||||||||||||
|
Subroutine
AddHistory: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | =head2 IN() | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | returns the filehandle for input. | ||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | =cut | ||||||||||||||||||||||||||||
| 488 | sub IN | ||||||||||||||||||||||||||||
| 489 | { | ||||||||||||||||||||||||||||
| 490 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
IN: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 491 | 0 | return $self->{IN}; | |||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | =head2 OUT() | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | returns the filehandle for output. | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | =cut | ||||||||||||||||||||||||||||
| 499 | sub OUT | ||||||||||||||||||||||||||||
| 500 | { | ||||||||||||||||||||||||||||
| 501 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
OUT: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 502 | 0 | return $self->{OUT}; | |||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | =head2 MinLine([$minline]) | ||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | B<minline([$minline])> | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | If argument is specified, it is an advice on minimal size of line to be included into history. | ||||||||||||||||||||||||||||
| 510 | C<undef> means do not include anything into history (autohistory off). | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | Returns the old value. | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | =cut | ||||||||||||||||||||||||||||
| 515 | sub MinLine | ||||||||||||||||||||||||||||
| 516 | { | ||||||||||||||||||||||||||||
| 517 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
MinLine: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 518 | 0 | my ($minline) = @_; | |||||||||||||||||||||||||||
| 519 | 0 | my $result = $self->{features}->{minline}; | |||||||||||||||||||||||||||
| 520 | 0 | ▶ | $self->{features}->{minline} = $minline if @_ >= 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 521 | 0 | $self->{features}->{autohistory} = defined($self->{features}->{minline}); | |||||||||||||||||||||||||||
| 522 | 0 | return $result; | |||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||
| 524 | sub minline | ||||||||||||||||||||||||||||
| 525 | { | ||||||||||||||||||||||||||||
| 526 | 0 | ▶ | return MinLine(@_); | ||||||||||||||||||||||||||
|
Subroutine
minline: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | =head2 findConsole() | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | returns an array with two strings that give most appropriate names for files for input and output using conventions C<"<$in">, C<">out">. | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | =cut | ||||||||||||||||||||||||||||
| 534 | sub findConsole | ||||||||||||||||||||||||||||
| 535 | { | ||||||||||||||||||||||||||||
| 536 | 0 | ▶ | return (Term::ReadLine::Stub::findConsole(@_)); | ||||||||||||||||||||||||||
|
Subroutine
findConsole: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | =head2 Attribs() | ||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | returns a reference to a hash which describes internal configuration of the package. B<Not supported in this package.> | ||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | =cut | ||||||||||||||||||||||||||||
| 544 | sub Attribs | ||||||||||||||||||||||||||||
| 545 | { | ||||||||||||||||||||||||||||
| 546 | 0 | ▶ | return {}; | ||||||||||||||||||||||||||
|
Subroutine
Attribs: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 547 | } | ||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||
| 549 | =head2 Features() | ||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||
| 551 | Returns a reference to a hash with keys being features present in current implementation. | ||||||||||||||||||||||||||||
| 552 | This features are present: | ||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | =over | ||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | =item * | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | I<appname> is not present and is the name of the application. B<But not supported yet.> | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | =item * | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | I<addhistory> is present, always C<TRUE>. | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | =item * | ||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | I<minline> is present, default 1. See C<MinLine> method. | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | =item * | ||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | I<autohistory> is present. C<FALSE> if minline is C<undef>. See C<MinLine> method. | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | =item * | ||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | I<gethistory> is present, always C<TRUE>. | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | =item * | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | I<sethistory> is present, always C<TRUE>. | ||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | =item * | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | I<changehistory> is present, default C<TRUE>. See C<changehistory> method. | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | =item * | ||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | I<utf8> is present. C<TRUE> if input file handle has C<:utf8> layer. | ||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | =back | ||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | =cut | ||||||||||||||||||||||||||||
| 591 | sub Features | ||||||||||||||||||||||||||||
| 592 | { | ||||||||||||||||||||||||||||
| 593 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
Features: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 594 | 0 | my %features = %{$self->{features}}; | |||||||||||||||||||||||||||
| 595 | 0 | return \%features; | |||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | =head1 Additional Methods and Functions | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | =cut | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | =head2 newTTY([$IN[, $OUT]]) | ||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | takes two arguments which are input filehandle and output filehandle. Switches to use these filehandles. | ||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||
| 606 | =cut | ||||||||||||||||||||||||||||
| 607 | sub newTTY | ||||||||||||||||||||||||||||
| 608 | { | ||||||||||||||||||||||||||||
| 609 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
newTTY: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 610 | 0 | my ($IN, $OUT) = @_; | |||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | 0 | my ($console, $consoleOUT) = findConsole(); | |||||||||||||||||||||||||||
| 613 | 0 | ▶ | my $console_utf8 = defined($ENV{LANG}) && $ENV{LANG} =~ /\.UTF\-?8$/i; | ||||||||||||||||||||||||||
|
Condition0%0/3
defined $ENV{'LANG'} && $ENV{'LANG'} =~ /\.UTF\-?8$/i
MC/DC0%0/2
defined $ENV{'LANG'} && $ENV{'LANG'} =~ /\.UTF\-?8$/i
defined $ENV{'LANG'}
$ENV{'LANG'} =~ /\.UTF\-?8$/i
Truth table0%0/3
A && B
A= defined $ENV{'LANG'}B= $ENV{'LANG'} =~ /\.UTF\-?8$/i
| |||||||||||||||||||||||||||||
| 614 | 0 | my $console_layers = ""; | |||||||||||||||||||||||||||
| 615 | 0 | ▶ | $console_layers .= " :utf8" if $console_utf8; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | 0 | my $in; | |||||||||||||||||||||||||||
| 618 | 0 | ▶ | $in = $IN if ref($IN) eq "GLOB"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 619 | 0 | ▶ | $in = \$IN if ref(\$IN) eq "GLOB"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 620 | 0 | ▶ | open($in, "<$console_layers", $console) unless defined($in); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 621 | 0 | ▶ | $in = \*STDIN unless defined($in); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 622 | 0 | $self->{IN} = $in; | |||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | 0 | my $out; | |||||||||||||||||||||||||||
| 625 | 0 | ▶ | $out = $OUT if ref($OUT) eq "GLOB"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 626 | 0 | ▶ | $out = \$OUT if ref(\$OUT) eq "GLOB"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 627 | 0 | ▶ | open($out, ">$console_layers", $consoleOUT) unless defined($out); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 628 | 0 | ▶ | $out = \*STDOUT unless defined($out); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 629 | 0 | $self->{OUT} = $out; | |||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||
| 631 | 0 | $self->{features}->{utf8} = grep(":utf8", PerlIO::get_layers($in)); | |||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | 0 | return ($self->{IN}, $self->{OUT}); | |||||||||||||||||||||||||||
| 634 | } | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | =head2 ornaments | ||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | This is void implementation. Ornaments is B<not supported>. | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | =cut | ||||||||||||||||||||||||||||
| 641 | sub ornaments | ||||||||||||||||||||||||||||
| 642 | { | ||||||||||||||||||||||||||||
| 643 | 0 | ▶ | return; | ||||||||||||||||||||||||||
|
Subroutine
ornaments: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 644 | } | ||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | =head2 gethistory() | ||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||
| 648 | B<GetHistory()> | ||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||
| 650 | Returns copy of the history in Array. | ||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||
| 652 | =cut | ||||||||||||||||||||||||||||
| 653 | sub gethistory | ||||||||||||||||||||||||||||
| 654 | { | ||||||||||||||||||||||||||||
| 655 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
gethistory: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 656 | 0 | return @{$self->{history}}; | |||||||||||||||||||||||||||
| 657 | } | ||||||||||||||||||||||||||||
| 658 | sub GetHistory | ||||||||||||||||||||||||||||
| 659 | { | ||||||||||||||||||||||||||||
| 660 | 0 | ▶ | return gethistory(@_); | ||||||||||||||||||||||||||
|
Subroutine
GetHistory: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 661 | } | ||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||
| 663 | =head2 sethistory($line1[, $line2[, ...]]) | ||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||
| 665 | B<SetHistory($line1[, $line2[, ...]])> | ||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | rewrites all history by argument values. | ||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||
| 669 | =cut | ||||||||||||||||||||||||||||
| 670 | sub sethistory | ||||||||||||||||||||||||||||
| 671 | { | ||||||||||||||||||||||||||||
| 672 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
sethistory: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 673 | 0 | ▶ | if ($self->{features}->{utf8}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 674 | { | ||||||||||||||||||||||||||||
| 675 | 0 | ▶ | for (my $i = 0; $i < @_; $i++) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 676 | { | ||||||||||||||||||||||||||||
| 677 | 0 | utf8::decode($_[$i]); | |||||||||||||||||||||||||||
| 678 | } | ||||||||||||||||||||||||||||
| 679 | } | ||||||||||||||||||||||||||||
| 680 | 0 | @{$self->{history}} = @_; | |||||||||||||||||||||||||||
| 681 | 0 | return 1; | |||||||||||||||||||||||||||
| 682 | } | ||||||||||||||||||||||||||||
| 683 | sub SetHistory | ||||||||||||||||||||||||||||
| 684 | { | ||||||||||||||||||||||||||||
| 685 | 0 | ▶ | return sethistory(@_); | ||||||||||||||||||||||||||
|
Subroutine
SetHistory: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 686 | } | ||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||
| 688 | =head2 changehistory([$changehistory]) | ||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | If argument is specified, it allows to change history lines when argument value is true. | ||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||
| 692 | Returns the old value. | ||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||
| 694 | =cut | ||||||||||||||||||||||||||||
| 695 | sub changehistory | ||||||||||||||||||||||||||||
| 696 | { | ||||||||||||||||||||||||||||
| 697 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
changehistory: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 698 | 0 | my ($changehistory) = @_; | |||||||||||||||||||||||||||
| 699 | 0 | my $result = $self->{features}->{changehistory}; | |||||||||||||||||||||||||||
| 700 | 0 | ▶ | $self->{features}->{changehistory} = $changehistory if @_ >= 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 701 | 0 | return $result; | |||||||||||||||||||||||||||
| 702 | } | ||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||
| 704 | =head1 Other Methods and Functions | ||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||
| 706 | =cut | ||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||
| 708 | =head2 readkey([$echo]) | ||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||
| 710 | reads a key from input and echoes if I<echo> argument is C<TRUE>. | ||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||
| 712 | Returns C<undef> on C<EOF>. | ||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||
| 714 | =cut | ||||||||||||||||||||||||||||
| 715 | sub readkey | ||||||||||||||||||||||||||||
| 716 | { | ||||||||||||||||||||||||||||
| 717 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
readkey: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 718 | 0 | my ($echo) = @_; | |||||||||||||||||||||||||||
| 719 | my ($in, $out) = | ||||||||||||||||||||||||||||
| 720 | 0 | ($self->{IN}, $self->{OUT}); | |||||||||||||||||||||||||||
| 721 | 0 | ▶ | unless (-t $in) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 722 | { | ||||||||||||||||||||||||||||
| 723 | 0 | return getc($in); | |||||||||||||||||||||||||||
| 724 | } | ||||||||||||||||||||||||||||
| 725 | 0 | local $\ = undef; | |||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||
| 727 | 0 | $self->{readmode} = 'cbreak'; | |||||||||||||||||||||||||||
| 728 | 0 | Term::ReadKey::ReadMode($self->{readmode}, $self->{IN}); | |||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||
| 730 | 0 | my $result; | |||||||||||||||||||||||||||
| 731 | 0 | my ($char, $esc) = ("", undef); | |||||||||||||||||||||||||||
| 732 | 0 | ▶ | while (defined($char = getc($in))) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 733 | { | ||||||||||||||||||||||||||||
| 734 | 0 | ▶ | unless (defined($esc)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 735 | { | ||||||||||||||||||||||||||||
| 736 | 0 | given ($char) | |||||||||||||||||||||||||||
| 737 | { | ||||||||||||||||||||||||||||
| 738 | when (/\e/) | ||||||||||||||||||||||||||||
| 739 | 0 | { | |||||||||||||||||||||||||||
| 740 | 0 | $esc = ""; | |||||||||||||||||||||||||||
| 741 | } | ||||||||||||||||||||||||||||
| 742 | when (/\x04/) | ||||||||||||||||||||||||||||
| 743 | 0 | { | |||||||||||||||||||||||||||
| 744 | 0 | $result = undef; | |||||||||||||||||||||||||||
| 745 | 0 | last; | |||||||||||||||||||||||||||
| 746 | } | ||||||||||||||||||||||||||||
| 747 | default | ||||||||||||||||||||||||||||
| 748 | 0 | { | |||||||||||||||||||||||||||
| 749 | 0 | ▶ | print $out encode_controlchar($char) if $echo; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 750 | 0 | $result = $char; | |||||||||||||||||||||||||||
| 751 | 0 | last; | |||||||||||||||||||||||||||
| 752 | } | ||||||||||||||||||||||||||||
| 753 | } | ||||||||||||||||||||||||||||
| 754 | 0 | next; | |||||||||||||||||||||||||||
| 755 | } | ||||||||||||||||||||||||||||
| 756 | 0 | $esc .= $char; | |||||||||||||||||||||||||||
| 757 | 0 | ▶ | if ($esc =~ /^.\d?\D/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 758 | { | ||||||||||||||||||||||||||||
| 759 | 0 | $result = "\e$esc"; | |||||||||||||||||||||||||||
| 760 | 0 | $esc = undef; | |||||||||||||||||||||||||||
| 761 | 0 | last; | |||||||||||||||||||||||||||
| 762 | } | ||||||||||||||||||||||||||||
| 763 | } | ||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||
| 765 | 0 | Term::ReadKey::ReadMode('restore', $self->{IN}); | |||||||||||||||||||||||||||
| 766 | 0 | $self->{readmode} = ''; | |||||||||||||||||||||||||||
| 767 | 0 | return $result; | |||||||||||||||||||||||||||
| 768 | } | ||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||
| 770 | =head2 encode_controlchar($c) | ||||||||||||||||||||||||||||
| 771 | |||||||||||||||||||||||||||||
| 772 | encodes if argument C<c> is a control character, otherwise returns argument C<c>. | ||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||
| 774 | =cut | ||||||||||||||||||||||||||||
| 775 | sub encode_controlchar | ||||||||||||||||||||||||||||
| 776 | { | ||||||||||||||||||||||||||||
| 777 | 0 | ▶ | my ($c) = @_; | ||||||||||||||||||||||||||
|
Subroutine
encode_controlchar: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 778 | 0 | $c = substr($c, 0, 1); | |||||||||||||||||||||||||||
| 779 | 0 | my $s; | |||||||||||||||||||||||||||
| 780 | 0 | given ($c) | |||||||||||||||||||||||||||
| 781 | { | ||||||||||||||||||||||||||||
| 782 | when (/[\x00-\x1F]/) | ||||||||||||||||||||||||||||
| 783 | 0 | { | |||||||||||||||||||||||||||
| 784 | 0 | $s = "^".chr(0x40+ord($c)); | |||||||||||||||||||||||||||
| 785 | } | ||||||||||||||||||||||||||||
| 786 | when ($c =~ /[\x7F]/) | ||||||||||||||||||||||||||||
| 787 | 0 | { | |||||||||||||||||||||||||||
| 788 | 0 | $s = "^".chr(0x3F); | |||||||||||||||||||||||||||
| 789 | } | ||||||||||||||||||||||||||||
| 790 | default | ||||||||||||||||||||||||||||
| 791 | 0 | { | |||||||||||||||||||||||||||
| 792 | 0 | $s = $c; | |||||||||||||||||||||||||||
| 793 | } | ||||||||||||||||||||||||||||
| 794 | } | ||||||||||||||||||||||||||||
| 795 | 0 | return $s; | |||||||||||||||||||||||||||
| 796 | } | ||||||||||||||||||||||||||||
| 797 | |||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||
| 799 | 1 | 1; | |||||||||||||||||||||||||||
| 800 | __END__ | ||||||||||||||||||||||||||||