File Coverage

blib/lib/Games/Roguelike/Console/Dump.pm
Criterion Covered Total %
statement 65 87 74.7
branch 11 20 55.0
condition 1 3 33.3
subroutine 12 20 60.0
pod 12 14 85.7
total 101 144 70.1


line stmt bran cond sub pod time code
1             package Games::Roguelike::Console::Dump;
2            
3             =head1 NAME
4            
5             Games::Roguelike::Console::Dump - fake console that dumps to file, for testing
6            
7             =head1 SYNOPSIS
8            
9             use Games::Roguelike::Console::Dump;
10            
11             $con = Games::Roguelike::Console::Dump->new(keys=>'qY', file=>'/dev/null');
12            
13             =head1 DESCRIPTION
14            
15             Fake console that dumps screens to file, used for testing game scripts without needing "curses" support.
16            
17             Notably, the new function takes a keystroke string and a file as arguments.
18            
19             Inherits from Games::Roguelike::Console. See Games::Roguelike::Console for list of methods.
20            
21             =head1 SEE ALSO
22            
23             L
24            
25             =cut
26            
27 5     5   35 use strict;
  5         13  
  5         243  
28 5     5   31 use IO::File;
  5         12  
  5         1096  
29 5     5   34 use Carp qw(cluck croak);
  5         12  
  5         313  
30 5     5   31 use Games::Roguelike::Utils qw(:DEFAULT);
  5         8  
  5         660  
31 5     5   33 use base 'Games::Roguelike::Console';
  5         9  
  5         12272  
32            
33             our $VERSION = '0.4.' . [qw$Revision: 233 $]->[1];
34            
35             sub new {
36 3     3 1 7 my $pkg = shift;
37 3 50       10 croak "usage: Games::Roguelike::Console::Dump->new()" unless $pkg;
38            
39 3         11 my $self = bless {}, $pkg;
40 3         11 $self->init(@_);
41 3         15 return $self;
42             }
43            
44             my $STD;
45             sub init {
46 3     3 0 6 my $self = shift;
47            
48 3         27 my %opt = @_;
49            
50 3 50       10 if ($opt{file}) {
51 3         75 $self->{file} = $opt{file};
52 3         28 $opt{out} = new IO::File;
53 3         447 open($opt{out}, ">" . $opt{file});
54             }
55            
56 3 50       20 $self->{out} = *STDOUT{IO}
57             unless $self->{out} = $opt{out};
58            
59 3         22 $self->{out}->autoflush(1);
60            
61 3         166 $self->{buf} = [];
62 3         13 $self->{cbuf} = $opt{keys};
63             }
64            
65             sub DESTROY {
66 2     2   31 my $self = shift;
67 2 50       11 if ($self->{out}) {
68 2         402 close $self->{out};
69             }
70             }
71            
72            
73             sub clear {
74 0     0 0 0 my $self = shift;
75 0         0 my $out = $self->{out};
76 0         0 print $out ("******************\n"); #clear the screen
77             }
78            
79 0     0 1 0 sub redraw {
80             }
81            
82 0     0 1 0 sub attron {
83             }
84            
85 0     0 1 0 sub attroff {
86             }
87            
88             sub addstr {
89 2163     2163 1 3121 my $self = shift;
90 2163         3633 my $str = pop @_;
91 2163 50       8645 if (@_== 0) {
    50          
92 0         0 for (my $i = 0; $i < length($str); ++$i) {
93 0         0 $self->{buf}->[$self->{cy}][$self->{cx}+$i] = substr($str,$i,1);
94             }
95 0         0 $self->{cx} += length($str);
96             } elsif (@_==2) {
97 2163         3102 my ($y, $x) = @_;
98 2163         6250 for (my $i = 0; $i < length($str); ++$i) {
99 2163         16950 $self->{buf}->[$y][$x+$i] = substr($str,$i,1);
100             }
101 2163         4188 $self->{cy}=$y;
102 2163         15620 $self->{cx}=$x+length($str);
103             }
104             }
105            
106             sub tagstr {
107 0     0 1 0 my $self = shift;
108 0         0 my $str = pop @_;
109 0         0 $str =~ s/<[^>]+>//g;
110 0         0 $self->addstr(@_, $str);
111             }
112            
113            
114             sub refresh {
115 3     3 1 6 my $self = shift;
116 3         9 my $out = $self->{out};
117            
118 3         7 my $cc = 0;
119            
120 3         8 for (my $y = 0; $y <= @{$self->{buf}}; ++$y) {
  63         177  
121 60 100       148 next if !$self->{buf}->[$y];
122 54 50 33     131 next if $self->{cur}->[$y] && (join('',@{$self->{buf}->[$y]}) eq join('',@{$self->{cur}->[$y]}));
  0         0  
  0         0  
123 54         310 print $out sprintf("%03d|", $y), @{$self->{buf}->[$y]}, "\n";
  54         890  
124 54         82 @{$self->{cur}->[$y]} = @{$self->{buf}->[$y]};
  54         1066  
  54         109  
125 54         102 ++$cc;
126             }
127            
128 3         13 ++$self->{refrc};
129            
130 3 50       12 return unless $cc > 0;
131            
132 3         121 print $out "\n<*" . $self->{refrc} . ">\n";
133             }
134            
135             sub move {
136 0     0 1 0 my $self = shift;
137 0         0 my ($y, $x) = @_;
138 0         0 $self->{cy}=$y;
139 0         0 $self->{cx}=$x;
140             }
141            
142 0     0 1 0 sub cursor {
143             }
144            
145             sub addch {
146 2163     2163 1 3234 my $self = shift;
147 2163         20743 $self->addstr(@_);
148             }
149            
150             sub getch {
151 3     3 1 9 my $self = shift;
152            
153 3         6 my $c;
154 3 50       13 if ($self->{cbuf}) {
155 3         10 $c = substr($self->{cbuf},0,1);
156 3         9 $self->{cbuf} = substr($self->{cbuf},1);
157             }
158            
159 3         59 return $c;
160             }
161            
162             sub nbgetch {
163 0     0 1   my $self = shift;
164 0           return $self->getch();
165             }
166            
167             1;