Console.pm

filter: Statementstmt 61.7% 63 / 102 Branchbran 29.6% 16 / 54 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 73.3% 11 / 15 Podpod 50.0% 4 / 8 totaltotal 51.0% 94 / 184 CC 29 SCAR 35.6
CC
29
Cov
50%
parsecolor
110.0
new
28.7
attrstr
6.0
CRAP 35.1
« Console/ANSI.pm ↑ Summary World/Daemon.pm »
1 package Games::Roguelike::Console;
2
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 5 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 our @ISA=qw(Exporter);
7 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 5 our $VERSION = '0.4.' . [qw$Revision: 256 $]->[1];
11
12 =head1 NAME
13
14 Games::Roguelike::Console - Platform-neutral console handling
15
16 =head1 SYNOPSIS
17
18 use Games::Roguelike::Console;
19
20 $con = Games::Roguelike::Console->new();
21 $con->attron('bold yellow');
22 $con->addstr('test');
23 $con->attroff();
24 $con->refresh();
25
26 =head1 DESCRIPTION
27
28 Attempts to figure out which Games::Roguelike::Console subclass to instantiate in order to provide console support.
29
30 =head2 METHODS
31
32 =over 4
33
34 =item new ([type=>$stype], [noinit=>1])
35
36 Create a new console, optionally specifying the subtype (win32, ansi, curses or dump:file[:keys]), and the noinit flag (which suppresses terminal initialization.)
37
38 If a type is not specified, a suitable default will be chosen.
39
40 =item addch ([$y, $x], $str);
41
42 =item addstr ([$y, $x], $str);
43
44 =item attrstr ($color, [$y, $x], $str);
45
46 Prints a string at the y, x positions or at the current cursor position (also positions the cursor at y, x+length(str))
47
48 =item attron ($color)
49
50 Turns on color attributes ie: bold blue, white, white on black, black on bold blue
51
52 =item attroff ()
53
54 Turns off color attributes
55
56 =item refresh ()
57
58 Draws the current screen
59
60 =item redraw ()
61
62 Redraws entire screen (if out of sync)
63
64 =item move ($y, $x)
65
66 Moves the cursor to y, x
67
68 =item getch ()
69
70 Reads a character from input
71
72 =item nbgetch ()
73
74 Reads a character from input, non-blocking
75
76 =item parsecolor ()
77
78 Helper function for subclass, parses an attribute then calls "nativecolor($fg, $bg, $bold)", caching the results.
79
80 Subclass can define this instead of nativecolor, if desired.
81
82 =item tagstr ([$y, $x,] $str)
83
84 Moves the cursor to y, x and writes the string $str, which can contain <color> tags
85
86 =item cursor([bool])
87
88 Changes the state of whether the cursor is shown, or returns the current state.
89
90 =item rect(x, y, w, h)
91
92 Sets the left margin (x) for things that parse out carraige returns, and is the
93 rectangle used for scrolling.
94
95 =back
96
97 =head1 SEE ALSO
98
99 L<Games::Roguelike::Console::ANSI>, L<Games::Roguelike::Console::Win32>, L<Games::Roguelike::Console::Curses>
100
101 =head1 AUTHOR
102
103 Erik Aronesty C<earonesty@cpan.org>
104
105 =head1 LICENSE
106
107 This program is free software; you can redistribute it and/or
108 modify it under the same terms as Perl itself.
109
110 See L<http://www.perl.com/perl/misc/Artistic.html> or the included LICENSE file.
111
112 =cut
113
114
115 # platform independent
116 5 ▶use Games::Roguelike::Console::ANSI;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 5 ▶use Games::Roguelike::Console::Dump;
Subroutine BEGIN: called CC=1 SCAR=0.0
118
119 5 our ($OK_WIN32, $OK_CURSES, $DUMPFILE, $DUMPKEYS);
120
121 5 my %CONDATA;
122
123 5 eval{require Games::Roguelike::Console::Win32};
124 5 $OK_WIN32 = !$@;
125
126 5 eval{require Games::Roguelike::Console::Curses};
127 5 $OK_CURSES = !$@;
128
129 # guess best package, and return "new of that package"
130
131 4 ▶sub new {
Subroutine new: called CC=12 SCAR=33.6
Pod documented
132 4 my $pkg = shift;
133 4 my %opt = @_;
134
135 4 ▶ if ($DUMPFILE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Games::Roguelike::Console::DUMPFILE) 3 1 4
136 # override params and just create a dump console
137 3 ▶ return new Games::Roguelike::Console::Dump @_, file=>($DUMPFILE?$DUMPFILE:'>/dev/null'), keys=>$DUMPKEYS;
Branch: 1 branch
BranchTrueFalseTotal
$Games::Roguelike::Console::DUMPFILE ? : 3 0 3
138 }
139
140 1 ▶ $opt{type} = '' if !defined $opt{type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{"type"} 1 0 1
141
142 1 ▶ if ($opt{type} eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"type"} eq "") 1 0 1
143 1 ▶ $opt{type} = 'win32' if $OK_WIN32;
Branch: 1 branch
BranchTrueFalseTotal
if $Games::Roguelike::Console::OK_WIN32 0 1 1
144 1 ▶ $opt{type} = 'curses' if $OK_CURSES;
Branch: 1 branch
BranchTrueFalseTotal
if $Games::Roguelike::Console::OK_CURSES 0 1 1
145 }
146
147 1 ▶ $opt{type} = 'ansi' if $opt{type} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"type"} eq "" 1 0 1
148
149 1 ▶ if ($opt{type} eq 'ansi') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"type"} eq "ansi") 1 0 1
150 1 return new Games::Roguelike::Console::ANSI @_;
151 }
152 0 ▶ if ($opt{type} =~ /dump:?(.*):?(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"type"} =~ /dump:?(.*):?(.*)/) 0 0 0
153 0 return new Games::Roguelike::Console::Dump @_, file=>$1, keys=>$2;
154 }
155 0 ▶ if ($opt{type} eq 'win32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"type"} eq "win32") 0 0 0
156 0 return new Games::Roguelike::Console::Win32 @_;
157 }
158 0 ▶ if ($opt{type} eq 'curses') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"type"} eq "curses") 0 0 0
159 0 return new Games::Roguelike::Console::Curses @_;
160 }
161 }
162
163 # this should be called by sublcass, unless they supply their own defcolor, rect defaults
164 2 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
165 2 my $self = shift;
166 2 my %opts = @_;
167 2 $self->defcolor($opts{defcolor});
168 2 $self->rect($opts{x}, $opts{y}, $opts{w}, $opts{h});
169 }
170
171 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
172 0 croak "hey, this should never be called, override it!";
173 }
174
175 5 my %COLORMAP;
176 0 ▶sub parsecolor {
Subroutine parsecolor: not called CC=10 SCAR=47.0
Pod documented
177 0 my $self = shift;
178 0 my $pkg = ref($self);
179 0 my ($attr, $parsedef) = @_;
180
181 0 ▶ $attr = '' if ! defined $attr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr 0 0 0
182 0 ▶ if ($parsedef || !$COLORMAP{$pkg}{$attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsedef or not $COLORMAP{$pkg}{$attr}) 0 0 0
Condition0%0/3
$parsedef or not $COLORMAP{$pkg}{$attr}
l!l&&r!l&&!r
000
MC/DC0%0/2
$parsedef or not $COLORMAP{$pkg}{$attr}
$parsedef not $COLORMAP{$pkg}{$attr}
Truth table0%0/3
A or not(B)
A= $parsedefB= not $COLORMAP{$pkg}{$attr}
ABresult
1X1
011
000
183 0 my $bg = $CONDATA{$self}->{bg};
184 0 my $fg = $CONDATA{$self}->{fg};
185 0 ▶ $bg = $1 if $attr=~ s/on[\s_]+(.*)$//;
Branch: 1 branch
BranchTrueFalseTotal
if $attr =~ s/on[\s_]+(.*)$// 0 0 0
186 0 $fg = $attr;
187 0 my $bold = 0;
188 0 ▶ $bold = 1 if $fg =~ s/\s*bold\s*//;
Branch: 1 branch
BranchTrueFalseTotal
if $fg =~ s/\s*bold\s*// 0 0 0
189 0 ▶ $fg = 'white' if !$fg;
Branch: 1 branch
BranchTrueFalseTotal
unless $fg 0 0 0
190 # trim spaces in color names
191 0 $fg =~ s/ //g;
192 0 $bg =~ s/ //g;
193 0 ▶ ($fg, $bold) = ('black', 1) if $fg =~ /gray|grey/;
Branch: 1 branch
BranchTrueFalseTotal
if $fg =~ /gray|grey/ 0 0 0
194 0 ▶ ($bg, $bold) = ('black', 1) if $bg =~ /gray|grey/;
Branch: 1 branch
BranchTrueFalseTotal
if $bg =~ /gray|grey/ 0 0 0
195 0 $COLORMAP{$pkg}{$attr} = $self->nativecolor($fg, $bg, $bold);
196 0 ▶ return ($COLORMAP{$pkg}{$attr}, $fg, $bg) if $parsedef;
Branch: 1 branch
BranchTrueFalseTotal
if $parsedef 0 0 0
197 }
198 0 return $COLORMAP{$pkg}{$attr};
199 }
200
201 0 ▶sub nativecolor {
Subroutine nativecolor: not called CC=1 SCAR=6.9
Pod undocumented
202 0 my $self = shift;
203 0 my ($fg, $bg, $bold) = @_;
204 0 croak "nativecolor must be overridden in " . ref($self);
205 }
206
207 184 ▶sub attrch {
Subroutine attrch: called CC=2 SCAR=8.5
Pod undocumented
208 184 my $self = shift;
209 184 my ($color, @args) = @_;
210
211 184 ▶ if ($color) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color) { } 0 184 184
212 0 $self->attron($color);
213 0 $self->addch(@args);
214 0 $self->attroff($color);
215 } else {
216 184 $self->addch(@args);
217 }
218 }
219
220 0 ▶sub attrstr {
Subroutine attrstr: not called CC=2 SCAR=17.9
Pod documented
221 0 my $self = shift;
222 0 my ($color, @args) = @_;
223
224 0 ▶ if ($color) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color) { } 0 0 0
225 0 $self->attron($color);
226 0 $self->addstr(@args);
227 0 $self->attroff($color);
228 } else {
229 0 $self->addch(@args);
230 }
231 }
232
233 2 ▶sub rect {
Subroutine rect: called CC=5 SCAR=17.0
Pod documented
234 2 my $self = shift;
235 2 my ($x, $y, $w, $h) = @_;
236 2 ▶ $CONDATA{$self}->{rx} = $x+0 if defined $x;
Branch: 1 branch
BranchTrueFalseTotal
if defined $x 0 2 2
237 2 ▶ $CONDATA{$self}->{ry} = $y+0 if defined $y;
Branch: 1 branch
BranchTrueFalseTotal
if defined $y 0 2 2
238 2 ▶ $CONDATA{$self}->{rw} = $w+0 if defined $w;
Branch: 1 branch
BranchTrueFalseTotal
if defined $w 0 2 2
239 2 ▶ $CONDATA{$self}->{rh} = $h+0 if defined $h;
Branch: 1 branch
BranchTrueFalseTotal
if defined $h 0 2 2
240 2 return ($CONDATA{$self}->{rx}, $CONDATA{$self}->{ry}, $CONDATA{$self}->{rw}, $CONDATA{$self}->{rh});
241 }
242
243 2 ▶sub defcolor {
Subroutine defcolor: called CC=3 SCAR=11.2
Pod undocumented
244 2 my $self = shift;
245 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 0 2
246 2 ▶ my ($color, $fg, $bg) = $self->parsecolor(($_[0] ? $_[0] : 'white on black'), 1);
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 2 2
247 2 $CONDATA{$self}->{defcolor}= $color;
248 2 $CONDATA{$self}->{fg}= $fg;
249 2 $CONDATA{$self}->{bg}= $bg;
250 }
251 2 return $CONDATA{$self}->{defcolor};
252 }
253
254 5 1;
« Console/ANSI.pm ↑ Summary World/Daemon.pm »