Console/ANSI.pm

filter: Statementstmt 22.0% 70 / 317 Branchbran 7.8% 12 / 152 Conditioncond 6.6% 2 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 43.7% 14 / 32 Podpod 59.0% 13 / 22 totaltotal 19.4% 111 / 571 CC 87 SCAR 42.9
CC
87
Cov
17%
nbgetch
272.0
invalidate
132.0
refresh
76.5
CRAP 73.0
« World.pm ↑ Summary Console.pm »
1 package Games::Roguelike::Console::ANSI;
2
3 =head1 NAME
4
5 Games::Roguelike::Console::ANSI - socket-friendly, object oriented curses-like support for an ansi screen buffer
6
7 =head1 SYNOPSIS
8
9 use Games::Roguelike::Console::ANSI;
10
11 $con = Games::Roguelike::Console::ANSI->new();
12 $con->attron('bold yellow');
13 $con->addstr('test');
14 $con->attroff();
15 $con->refresh();
16
17 =head1 DESCRIPTION
18
19 Allows a curses-like ansi screen buffer that works on win32, and doesn't crash when used with
20 sockets like the perl ncurses does.
21
22 Inherits from Games::Roguelike::Console. See Games::Roguelike::Console for list of methods.
23
24 Uses Term::ANSIColor for colors.
25
26 =head1 SEE ALSO
27
28 L<Games::Roguelike::Console>
29
30 =head1 AUTHOR
31
32 Erik Aronesty C<earonesty@cpan.org>
33
34 =head1 LICENSE
35
36 This program is free software; you can redistribute it and/or
37 modify it under the same terms as Perl itself.
38
39 See L<http://www.perl.com/perl/misc/Artistic.html> or the included LICENSE file.
40
41 =cut
42
43 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 5 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 5 ▶use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 5 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 5 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 5 ▶use Carp qw(confess croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
49
50 5 ▶use base 'Games::Roguelike::Console';
Subroutine BEGIN: called CC=1 SCAR=0.0
51
52 5 our $VERSION = '0.4.' . [qw$Revision: 258 $]->[1];
53
54 5 our $KEY_ESCAPE = chr(27);
55 5 our $KEY_NOOP = chr(241);
56 5 our ($KEY_LEFT, $KEY_UP, $KEY_RIGHT, $KEY_DOWN) = ('[D','[A','[C','[B');
57
58 5 my %TELKEY = (
59 "\xfb" => 'WILL',
60 "\xfc" => 'WONT',
61 "\xfd" => 'DO',
62 "\xfe" => 'DONT',
63 );
64
65 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
66 2 my $pkg = shift;
67 2 ▶ croak "usage: Games::Roguelike::Console::ANSI->new()" unless $pkg;
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg 0 2 2
68
69 2 my $self = bless {}, $pkg;
70 2 $self->init(@_);
71 2 return $self;
72 }
73
74 5 my $STD;
75 2 ▶sub init {
Subroutine init: called CC=8 SCAR=32.0
Pod undocumented
76 2 my $self = shift;
77
78 2 my %opt = @_;
79
80 2 ▶ $self->{in} = *STDIN{IO} unless $self->{in} = $opt{in};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"in"} = $opt{"in"} 1 1 2
81 2 ▶ $self->{out} = *STDOUT{IO} unless $self->{out} = $opt{out};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"out"} = $opt{"out"} 1 1 2
82 2 $self->{cursor} = 1;
83 2 $self->{cx} = 0;
84 2 $self->{cy} = 0;
85 2 $self->{cattr} = '';
86 2 $self->{cbuf} = '';
87 2 $self->{reset} = color('reset');
88
89 2 $self->SUPER::init(%opt);
90
91 # initialize ansi terminal
92 2 ▶ if (!$opt{noinit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opt{"noinit"}) 0 2 2
93
94 0 my $out = $self->{out};
95
96 0 $self->{usereadkey} = (($self->{out}->fileno() == 1));
97
98 # i think get away from readkey
99 # and just send the ansi sequence
100 # for determining terminal size
101
102 0 ▶ if ($self->{usereadkey}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"usereadkey"}) 0 0 0
103 # this will (wrongly) close the output handle if it fails
104 0 eval {
105 0 ($self->{winx}, $self->{winy}) = GetTerminalSize($self->{out});
106 0 ReadMode 'cbreak', $self->{in};
107 };
108 }
109
110 0 ▶ if (!$self->{winx}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"winx"}) 0 0 0
111 # todo: negotiate using telnet stuff
112 0 ($self->{winx}, $self->{winy}) = (80,40);
113 }
114
115 0 $self->{invl}=$self->{winx}+1;
116 0 $self->{invr}=-1;
117 0 $self->{invt}=$self->{winy}+1;
118 0 $self->{invb}=-1;
119
120
121 0 print $out ("\033[2J"); #clear the screen
122 0 print $out ("\033[0;0H"); #jump to 0,0
123 0 print $out ("\033[=0c"); #hide cursor
124
125 0 $self->{cursor} = 0;
126 0 ▶ if ($self->{out}->fileno() == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"out"}->fileno == 1) 0 0 0
127 # there's probably a tty
128 0 $STD = $self;
129 0 $SIG{INT} = \&sig_int_handler;
130 0 $SIG{__DIE__} = \&sig_die_handler;
131 0 ▶ $self->{speed} = `stty speed` unless $self->{speed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"speed"} 0 0 0
132 }
133 }
134 }
135
136 0 ▶sub clear {
Subroutine clear: not called CC=1 SCAR=6.9
Pod undocumented
137 0 my $self = shift;
138 0 my $out = $self->{out};
139 0 @{$self->{buf}} = [];
140 0 @{$self->{cur}} = [];
141 0 print $out ("\033[2J"); #clear the screen
142 0 print $out ("\033[0;0H"); #jump to 0,0
143 }
144
145 0 ▶sub redraw {
Subroutine redraw: not called CC=2 SCAR=17.9
Pod documented
146 0 my $self = shift;
147 0 my $out = $self->{out};
148 0 @{$self->{cur}} = [];
149 0 print $out "\033c"; # reset
150 0 ▶ print $out ("\033[=0c") if !$self->{cursor}; # hide cursor
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cursor"} 0 0 0
151 0 $self->clear();
152 0 refresh();
153 }
154
155 1 ▶sub reset_fh {
Subroutine reset_fh: called CC=1 SCAR=0.0
Pod undocumented
156 1 my $out = shift;
157 #print $out "\033[c"; # reset
158 1 print $out "\033[=1c"; # show cursor
159 1 print $out "\033[30;0H"; # jump to col 0
160 1 eval {ReadMode 0, $out}; # normal input
161 }
162
163 0 ▶sub sig_int_handler {
Subroutine sig_int_handler: not called CC=1 SCAR=6.9
Pod undocumented
164 0 reset_fh(*STDOUT{IO});
165 0 exit;
166 }
167
168 0 ▶sub sig_die_handler {
Subroutine sig_die_handler: not called CC=2 SCAR=17.9
Pod undocumented
169 0 ▶ die @_ if $^S;
Branch: 1 branch
BranchTrueFalseTotal
if $^S 0 0 0
170 0 reset_fh(*STDOUT{IO});
171 0 die @_;
172 }
173
174 5 ▶sub END {
Subroutine END: called CC=4 SCAR=17.9
175 # this is only done because DESTROY is never called for some reason on Win32
176 5 ▶ if ($STD) {
Branch: 1 branch
BranchTrueFalseTotal
if ($STD) 0 5 5
177 0 reset_fh(*STDOUT{IO});
178 0 $STD = undef;
179 }
180
181 5 ▶ if ($^O =~ /linux|darwin/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /linux|darwin/) 5 0 5
182 5 ▶ if (my $tty = POSIX::ttyname(1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tty = POSIX::ttyname(1)) 0 5 5
183 0 system("stty -F $tty sane");
184 }
185 }
186 }
187
188 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=5 SCAR=18.1
189 1 my $self = shift;
190 1 ▶ if ($self->{out} && fileno($self->{out})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"out"} and fileno $self->{"out"}) 1 0 1
Condition33%1/3
$self->{"out"} and fileno $self->{"out"}
!ll&&!rl&&r
001
MC/DC0%0/2
$self->{"out"} and fileno $self->{"out"}
$self->{"out"} fileno $self->{"out"}
Truth table33%1/3
A and B
A= $self->{"out"}B= fileno $self->{"out"}
ABresult
0X0
100
111
191 1 reset_fh($self->{out});
192 1 ▶ if ($self->{out}->fileno()
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"out"}->fileno and $self->{"out"}->fileno == 1) 1 0 1
Condition33%1/3
$self->{"out"}->fileno and $self->{"out"}->fileno == 1
!ll&&!rl&&r
001
MC/DC0%0/2
$self->{"out"}->fileno and $self->{"out"}->fileno == 1
$self->{"out"}->fileno $self->{"out"}->fileno == 1
Truth table33%1/3
A and B
A= $self->{"out"}->filenoB= $self->{"out"}->fileno == 1
ABresult
0X0
100
111
193 && $self->{out}->fileno() == 1) {
194 1 $STD = undef;
195 1 $SIG{INT} = undef;
196 1 $SIG{__DIE__} = undef;
197 }
198 }
199 }
200
201 0 ▶sub tagstr {
Subroutine tagstr: not called CC=8 SCAR=42.8
Pod documented
202 0 my $self = shift;
203 0 my ($y, $x, $str);
204 0 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
205 0 ($y, $x, $str) = ($self->{cy}, $self->{cx}, @_);
206 } else {
207 0 ($y, $x, $str) = @_;
208 }
209 0 my $attr;
210 0 my $r = $x;
211 0 my $c;
212 0 ▶ for (my $i = 0; $i < length($str); ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $str) 0 0 0
213 0 $c = substr($str,$i,1);
214 0 ▶ if ($c eq '<') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "<") 0 0 0
215 0 substr($str,$i) =~ s/<([^>]*)>//;
216 0 $attr = $1;
217 0 $attr =~ s/(bold )?gray/bold black/i;
218 0 $attr =~ s/,/ /;
219 0 $attr =~ s/\bon /on_/;
220 0 ▶ if ($attr eq 'gt') {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr eq 'gt') { } 0 0 0
elsif ($attr eq 'lt') { } 0 0 0
221 0 $c = '>';
222 0 --$i;
223 } elsif ($attr eq 'lt') {
224 0 $c = '<';
225 0 --$i;
226 } else {
227 0 $c = substr($str,$i,1);
228 }
229 }
230 0 ▶ if ($c eq "\r") {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\r") 0 0 0
231 0 next;
232 }
233 0 ▶ if ($c eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\n") 0 0 0
234 0 $r = 0;
235 0 $y++;
236 0 next;
237 }
238 0 $self->{buf}->[$y][$r]->[0] = $self->parsecolor($attr);
239 0 $self->{buf}->[$y][$r]->[1] = $c;
240 0 ++$r;
241
242 }
243 0 $self->invalidate($x, $y, $x+$r, $y);
244 0 $self->{cy}=$y;
245 0 $self->{cx}=$x+$r;
246 }
247
248 2 ▶sub parsecolor {
Subroutine parsecolor: called CC=2 SCAR=7.1
Pod documented
249 2 my $self = shift;
250 2 my $color = shift;
251 2 ▶ if ($color) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color) { } 2 0 2
252 2 $color =~ s/(bold )?gray/bold black/i;
253 2 $color =~ s/,/ /;
254 2 $color =~ s/\bon\s+/on_/;
255 2 return color($color);
256 } else {
257 0 return '';
258 }
259 }
260
261 0 ▶sub attron {
Subroutine attron: not called CC=1 SCAR=6.9
Pod documented
262 0 my $self = shift;
263 0 $self->{cattr} = $self->parsecolor(@_);
264 }
265
266 0 ▶sub attroff {
Subroutine attroff: not called CC=1 SCAR=6.9
Pod documented
267 0 my $self = shift;
268 0 my ($attr) = @_;
269 0 $self->{cattr} = '';
270 }
271
272 0 ▶sub addstr {
Subroutine addstr: not called CC=7 SCAR=40.3
Pod documented
273 0 my $self = shift;
274 0 my $str = pop @_;
275
276 0 ▶ if (@_== 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 0) { } 0 0 0
elsif (@_ == 2) { } 0 0 0
277 0 ▶ for (my $i = 0; $i < length($str); ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $str) 0 0 0
278 0 my $c = substr($str,$i,1);
279 0 ▶ if ($c eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\n") 0 0 0
280 0 $self->{cx} = 0;
281 0 $self->{cy}++;
282 0 next;
283 }
284 0 $self->{buf}->[$self->{cy}][$self->{cx}+$i]->[0] = $self->{cattr};
285 0 $self->{buf}->[$self->{cy}][$self->{cx}+$i]->[1] = $c;
286 }
287 0 $self->invalidate($self->{cx}, $self->{cy}, $self->{cx} + length($str), $self->{cy});
288 0 $self->{cx} += length($str);
289 } elsif (@_==2) {
290 0 my ($y, $x) = @_;
291 0 ▶ for (my $i = 0; $i < length($str); ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $str) 0 0 0
292 0 my $c = substr($str,$i,1);
293 0 ▶ if ($c eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\n") 0 0 0
294 0 $c = 0;
295 0 $y++;
296 0 next;
297 }
298 0 $self->{buf}->[$y][$x+$i]->[0] = $self->{cattr};
299 0 $self->{buf}->[$y][$x+$i]->[1] = $c;
300 }
301 0 $self->invalidate($x, $y, $x+length($str), $y);
302 0 $self->{cy}=$y;
303 0 $self->{cx}=$x+length($str);
304 }
305 }
306
307 0 ▶sub invalidate {
Subroutine invalidate: not called CC=11 SCAR=48.8
Pod undocumented
308 0 my $self = shift;
309 0 my ($l, $t, $r, $b) = @_;
310 0 ▶ $r = 0 if ($r < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $r < 0 0 0 0
311 0 ▶ $t = 0 if ($t < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $t < 0 0 0 0
312 0 ▶ $b = $self->{winy} if ($b > $self->{winy});
Branch: 1 branch
BranchTrueFalseTotal
if $b > $self->{"winy"} 0 0 0
313 0 ▶ $r = $self->{winx} if ($r > $self->{winx});
Branch: 1 branch
BranchTrueFalseTotal
if $r > $self->{"winx"} 0 0 0
314
315 0 ▶ if ($r < $l) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r < $l) 0 0 0
316 0 my $m = $r;
317 0 $r = $l;
318 0 $l = $m;
319 }
320 0 ▶ if ($b < $t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b < $t) 0 0 0
321 0 my $m = $t;
322 0 $b = $t;
323 0 $t = $m;
324 }
325 0 ▶ $self->{invl} = $l if $l < $self->{invl};
Branch: 1 branch
BranchTrueFalseTotal
if $l < $self->{"invl"} 0 0 0
326 0 ▶ $self->{invr} = $r if $r > $self->{invr};
Branch: 1 branch
BranchTrueFalseTotal
if $r > $self->{"invr"} 0 0 0
327 0 ▶ $self->{invt} = $t if $t < $self->{invt};
Branch: 1 branch
BranchTrueFalseTotal
if $t < $self->{"invt"} 0 0 0
328 0 ▶ $self->{invb} = $b if $b > $self->{invb};
Branch: 1 branch
BranchTrueFalseTotal
if $b > $self->{"invb"} 0 0 0
329 }
330
331 0 ▶sub refresh {
Subroutine refresh: not called CC=9 SCAR=43.4
Pod documented
332 0 my $self = shift;
333 0 my $out = $self->{out};
334
335 # it's expected that the "buf" array will frequently be uninitialized
336 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
337
338 0 my $cc;
339 0 ▶ for (my $y = $self->{invt}; $y <= $self->{invb}; ++$y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y <= $self->{"invb"}) 0 0 0
340 0 ▶ for (my $x = $self->{invl}; $x <= $self->{invr}; ++$x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $self->{"invr"}) 0 0 0
341 0 ▶ if (!($self->{buf}->[$y][$x]->[0] eq $self->{cur}->[$y][$x]->[0]) || !($self->{buf}->[$y][$x]->[1] eq $self->{cur}->[$y][$x]->[1])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"buf"}[$y][$x][0] eq $self->{"cur"}[$y][$x][0] and $self->{"buf"}[$y][$x][1] eq $self->{"cur"}[$y][$x][1]) 0 0 0
Condition0%0/3
$self->{"buf"}[$y][$x][0] eq $self->{"cur"}[$y][$x][0] and $self->{"buf"}[$y][$x][1] eq $self->{"cur"}[$y][$x][1]
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"buf"}[$y][$x][0] eq $self->{"cur"}[$y][$x][0] and $self->{"buf"}[$y][$x][1] eq $self->{"cur"}[$y][$x][1]
$self->{"buf"}[$y][$x][0] eq $self->{"cur"}[$y][$x][0] $self->{"buf"}[$y][$x][1] eq $self->{"cur"}[$y][$x][1]
Truth table0%0/3
A and B
A= $self->{"buf"}[$y][$x][0] eq $self->{"cur"}[$y][$x][0]B= $self->{"buf"}[$y][$x][1] eq $self->{"cur"}[$y][$x][1]
ABresult
0X0
100
111
342 0 print $out "\033[", ($y+1), ";", ($x+1), "H", @{$self->{buf}->[$y][$x]};
343 0 $cc += 9;
344 0 $self->{cur}->[$y][$x]->[0]=$self->{buf}->[$y][$x]->[0];
345 0 $self->{cur}->[$y][$x]->[1]=$self->{buf}->[$y][$x]->[1];
346 0 my $pattr = $self->{cur}->[$y][$x]->[0];
347 # reduce unnecessary cursor moves & color sets
348 0 ▶ while ($x < $self->{invr} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"invr"} and not $self->{"buf"}[$y][$x + 1][0] eq $self->{"cur"}[$y][$x + 1][0] && $self->{"buf"}[$y][$x + 1][1] eq $self->{"cur"}[$y][$x + 1][1]) 0 0 0
Condition0%0/6
$x < $self->{"invr"} and not $self->{"buf"}[$y][$x + 1][0] eq $self->{"cur"}[$y][$x + 1][0] && $self->{"buf"}[$y][$x + 1][1] eq $self->{"cur"}[$y][$x + 1][1]
!ll&&!rl&&r
000
$self->{'buf'}[$y][$x + 1][0] eq $self->{'cur'}[$y][$x + 1][0] && $self->{'buf'}[$y][$x + 1][1] eq $self->{'cur'}[$y][$x + 1][1]
!ll&&!rl&&r
000
MC/DC0%0/3
$x < $self->{"invr"} and not $self->{"buf"}[$y][$x + 1][0] eq $self->{"cur"}[$y][$x + 1][0] && $self->{"buf"}[$y][$x + 1][1] eq $self->{"cur"}[$y][$x + 1][1]
$x < $self->{"invr"} $self->{'buf'}[$y][$x + 1][0] eq $self->{'cur'}[$y][$x + 1][0] $self->{'buf'}[$y][$x + 1][1] eq $self->{'cur'}[$y][$x + 1][1]
Truth table0%0/4
A and not(B && C)
A= $x < $self->{"invr"}B= $self->{'buf'}[$y][$x + 1][0] eq $self->{'cur'}[$y][$x + 1][0]C= $self->{'buf'}[$y][$x + 1][1] eq $self->{'cur'}[$y][$x + 1][1]
ABCresult
0XX0
1110
10X1
1101
349 !( ($self->{buf}->[$y][$x+1]->[0] eq $self->{cur}->[$y][$x+1]->[0])
350 && ($self->{buf}->[$y][$x+1]->[1] eq $self->{cur}->[$y][$x+1]->[1])
351 )
352 ) {
353 0 ++$x;
354 0 ▶ if (!($pattr eq $self->{buf}->[$y][$x]->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pattr eq $self->{"buf"}[$y][$x][0]) 0 0 0
355 0 print $out $self->{reset};
356 0 print $out $self->{buf}->[$y][$x]->[0];
357 0 $pattr = $self->{buf}->[$y][$x]->[0];
358 0 $cc += 7;
359 }
360 0 print $out $self->{buf}->[$y][$x]->[1];
361 0 $self->{cur}->[$y][$x]->[0]=$self->{buf}->[$y][$x]->[0];
362 0 $self->{cur}->[$y][$x]->[1]=$self->{buf}->[$y][$x]->[1];
363 0 $cc += 1;
364 }
365 0 print $out $self->{reset};
366 0 $cc += 4;
367 }
368 }
369 }
370 0 $self->{invl}=$self->{winx}+1;
371 0 $self->{invr}=-1;
372 0 $self->{invt}=$self->{winy}+1;
373 0 $self->{invb}=-1;
374 }
375
376 0 ▶sub move {
Subroutine move: not called CC=4 SCAR=30.0
Pod documented
377 0 my $self = shift;
378 0 my $out = $self->{out};
379 0 my ($y, $x) = @_;
380 0 $self->{cy}=$y;
381 0 $self->{cx}=$x;
382 0 ▶ if ($self->{cursor} && !($self->{cx}==$self->{scx} && $self->{cx}==$self->{scy})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cursor"} and not $self->{"cx"} == $self->{"scx"} && $self->{"cx"} == $self->{"scy"}) 0 0 0
Condition0%0/6
$self->{"cursor"} and not $self->{"cx"} == $self->{"scx"} && $self->{"cx"} == $self->{"scy"}
!ll&&!rl&&r
000
$self->{'cx'} == $self->{'scx'} && $self->{'cx'} == $self->{'scy'}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"cursor"} and not $self->{"cx"} == $self->{"scx"} && $self->{"cx"} == $self->{"scy"}
$self->{"cursor"} $self->{'cx'} == $self->{'scx'} $self->{'cx'} == $self->{'scy'}
Truth table0%0/4
A and not(B && C)
A= $self->{"cursor"}B= $self->{'cx'} == $self->{'scx'}C= $self->{'cx'} == $self->{'scy'}
ABCresult
0XX0
1110
10X1
1101
383 0 print $out "\033[", ($y+1), ";", ($x+1), "H";
384 0 $self->{scx} = $self->{cx};
385 0 $self->{scy} = $self->{cy};
386 }
387 }
388
389 0 ▶sub cursor {
Subroutine cursor: not called CC=5 SCAR=34.0
Pod documented
390 0 my $self = shift;
391 0 my ($set) = @_;
392 0 my $out = $self->{out};
393
394 0 ▶ if ($set && !$self->{cursor}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($set and not $self->{'cursor'}) { } 0 0 0
elsif (not $set and $self->{'cursor'}) { } 0 0 0
Condition0%0/3
$set and not $self->{"cursor"}
!ll&&!rl&&r
000
MC/DC0%0/2
$set and not $self->{"cursor"}
$set not $self->{"cursor"}
Truth table0%0/3
A and not(B)
A= $setB= not $self->{"cursor"}
ABresult
0X0
100
111
395 0 print $out ("\033[=1c"); #show cursor
396 0 $self->{cursor} = 1;
397 0 $self->move($self->{cy}, $self->{cx});
398 ▶ } elsif (!$set && $self->{cursor}) {
Condition0%0/3
not $set and $self->{"cursor"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $set and $self->{"cursor"}
not $set $self->{"cursor"}
Truth table0%0/3
not(A) and B
A= not $setB= $self->{"cursor"}
ABresult
0X0
100
111
399 0 print $out ("\033[=0c"); #hide cursor
400 0 $self->{cursor} = 0;
401 }
402 }
403
404 0 ▶sub addch {
Subroutine addch: not called CC=1 SCAR=6.9
Pod documented
405 0 my $self = shift;
406 0 $self->addstr(@_);
407 }
408
409 0 ▶sub getch_raw {
Subroutine getch_raw: not called CC=4 SCAR=30.0
Pod undocumented
410 0 my $self = shift;
411 0 my $time = shift;
412 0 ▶ if ($self->{usereadkey}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'usereadkey'}) { } 0 0 0
413 0 ▶ return ReadKey($time ? $time : 0, $self->{in});
Branch: 1 branch
BranchTrueFalseTotal
$time ? : 0 0 0
414 } else {
415 0 my $c;
416 0 ▶ $c = undef if !sysread($self->{in}, $c, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless sysread $self->{"in"}, $c, 1 0 0 0
417 0 return $c;
418 }
419 }
420
421 0 ▶sub trans {
Subroutine trans: not called CC=6 SCAR=37.4
Pod undocumented
422 0 my ($c) = @_;
423
424 0 ▶ if ($c eq $KEY_UP) {
Branch: 5 branches
BranchTrueFalseTotal
if ($c eq $Games::Roguelike::Console::ANSI::KEY_UP) { } 0 0 0
elsif ($c eq $Games::Roguelike::Console::ANSI::KEY_DOWN) { } 0 0 0
elsif ($c eq $Games::Roguelike::Console::ANSI::KEY_LEFT) { } 0 0 0
elsif ($c eq $Games::Roguelike::Console::ANSI::KEY_RIGHT) { } 0 0 0
elsif ($c eq "\cH") { } 0 0 0
425 0 return 'UP'
426 } elsif ($c eq $KEY_DOWN) {
427 0 return 'DOWN'
428 } elsif ($c eq $KEY_LEFT) {
429 0 return 'LEFT'
430 } elsif ($c eq $KEY_RIGHT) {
431 0 return 'RIGHT'
432 } elsif ($c eq "\x8") {
433 0 return 'BACKSPACE'
434 }
435
436 0 return $c;
437 }
438
439 0 ▶sub getch {
Subroutine getch: not called CC=6 SCAR=37.4
Pod documented
440 0 my $self = shift;
441
442 0 my $c;
443 0 ▶ if ($self->{cbuf}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cbuf'}) { } 0 0 0
444 0 $c = substr($self->{cbuf},0,1);
445 0 $self->{cbuf} = substr($self->{cbuf},1);
446 } else {
447 0 $c = $self->getch_raw();
448 }
449
450 0 ▶ if ($c eq $KEY_ESCAPE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq $Games::Roguelike::Console::ANSI::KEY_ESCAPE) 0 0 0
451 0 $c = $self->getch_raw(1);
452 0 ▶ if ($c eq '[') {
Branch: 3 branches
BranchTrueFalseTotal
if ($c eq '[') { } 0 0 0
elsif ($c eq $Games::Roguelike::Console::ANSI::KEY_NOOP) { } 0 0 0
elsif ($c eq $Games::Roguelike::Console::ANSI::KEY_ESCAPE) { } 0 0 0
453 0 $c = $self->getch_raw(1);
454 0 $c = '[' . $c;
455 } elsif ($c eq $KEY_NOOP) {
456 0 return getch();
457 } elsif ($c eq $KEY_ESCAPE) {
458 0 return 'ESC';
459 } else {
460 # unknown escape sequence
461 0 $self->{cbuf} .= $c;
462 0 return 'ESC';
463 }
464 }
465
466 0 return trans($c);
467 }
468
469 0 ▶sub nbgetch_raw {
Subroutine nbgetch_raw: not called CC=3 SCAR=24.8
Pod undocumented
470 0 my $self = shift;
471 0 my $c;
472 0 ▶ if (length($self->{cbuf}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{'cbuf'} > 0) { } 0 0 0
473 0 $c = substr($self->{cbuf},0,1);
474 0 $self->{cbuf} = substr($self->{cbuf},1);
475 } else {
476 0 ▶ if ($self->{usereadkey}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'usereadkey'}) { } 0 0 0
477 0 $c = ReadKey(-1, $self->{in});
478 } else {
479 0 sysread($self->{in}, $c, 1);
480 }
481 }
482 0 return $c;
483 }
484
485 0 ▶sub nbgetch {
Subroutine nbgetch: not called CC=16 SCAR=56.1
Pod documented
486 0 my $self = shift;
487
488 0 my $c = $self->nbgetch_raw();
489
490 0 ▶ if ($c eq $KEY_ESCAPE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq $Games::Roguelike::Console::ANSI::KEY_ESCAPE) { } 0 0 0
elsif (ord $c == 255) { } 0 0 0
491 0 my $c2 = $self->nbgetch_raw();
492 0 ▶ if (!defined($c2)) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $c2) { } 0 0 0
elsif ($c2 eq '[') { } 0 0 0
elsif ($c2 eq $Games::Roguelike::Console::ANSI::KEY_NOOP) { } 0 0 0
elsif ($c2 eq $Games::Roguelike::Console::ANSI::KEY_ESCAPE) { } 0 0 0
493 0 $self->{cbuf} = $KEY_ESCAPE;
494 0 $c = '';
495 } elsif ($c2 eq '[') {
496 0 my $ct = '';
497 0 my $cs = '';
498
499 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
defined $ct and not $ct =~ /^[a-z]$/i and do { do { $ct = $self->nbgetch_raw; $cs .= $ct if defined $ct } } 0 0 0
Condition0%0/3
defined $ct and not $ct =~ /^[a-z]$/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ct and not $ct =~ /^[a-z]$/i
defined $ct not $ct =~ /^[a-z]$/i
Truth table0%0/3
A and not(B)
A= defined $ctB= not $ct =~ /^[a-z]$/i
ABresult
0X0
100
111
500 0 $ct = $self->nbgetch_raw();
501 0 ▶ $cs .= $ct if defined $ct;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ct 0 0 0
502 } while (defined $ct && $ct !~ /^[a-z]$/i);
503
504 0 ▶ if (!defined($ct)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ct) { } 0 0 0
505 0 $self->{cbuf} = $KEY_ESCAPE . '[' . $cs;
506 } else {
507 0 $c = '[' . $cs;
508 }
509 } elsif ($c2 eq $KEY_NOOP) {
510 0 $c = '';
511 } elsif ($c2 eq $KEY_ESCAPE) {
512 0 $c = 'ESC';
513 } else {
514 0 $c = $c2;
515 0 ▶ $c = '' if ord($c) > 240;
Branch: 1 branch
BranchTrueFalseTotal
if ord $c > 240 0 0 0
516 }
517 } elsif (ord($c) == 255) { # telnet esc
518 0 my $c2 = $self->nbgetch_raw();
519 0 ▶ if (!defined($c2)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $c2) { } 0 0 0
elsif ($TELKEY{$c2}) { } 0 0 0
520 0 $self->{cbuf} = $c;
521 0 $c = '';
522 } elsif ($TELKEY{$c2}) {
523 # telnet do/don't
524 0 my $c3 = $self->nbgetch_raw();
525 0 ▶ if (!defined($c3)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $c3) { } 0 0 0
526 0 $self->{cbuf} = $c . $c2;
527 } else {
528 0 $c = $TELKEY{$c2} . ord($c3);
529 }
530 } else {
531 0 $c = $c2;
532 0 ▶ $c = '' if ord($c) > 240;
Branch: 1 branch
BranchTrueFalseTotal
if ord $c > 240 0 0 0
533 }
534 }
535
536 0 return trans($c);
537 }
538
539 5 1;
« World.pm ↑ Summary Console.pm »