Caroline.pm

filter: Statementstmt 19.1% 79 / 413 Branchbran 6.5% 13 / 200 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 41.6% 20 / 48 Podpod 17.2% 5 / 29 totaltotal 15.2% 117 / 767 CC 119 SCAR 62.0
CC
119
Cov
14%
edit
1806.0
refresh_multi_line
182.0
search
132.0
CRAP 493.8
↑ Summary Term/ReadLine/Caroline.pm »
1 package Caroline;
2 3 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 POSIX qw(termios_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Text::VisualWidth::PP 0.03 qw(vwidth);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Unicode::EastAsianWidth::Detect qw(is_cjk_lang);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Term::ReadKey qw(GetTerminalSize ReadLine ReadKey ReadMode);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = "0.22";
13  
14 3 our @EXPORT = qw( caroline );
15  
16 3 my $HISTORY_NEXT = 0;
17 3 my $HISTORY_PREV = 1;
18  
19 3 my $IS_WIN32 = $^O eq 'MSWin32';
20 3 ▶require Win32::Console::ANSI if $IS_WIN32;
Branch: 1 branch
BranchTrueFalseTotal
if $IS_WIN32 0 3 3
21  
22 3 ▶use Class::Accessor::Lite 0.05 (
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 rw => [qw(completion_callback history_max_len)],
24 3 );
25  
26 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 CTRL_A => 1,
28 CTRL_B => 2,
29 CTRL_C => 3,
30 CTRL_D => 4,
31 CTRL_E => 5,
32 CTRL_F => 6,
33 CTRL_H => 8,
34 CTRL_I => 9,
35 CTRL_K => 11,
36 CTRL_L => 12,
37 CTRL_M => 13,
38 CTRL_N => 14,
39 CTRL_P => 16,
40 CTRL_R => 18,
41 CTRL_T => 20,
42 CTRL_U => 21,
43 CTRL_W => 23,
44 CTRL_Z => 26,
45 BACKSPACE => 127,
46 ENTER => 13,
47 TAB => 9,
48 ESC => 27,
49 3 };
50  
51 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod documented
52 4 my $class = shift;
53 0 ▶ my %args = @_==1? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 4 4
54 my $self = bless {
55 history => [],
56 debug => !!$ENV{CAROLINE_DEBUG},
57 4 multi_line => 1,
58 history_max_len => 100,
59 %args
60 }, $class;
61 4 return $self;
62 }
63  
64 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod undocumented
65 0 my ($self, $stuff) = @_;
66 0 ▶ return unless $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"debug"} 0 0 0
67  
68 # require JSON::PP;
69 0 open my $fh, '>>:utf8', 'caroline.debug.log';
70 0 print $fh $stuff;
71 # print $fh JSON::PP->new->allow_nonref(1)->encode($stuff) . "\n";
72 0 close $fh;
73 }
74  
75 4 ▶sub history { shift->{history} }
Subroutine history: called CC=1 SCAR=0.0
Pod documented
76  
77 0 ▶sub history_len {
Subroutine history_len: not called CC=1 SCAR=6.9
Pod undocumented
78 0 my $self = shift;
79 0 0+@{$self->{history}};
80 }
81  
82 4 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
83 4 my $self = shift;
84 4 $self->disable_raw_mode();
85 }
86  
87 0 ▶sub readline {
Subroutine readline: not called CC=5 SCAR=34.0
Pod undocumented
88 0 my ($self, $prompt) = @_;
89 0 ▶ $prompt = '> ' unless defined $prompt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prompt 0 0 0
90 0 STDOUT->autoflush(1);
91  
92 0 local $Text::VisualWidth::PP::EastAsian = is_cjk_lang;
93  
94 0 ▶ if ($self->is_supported && -t STDIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_supported and -t STDIN) { } 0 0 0
Condition0%0/3
$self->is_supported and -t STDIN
!ll&&!rl&&r
000
MC/DC0%0/2
$self->is_supported and -t STDIN
$self->is_supported -t STDIN
Truth table0%0/3
A and B
A= $self->is_supportedB= -t STDIN
ABresult
0X0
100
111
95 0 return $self->read_raw($prompt);
96 } else {
97 0 print STDOUT $prompt;
98 0 STDOUT->flush;
99 # I need to use ReadLine() to support Win32.
100 0 my $line = ReadLine(0);
101 0 ▶ $line =~ s/\n$// if defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 0 0 0
102 0 return $line;
103 }
104 }
105  
106 0 ▶sub get_columns {
Subroutine get_columns: not called CC=1 SCAR=6.9
Pod undocumented
107 0 my $self = shift;
108 0 my ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();
109 0 return $wchar;
110 }
111  
112 0 ▶sub readkey {
Subroutine readkey: not called CC=6 SCAR=37.4
Pod undocumented
113 0 my $self = shift;
114 0 my $c = ReadKey(0);
115 0 ▶ return undef unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 0 0
116 0 ▶ return $c unless $IS_WIN32;
Branch: 1 branch
BranchTrueFalseTotal
unless $IS_WIN32 0 0 0
117  
118 # Win32 API always return the bytes encoded with ACP. So it must be
119 # decoded from double byte sequence. To detect double byte sequence, it
120 # use Win32 API IsDBCSLeadByte.
121 0 require Win32::API;
122 0 require Encode;
123 0 require Term::Encoding;
124 0 ▶ $self->{isleadbyte} ||= Win32::API->new(
Condition0%0/3
$self->{"isleadbyte"} ||= "Win32::API"->new("kernel32", "int IsDBCSLeadByte(char a)")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"isleadbyte"} ||= "Win32::API"->new("kernel32", "int IsDBCSLeadByte(char a)")
$self->{"isleadbyte"} "Win32::API"->new("kernel32", "int IsDBCSLeadByte(char a)")
Truth table0%0/3
A ||= B
A= $self->{"isleadbyte"}B= "Win32::API"->new("kernel32", "int IsDBCSLeadByte(char a)")
ABresult
1X1
011
000
125 'kernel32', 'int IsDBCSLeadByte(char a)',
126 );
127 0 ▶ $self->{encoding} ||= Term::Encoding::get_encoding();
Condition0%0/3
$self->{"encoding"} ||= Term::Encoding::get_encoding()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"encoding"} ||= Term::Encoding::get_encoding()
$self->{"encoding"} Term::Encoding::get_encoding()
Truth table0%0/3
A ||= B
A= $self->{"encoding"}B= Term::Encoding::get_encoding()
ABresult
1X1
011
000
128 0 ▶ if ($self->{isleadbyte}->Call($c)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"isleadbyte"}->Call($c)) 0 0 0
129 0 $c .= ReadKey(0);
130 0 $c = Encode::decode($self->{encoding}, $c);
131 }
132 0 $c;
133 }
134  
135 # linenoiseRaw
136 0 ▶sub read_raw {
Subroutine read_raw: not called CC=4 SCAR=30.0
Pod undocumented
137 0 my ($self, $prompt) = @_;
138  
139 0 local $self->{sigint};
140 0 local $self->{sigtstp};
141 0 my $ret;
142 {
143 0 $self->enable_raw_mode();
144 0 $ret = $self->edit($prompt);
145 0 $self->disable_raw_mode();
146 }
147 0 print STDOUT "\n";
148 0 STDOUT->flush;
149 0 ▶ if ($self->{sigint}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'sigint'}) { } 0 0 0
elsif ($self->{'sigtstp'}) { } 0 0 0
150 0 kill 'INT', $$;
151 } elsif ($self->{sigtstp}) {
152 0 ▶ kill $IS_WIN32 ? 'INT' : 'TSTP', $$;
Branch: 1 branch
BranchTrueFalseTotal
$IS_WIN32 ? : 0 0 0
153 }
154 0 return $ret;
155 }
156  
157 0 ▶sub enable_raw_mode {
Subroutine enable_raw_mode: not called CC=2 SCAR=17.9
Pod undocumented
158 0 my $self = shift;
159  
160 0 ▶ if ($IS_WIN32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IS_WIN32) 0 0 0
161 0 ReadMode(5);
162 0 return undef;
163 }
164 0 my $termios = POSIX::Termios->new;
165 0 $termios->getattr(0);
166 0 $self->{rawmode} = [$termios->getiflag, $termios->getoflag, $termios->getcflag, $termios->getlflag, $termios->getcc(VMIN), $termios->getcc(VTIME)];
167 0 $termios->setiflag($termios->getiflag & ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON));
168 0 $termios->setoflag($termios->getoflag & ~(OPOST));
169 0 $termios->setcflag($termios->getcflag | ~(CS8));
170 0 $termios->setlflag($termios->getlflag & ~(ECHO|ICANON|IEXTEN | ISIG));
171 0 $termios->setcc(VMIN, 1);
172 0 $termios->setcc(VTIME, 0);
173 0 $termios->setattr(0, TCSAFLUSH);
174 0 return undef;
175 }
176  
177 4 ▶sub disable_raw_mode {
Subroutine disable_raw_mode: called CC=3 SCAR=17.7
Pod undocumented
178 4 my $self = shift;
179  
180 4 ▶ if ($IS_WIN32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IS_WIN32) 0 4 4
181 0 ReadMode(0);
182 0 return undef;
183 }
184 4 ▶ if (my $r = delete $self->{rawmode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = delete $self->{"rawmode"}) 0 4 4
185 0 my $termios = POSIX::Termios->new;
186 0 $termios->getattr(0);
187 0 $termios->setiflag($r->[0]);
188 0 $termios->setoflag($r->[1]);
189 0 $termios->setcflag($r->[2]);
190 0 $termios->setlflag($r->[3]);
191 0 $termios->setcc(VMIN, $r->[4]);
192 0 $termios->setcc(VTIME, $r->[5]);
193 0 $termios->setattr(0, TCSAFLUSH);
194 }
195 4 return undef;
196 }
197  
198 17 ▶sub history_add {
Subroutine history_add: called CC=2 SCAR=6.9
Pod documented
199 17 my ($self, $line) = @_;
200 17 ▶ if (@{$self->{history}}+1 > $self->history_max_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"history"};} + 1 > $self->history_max_len) 6 11 17
201 6 shift @{$self->{history}};
202 }
203 17 push @{$self->{history}}, $line;
204 }
205  
206 0 ▶sub edit {
Subroutine edit: not called CC=42 SCAR=75.0
Pod undocumented
207 0 my ($self, $prompt) = @_;
208 0 print STDOUT $prompt;
209 0 STDOUT->flush;
210  
211 0 $self->history_add('');
212  
213 0 my $state = Caroline::State->new;
214 0 $state->{prompt} = $prompt;
215 0 $state->cols($self->get_columns);
216 0 $self->debug("Columns: $state->{cols}\n");
217  
218 0 while (1) {
219 0 my $c = $self->readkey;
220 0 ▶ unless (defined $c) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $c) 0 0 0
221 0 return $state->buf;
222 }
223 0 ▶ my $cc = ord($c) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $cc = ord $c or next 0 0 0
224  
225 0 ▶ if ($cc == TAB && defined $self->{completion_callback}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc == 9 and defined $self->{"completion_callback"}) 0 0 0
Condition0%0/3
$cc == 9 and defined $self->{"completion_callback"}
!ll&&!rl&&r
000
MC/DC0%0/2
$cc == 9 and defined $self->{"completion_callback"}
$cc == 9 defined $self->{"completion_callback"}
Truth table0%0/3
A and B
A= $cc == 9B= defined $self->{"completion_callback"}
ABresult
0X0
100
111
226 0 $c = $self->complete_line($state);
227 0 ▶ return undef unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 0 0
228 0 $cc = ord($c);
229 0 ▶ next if $cc == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cc == 0 0 0 0
230 }
231  
232 0 ▶ if ($cc == ENTER) { # enter
Branch: 18 branches
BranchTrueFalseTotal
if ($cc == 13) { } 0 0 0
elsif ($cc == 3) { } 0 0 0
elsif ($cc == 26) { } 0 0 0
elsif ($cc == 127 or $cc == 8) { } 0 0 0
elsif ($cc == 4) { } 0 0 0
elsif ($cc == 20) { } 0 0 0
elsif ($cc == 2) { } 0 0 0
elsif ($cc == 6) { } 0 0 0
elsif ($cc == 16) { } 0 0 0
elsif ($cc == 14) { } 0 0 0
elsif ($cc == 27) { } 0 0 0
elsif ($cc == 21) { } 0 0 0
elsif ($cc == 11) { } 0 0 0
elsif ($cc == 1) { } 0 0 0
elsif ($cc == 5) { } 0 0 0
elsif ($cc == 12) { } 0 0 0
elsif ($cc == 18) { } 0 0 0
elsif ($cc == 23) { } 0 0 0
233 0 pop @{$self->{history}};
234 0 return $state->buf;
235 } elsif ($cc==CTRL_C) { # ctrl-c
236 0 $self->{sigint}++;
237 0 return undef;
238 } elsif ($cc==CTRL_Z) { # ctrl-z
239 0 $self->{sigtstp}++;
240 0 return $state->buf;
241 ▶ } elsif ($cc == BACKSPACE || $cc == CTRL_H) { # backspace or ctrl-h
Condition0%0/3
$cc == 127 or $cc == 8
l!l&&r!l&&!r
000
MC/DC0%0/2
$cc == 127 or $cc == 8
$cc == 127 $cc == 8
Truth table0%0/3
A or B
A= $cc == 127B= $cc == 8
ABresult
1X1
011
000
242 0 $self->edit_backspace($state);
243 } elsif ($cc == CTRL_D) { # ctrl-d
244 0 ▶ if (length($state->buf) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $state->buf > 0) { } 0 0 0
245 0 $self->edit_delete($state);
246 } else {
247 0 return undef;
248 }
249 } elsif ($cc == CTRL_T) { # ctrl-t
250 # swaps current character with prvious
251 0 ▶ if ($state->pos > 0 && $state->pos < $state->len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->pos > 0 and $state->pos < $state->len) 0 0 0
Condition0%0/3
$state->pos > 0 and $state->pos < $state->len
!ll&&!rl&&r
000
MC/DC0%0/2
$state->pos > 0 and $state->pos < $state->len
$state->pos > 0 $state->pos < $state->len
Truth table0%0/3
A and B
A= $state->pos > 0B= $state->pos < $state->len
ABresult
0X0
100
111
252 0 my $aux = substr($state->buf, $state->pos-1, 1);
253 0 substr($state->{buf}, $state->pos-1, 1) = substr($state->{buf}, $state->pos, 1);
254 0 substr($state->{buf}, $state->pos, 1) = $aux;
255 0 ▶ if ($state->pos != $state->len -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->pos != $state->len - 1) 0 0 0
256 0 $state->{pos}++;
257 }
258 }
259 0 $self->refresh_line($state);
260 } elsif ($cc == CTRL_B) { # ctrl-b
261 0 $self->edit_move_left($state);
262 } elsif ($cc == CTRL_F) { # ctrl-f
263 0 $self->edit_move_right($state);
264 } elsif ($cc == CTRL_P) { # ctrl-p
265 0 $self->edit_history_next($state, $HISTORY_PREV);
266 } elsif ($cc == CTRL_N) { # ctrl-n
267 0 $self->edit_history_next($state, $HISTORY_NEXT);
268 } elsif ($cc == 27) { # escape sequence
269 # Read the next two bytes representing the escape sequence
270 0 ▶ my $buf = $self->readkey or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $buf = $self->readkey or return undef 0 0 0
271 0 ▶ $buf .= $self->readkey or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$buf .= $self->readkey or return undef 0 0 0
272  
273 0 ▶ if ($buf eq "[D") { # left arrow
Branch: 6 branches
BranchTrueFalseTotal
if ($buf eq '[D') { } 0 0 0
elsif ($buf eq '[C') { } 0 0 0
elsif ($buf eq '[A') { } 0 0 0
elsif ($buf eq '[B') { } 0 0 0
elsif ($buf eq '[1') { } 0 0 0
elsif ($buf eq '[4') { } 0 0 0
274 0 $self->edit_move_left($state);
275 } elsif ($buf eq "[C") { # right arrow
276 0 $self->edit_move_right($state);
277 } elsif ($buf eq "[A") { # up arrow
278 0 $self->edit_history_next($state, $HISTORY_PREV);
279 } elsif ($buf eq "[B") { # down arrow
280 0 $self->edit_history_next($state, $HISTORY_NEXT);
281 } elsif ($buf eq "[1") { # home
282 0 ▶ $buf = $self->readkey or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$buf = $self->readkey or return undef 0 0 0
283 0 ▶ if ($buf eq '~') {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf eq "~") 0 0 0
284 0 $state->{pos} = 0;
285 0 $self->refresh_line($state);
286 }
287 } elsif ($buf eq "[4") { # end
288 0 ▶ $buf = $self->readkey or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$buf = $self->readkey or return undef 0 0 0
289 0 ▶ if ($buf eq '~') {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf eq "~") 0 0 0
290 0 $state->{pos} = length($state->buf);
291 0 $self->refresh_line($state);
292 }
293 }
294 # TODO:
295 # else if (seq[0] == 91 && seq[1] > 48 && seq[1] < 55) {
296 # /* extended escape, read additional two bytes. */
297 # if (read(fd,seq2,2) == -1) break;
298 # if (seq[1] == 51 && seq2[0] == 126) {
299 # /* Delete key. */
300 # linenoiseEditDelete(&l);
301 # }
302 # }
303 } elsif ($cc == CTRL_U) { # ctrl-u
304 # delete the whole line.
305 0 $state->{buf} = '';
306 0 $state->{pos} = 0;
307 0 $self->refresh_line($state);
308 } elsif ($cc == CTRL_K) { # ctrl-k
309 0 substr($state->{buf}, $state->{pos}) = '';
310 0 $self->refresh_line($state);
311 } elsif ($cc == CTRL_A) { # ctrl-a
312 0 $state->{pos} = 0;
313 0 $self->refresh_line($state);
314 } elsif ($cc == CTRL_E) { # ctrl-e
315 0 $state->{pos} = length($state->buf);
316 0 $self->refresh_line($state);
317 } elsif ($cc == CTRL_L) { # ctrl-l
318 0 $self->clear_screen();
319 0 $self->refresh_line($state);
320 } elsif ($cc == CTRL_R) { # ctrl-r
321 0 $self->search($state);
322 } elsif ($cc == CTRL_W) { # ctrl-w
323 0 $self->edit_delete_prev_word($state);
324 } else {
325 0 $self->debug("inserting $cc\n");
326 0 $self->edit_insert($state, $c);
327 }
328 }
329 0 return $state->buf;
330 }
331  
332 1 ▶sub read_history_file {
Subroutine read_history_file: called CC=3 SCAR=11.0
Pod documented
333 1 my ($self, $filename) = @_;
334 1 ▶ open my $fh, '<:encoding(utf-8)', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(utf-8)", $filename or return undef 1 0 1
335 or return undef;
336 1 ▶ while (my $hist = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $hist = CORE::readline $fh)) 3 1 4
337 3 chomp($hist);
338 3 $self->history_add($hist);
339 }
340 1 close $fh;
341 1 return 1;
342 }
343  
344 2 ▶sub write_history_file {
Subroutine write_history_file: called CC=5 SCAR=16.2
Pod documented
345 2 my ($self, $filename) = @_;
346  
347 2 ▶ return undef if $self->history_max_len == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->history_max_len == 0 1 1 2
348  
349 1 ▶ open my $fh, '>:encoding(utf-8)', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(utf-8)", $filename or return undef 1 0 1
350 or return undef;
351 1 for my $hist (@{$self->history}) {
352 3 ▶ next unless $hist =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $hist =~ /\S/ 0 3 3
353 3 print $fh $hist . "\n";
354 }
355 1 close $fh;
356 1 return 1;
357 }
358  
359 0 ▶sub edit_delete {
Subroutine edit_delete: not called CC=3 SCAR=24.8
Pod undocumented
360 0 my ($self, $status) = @_;
361 0 ▶ if ($status->len > 0 && $status->pos < $status->len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->len > 0 and $status->pos < $status->len) 0 0 0
Condition0%0/3
$status->len > 0 and $status->pos < $status->len
!ll&&!rl&&r
000
MC/DC0%0/2
$status->len > 0 and $status->pos < $status->len
$status->len > 0 $status->pos < $status->len
Truth table0%0/3
A and B
A= $status->len > 0B= $status->pos < $status->len
ABresult
0X0
100
111
362 0 substr($status->{buf}, $status->pos, 1) = '';
363 0 $self->refresh_line($status);
364 }
365 }
366  
367 0 ▶sub search {
Subroutine search: not called CC=11 SCAR=48.8
Pod undocumented
368 0 my ($self, $state) = @_;
369  
370 0 my $query = '';
371 0 local $state->{query} = '';
372 LOOP:
373 0 while (1) {
374 0 my $c = $self->readkey;
375 0 ▶ unless (defined $c) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $c) 0 0 0
376 0 return $state->buf;
377 }
378 0 ▶ my $cc = ord($c) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $cc = ord $c or next 0 0 0
379  
380 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($cc == 2 or $cc == 3 or $cc == 6 or $cc == 13) 0 0 0
Condition0%0/9
$cc == 2 or $cc == 3 or $cc == 6 or $cc == 13
l!l&&r!l&&!r
000
$cc == 2 or $cc == 3 or $cc == 6
l!l&&r!l&&!r
000
$cc == 2 or $cc == 3
l!l&&r!l&&!r
000
MC/DC0%0/4
$cc == 2 or $cc == 3 or $cc == 6 or $cc == 13
$cc == 2 $cc == 3 $cc == 6 $cc == 13
Truth table0%0/5
A or B or C or D
A= $cc == 2B= $cc == 3C= $cc == 6D= $cc == 13
ABCDresult
1XXX1
01XX1
001X1
00011
00000
381 $cc == CTRL_B
382 || $cc == CTRL_C
383 || $cc == CTRL_F
384 || $cc == ENTER
385 ) {
386 0 return;
387 }
388 0 ▶ if ($cc == BACKSPACE || $cc == CTRL_H) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc == 127 or $cc == 8) { } 0 0 0
Condition0%0/3
$cc == 127 or $cc == 8
l!l&&r!l&&!r
000
MC/DC0%0/2
$cc == 127 or $cc == 8
$cc == 127 $cc == 8
Truth table0%0/3
A or B
A= $cc == 127B= $cc == 8
ABresult
1X1
011
000
389 0 $self->debug("ctrl-h in searching\n");
390 0 $query =~ s/.\z//;
391 } else {
392 0 $query .= $c;
393 }
394 0 $self->debug("C: $cc\n");
395  
396 0 $state->query($query);
397 0 $self->debug("Searching '$query'\n");
398 SEARCH:
399 0 for my $hist (@{$self->history}) {
400 0 ▶ if ((my $idx = index($hist, $query)) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $idx = index($hist, $query)) >= 0) 0 0 0
401 0 $state->buf($hist);
402 0 $state->pos($idx);
403 0 $self->refresh_line($state);
404 0 next LOOP;
405 }
406 }
407 0 $self->beep();
408 0 $self->refresh_line($state);
409 }
410 }
411  
412 0 ▶sub complete_line {
Subroutine complete_line: not called CC=10 SCAR=47.0
Pod undocumented
413 0 my ($self, $state) = @_;
414  
415 0 my @ret = grep { defined $_ } $self->{completion_callback}->($state->buf, $state->pos);
416 0 ▶ unless (@ret) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@ret) 0 0 0
417 0 $self->beep;
418 0 return "\0";
419 }
420  
421 0 my $i = 0;
422 0 while (1) {
423 # Show completion or original buffer
424 0 ▶ if ($i < @ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @ret) { } 0 0 0
425 0 my $cloned = Storable::dclone($state);
426 0 $cloned->{buf} = $ret[$i];
427 0 $cloned->{pos} = $state->pos + length($cloned->{buf}) - length($state->buf);
428 0 $self->refresh_line($cloned);
429 } else {
430 0 $self->refresh_line($state);
431 }
432  
433 0 my $c = $self->readkey;
434 0 ▶ unless (defined $c) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $c) 0 0 0
435 0 return undef;
436 }
437 0 ▶ my $cc = ord($c) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $cc = ord $c or next 0 0 0
438  
439 0 ▶ if ($cc == TAB) { # tab
Branch: 2 branches
BranchTrueFalseTotal
if ($cc == 9) { } 0 0 0
elsif ($cc == 27) { } 0 0 0
440 0 $i = ($i+1) % (1+@ret);
441 0 ▶ if ($i==@ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == @ret) 0 0 0
442 0 $self->beep();
443 }
444 } elsif ($cc == ESC) { # escape
445 # Re-show original buffer
446 0 ▶ if ($i<@ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @ret) 0 0 0
447 0 $self->refresh_line($state);
448 }
449 0 return $c;
450 } else {
451 # Update buffer and return
452 0 ▶ if ($i<@ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @ret) 0 0 0
453 0 $state->{pos} = $state->{pos} + length($ret[$i]) - length($state->{buf});
454 0 $state->{buf} = $ret[$i];
455 }
456 0 return $c;
457 }
458 }
459 }
460  
461 0 ▶sub beep {
Subroutine beep: not called CC=1 SCAR=6.9
Pod undocumented
462 0 my $self = shift;
463 0 $self->debug("Beep!\n");
464 0 print STDERR "\x7";
465 0 STDERR->flush;
466 }
467  
468 0 ▶sub edit_delete_prev_word {
Subroutine edit_delete_prev_word: not called CC=5 SCAR=34.0
Pod undocumented
469 0 my ($self, $state) = @_;
470  
471 0 my $old_pos = $state->pos;
472 0 ▶ while ($state->pos > 0 && substr($state->buf, $state->pos-1, 1) eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->pos > 0 and substr($state->buf, $state->pos - 1, 1) eq " ") 0 0 0
Condition0%0/3
$state->pos > 0 and substr($state->buf, $state->pos - 1, 1) eq " "
!ll&&!rl&&r
000
MC/DC0%0/2
$state->pos > 0 and substr($state->buf, $state->pos - 1, 1) eq " "
$state->pos > 0 substr($state->buf, $state->pos - 1, 1) eq " "
Truth table0%0/3
A and B
A= $state->pos > 0B= substr($state->buf, $state->pos - 1, 1) eq " "
ABresult
0X0
100
111
473 0 $state->{pos}--;
474 }
475 0 ▶ while ($state->pos > 0 && substr($state->buf, $state->pos-1, 1) ne ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->pos > 0 and substr($state->buf, $state->pos - 1, 1) ne " ") 0 0 0
Condition0%0/3
$state->pos > 0 and substr($state->buf, $state->pos - 1, 1) ne " "
!ll&&!rl&&r
000
MC/DC0%0/2
$state->pos > 0 and substr($state->buf, $state->pos - 1, 1) ne " "
$state->pos > 0 substr($state->buf, $state->pos - 1, 1) ne " "
Truth table0%0/3
A and B
A= $state->pos > 0B= substr($state->buf, $state->pos - 1, 1) ne " "
ABresult
0X0
100
111
476 0 $state->{pos}--;
477 }
478 0 my $diff = $old_pos - $state->pos;
479 0 substr($state->{buf}, $state->pos, $diff) = '';
480 0 $self->refresh_line($state);
481 }
482  
483 0 ▶sub edit_history_next {
Subroutine edit_history_next: not called CC=5 SCAR=34.0
Pod undocumented
484 0 my ($self, $state, $dir) = @_;
485 0 ▶ if ($self->history_len > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->history_len > 1) 0 0 0
486 0 $self->history->[$self->history_len-1-$state->{history_index}] = $state->buf;
487 0 ▶ $state->{history_index} += ( ($dir == $HISTORY_PREV) ? 1 : -1 );
Branch: 1 branch
BranchTrueFalseTotal
$dir == $HISTORY_PREV ? : 0 0 0
488 0 ▶ if ($state->{history_index} < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state->{'history_index'} < 0) { } 0 0 0
elsif ($state->{'history_index'} >= $self->history_len) { } 0 0 0
489 0 $state->{history_index} = 0;
490 0 return;
491 } elsif ($state->{history_index} >= $self->history_len) {
492 0 $state->{history_index} = $self->history_len-1;
493 0 return;
494 }
495 0 $state->{buf} = $self->history->[$self->history_len - 1 - $state->{history_index}];
496 0 $state->{pos} = $state->len;
497 0 $self->refresh_line($state);
498 }
499 }
500  
501 0 ▶sub edit_backspace {
Subroutine edit_backspace: not called CC=3 SCAR=24.8
Pod undocumented
502 0 my ($self, $state) = @_;
503 0 ▶ if ($state->pos > 0 && length($state->buf) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->pos > 0 and length $state->buf > 0) 0 0 0
Condition0%0/3
$state->pos > 0 and length $state->buf > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$state->pos > 0 and length $state->buf > 0
$state->pos > 0 length $state->buf > 0
Truth table0%0/3
A and B
A= $state->pos > 0B= length $state->buf > 0
ABresult
0X0
100
111
504 0 substr($state->{buf}, $state->pos-1, 1) = '';
505 0 $state->{pos}--;
506 0 $self->refresh_line($state);
507 }
508 }
509  
510 0 ▶sub clear_screen {
Subroutine clear_screen: not called CC=1 SCAR=6.9
Pod undocumented
511 0 my ($self) = @_;
512 0 print STDOUT "\x1b[H\x1b[2J";
513 }
514  
515 0 ▶sub refresh_line {
Subroutine refresh_line: not called CC=2 SCAR=17.9
Pod undocumented
516 0 my ($self, $state) = @_;
517 0 ▶ if ($self->{multi_line}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'multi_line'}) { } 0 0 0
518 0 $self->refresh_multi_line($state);
519 } else {
520 0 $self->refresh_single_line($state);
521 }
522 }
523  
524 0 ▶sub refresh_multi_line {
Subroutine refresh_multi_line: not called CC=13 SCAR=52.0
Pod undocumented
525 0 my ($self, $state) = @_;
526  
527 0 my $plen = vwidth($state->prompt);
528 0 $self->debug($state->buf. "\n");
529  
530 # rows used by current buf
531 0 my $rows = int(($plen + vwidth($state->buf) + $state->cols -1) / $state->cols);
532 0 ▶ if (defined $state->query) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state->query) 0 0 0
533 0 $rows++;
534 }
535  
536 # cursor relative row
537 0 my $rpos = int(($plen + $state->oldpos + $state->cols) / $state->cols);
538  
539 0 my $old_rows = $state->maxrows;
540  
541 # update maxrows if needed.
542 0 ▶ if ($rows > $state->maxrows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows > $state->maxrows) 0 0 0
543 0 $state->maxrows($rows);
544 }
545  
546 0 $self->debug(sprintf "[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d",
547 $state->len, $state->pos, $state->oldpos, $plen, $rows, $rpos, $state->maxrows, $old_rows);
548  
549 # First step: clear all the lines used before. To do start by going to the last row.
550 0 ▶ if ($old_rows - $rpos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_rows - $rpos > 0) 0 0 0
551 0 $self->debug(sprintf ", go down %d", $old_rows-$rpos);
552 0 printf STDOUT "\x1b[%dB", $old_rows-$rpos;
553 }
554  
555 # Now for every row clear it, go up.
556 0 my $j;
557 0 ▶ for ($j=0; $j < ($old_rows-1); ++$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $old_rows - 1) 0 0 0
558 0 $self->debug(sprintf ", clear+up %d %d", $old_rows-1, $j);
559 0 print("\x1b[0G\x1b[0K\x1b[1A");
560 }
561  
562 # Clean the top line
563 0 $self->debug(", clear");
564 0 print("\x1b[0G\x1b[0K");
565  
566 # Write the prompt and the current buffer content
567 0 print $state->prompt;
568 0 print $state->buf;
569 0 ▶ if (defined $state->query) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state->query) 0 0 0
570 0 print "\015\nSearch: " . $state->query;
571 }
572  
573 # If we are at the very end of the screen with our prompt, we need to
574 # emit a newline and move the prompt to the first column
575 0 ▶ if ($state->pos && $state->pos == $state->len && ($state->pos + $plen) % $state->cols == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->pos and $state->pos == $state->len and ($state->pos + $plen) % $state->cols == 0) 0 0 0
Condition0%0/6
$state->pos and $state->pos == $state->len and ($state->pos + $plen) % $state->cols == 0
!ll&&!rl&&r
000
$state->pos and $state->pos == $state->len
!ll&&!rl&&r
000
MC/DC0%0/3
$state->pos and $state->pos == $state->len and ($state->pos + $plen) % $state->cols == 0
$state->pos $state->pos == $state->len ($state->pos + $plen) % $state->cols == 0
Truth table0%0/4
A and B and C
A= $state->posB= $state->pos == $state->lenC= ($state->pos + $plen) % $state->cols == 0
ABCresult
0XX0
10X0
1100
1111
576 0 $self->debug("<newline>");
577 0 print "\n";
578 0 print "\x1b[0G";
579 0 $rows++;
580 0 ▶ if ($rows > $state->maxrows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows > $state->maxrows) 0 0 0
581 0 $state->maxrows(int $rows);
582 }
583 }
584  
585 # Move cursor to right position
586 0 my $rpos2 = int(($plen + $state->vpos + $state->cols) / $state->cols); # current cursor relative row
587 0 $self->debug(sprintf ", rpos2 %d", $rpos2);
588 # Go up till we reach the expected position
589 0 ▶ if ($rows - $rpos2 > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows - $rpos2 > 0) 0 0 0
590 # cursor up
591 0 printf "\x1b[%dA", $rows-$rpos2;
592 }
593  
594 # Set column
595 0 my $col;
596 {
597 0 $col = 1;
598 0 my $buf = $state->prompt . substr($state->buf, 0, $state->pos);
599 0 for (split //, $buf) {
600 0 $col += vwidth($_);
601 0 ▶ if ($col > $state->cols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col > $state->cols) 0 0 0
602 0 $col -= $state->cols;
603 }
604 }
605 }
606 0 $self->debug(sprintf ", set col %d", $col);
607 0 printf "\x1b[%dG", $col;
608  
609 0 $state->oldpos($state->pos);
610  
611 0 $self->debug("\n");
612 }
613  
614 0 ▶sub refresh_single_line {
Subroutine refresh_single_line: not called CC=3 SCAR=24.8
Pod undocumented
615 0 my ($self, $state) = @_;
616  
617 0 my $buf = $state->buf;
618 0 my $len = $state->len;
619 0 my $pos = $state->pos;
620 0 ▶ while ((vwidth($state->prompt)+$pos) >= $state->cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (vwidth($state->prompt) + $pos >= $state->cols) 0 0 0
621 0 substr($buf, 0, 1) = '';
622 0 $len--;
623 0 $pos--;
624 }
625 0 ▶ while (vwidth($state->prompt) + vwidth($buf) > $state->cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (vwidth($state->prompt) + vwidth($buf) > $state->cols) 0 0 0
626 0 $len--;
627 }
628  
629 0 print STDOUT "\x1b[0G"; # cursor to left edge
630 0 print STDOUT $state->{prompt};
631 0 print STDOUT $buf;
632 0 print STDOUT "\x1b[0K"; # erase to right
633  
634 # Move cursor to original position
635 printf "\x1b[0G\x1b[%dC", (
636 length($state->{prompt})
637 0 + vwidth(substr($buf, 0, $pos))
638 );
639 }
640  
641 0 ▶sub edit_move_right {
Subroutine edit_move_right: not called CC=2 SCAR=17.9
Pod undocumented
642 0 my ($self, $state) = @_;
643 0 ▶ if ($state->pos != length($state->buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->pos != length $state->buf) 0 0 0
644 0 $state->{pos}++;
645 0 $self->refresh_line($state);
646 }
647 }
648  
649 0 ▶sub edit_move_left {
Subroutine edit_move_left: not called CC=2 SCAR=17.9
Pod undocumented
650 0 my ($self, $state) = @_;
651 0 ▶ if ($state->pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->pos > 0) 0 0 0
652 0 $state->{pos}--;
653 0 $self->refresh_line($state);
654 }
655 }
656  
657  
658 0 ▶sub edit_insert {
Subroutine edit_insert: not called CC=2 SCAR=17.9
Pod undocumented
659 0 my ($self, $state, $c) = @_;
660 0 ▶ if (length($state->buf) == $state->pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $state->buf == $state->pos) { } 0 0 0
661 0 $state->{buf} .= $c;
662 } else {
663 0 substr($state->{buf}, $state->{pos}, 0) = $c;
664 }
665 0 $state->{pos}++;
666 0 $self->refresh_line($state);
667 }
668  
669 0 ▶sub is_supported {
Subroutine is_supported: not called CC=5 SCAR=34.0
Pod undocumented
670 0 my ($self) = @_;
671 0 ▶ return 1 if $IS_WIN32;
Branch: 1 branch
BranchTrueFalseTotal
if $IS_WIN32 0 0 0
672 0 my $term = $ENV{'TERM'};
673 0 ▶ return 0 unless defined $term;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $term 0 0 0
674 0 ▶ return 0 if $term eq 'dumb';
Branch: 1 branch
BranchTrueFalseTotal
if $term eq "dumb" 0 0 0
675 0 ▶ return 0 if $term eq 'cons25';
Branch: 1 branch
BranchTrueFalseTotal
if $term eq "cons25" 0 0 0
676 0 return 1;
677 }
678  
679 package Caroline::State;
680  
681 3 ▶use Class::Accessor::Lite 0.05 (
Subroutine BEGIN: called CC=1 SCAR=0.0
682 3 rw => [qw(buf pos cols prompt oldpos maxrows query)],
683 3 );
684  
685 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
686 0 my $class = shift;
687 0 bless {
688 buf => '',
689 pos => 0,
690 history_index => 0,
691 oldpos => 0,
692 maxrows => 0,
693 }, $class;
694 }
695 3 ▶use Text::VisualWidth::PP 0.03 qw(vwidth);
Subroutine BEGIN: called CC=1 SCAR=0.0
696  
697 0 ▶sub len { length(shift->buf) }
Subroutine len: not called CC=1 SCAR=6.9
698 0 ▶sub plen { length(shift->prompt) }
Subroutine plen: not called CC=1 SCAR=6.9
699  
700 0 ▶sub vpos {
Subroutine vpos: not called CC=1 SCAR=6.9
701 0 my $self = shift;
702 0 vwidth(substr($self->buf, 0, $self->pos));
703 }
704  
705 0 ▶sub width {
Subroutine width: not called CC=1 SCAR=6.9
706 0 my $self = shift;
707 0 vwidth($self->prompt . $self->buf);
708 }
709  
710 3 1;
711 __END__
↑ Summary Term/ReadLine/Caroline.pm »