TextMode/Reader/ATASCII.pm

filter: Statementstmt 58.6% 44 / 75 Branchbran 46.4% 26 / 56 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 62.5% 5 / 8 Podpod 100% 4 / 4 totaltotal 54.0% 80 / 148 CC 31 SCAR 44.0
CC
31
Cov
53%
_read
116.8
tab
12.0
store
4.7
CRAP 81.6
« TextMode/Reader/PCBoard.pm ↑ Summary TextMode/Reader/XBin.pm »
1 package Image::TextMode::Reader::ATASCII;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Types::Standard qw( Int Object );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use charnames ':full';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 extends 'Image::TextMode::Reader';
8  
9 # State definitions
10 1 my $S_TXT = 0;
11 1 my $S_ESC = 1;
12 1 my $S_END = 2;
13  
14 1 has 'linewrap' => ( is => 'rw', isa => Int, default => 41 );
15  
16 1 has 'tabstop' => ( is => 'rw', isa => Int, default => 8 );
17  
18 1 has 'image' => ( is => 'rw', isa => Object );
19  
20 1 has 'x' => ( is => 'rw', isa => Int, default => 0 );
21  
22 1 has 'y' => ( is => 'rw', isa => Int, default => 0 );
23  
24 1 has 'state' => ( is => 'rw', isa => Int, default => $S_TXT );
25  
26 sub _read {
27 1 ▶ my ( $self, $image, $fh, $options ) = @_;
Subroutine _read: called CC=26 SCAR=47.6
28  
29 1 ▶ if ( $options->{ width } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"width"}) 0 1 1
30 0 $self->linewrap( $options->{ width } );
31 }
32  
33 1 $image->render_options->{ blink_mode } = 0;
34 1 $self->image( $image );
35  
36 1 $self->state( $S_TXT );
37  
38 1 my $ch;
39 1 ▶ while ( read( $fh, $ch, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, $ch, 1) 8 1 9
40 8 my $state = $self->state;
41  
42 8 ▶ last if tell( $fh ) > $options->{ filesize };
Branch: 1 branch
BranchTrueFalseTotal
if tell $fh > $options->{"filesize"} 0 8 8
43  
44 8 ▶ if ( $state == $S_TXT ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($state == $S_TXT) { } 6 2 8
elsif ($state == $S_ESC) { } 2 0 2
elsif ($state == $S_END) { } 0 0 0
45 6 ▶ if ( ord $ch == 27 ) {
Branch: 16 branches
BranchTrueFalseTotal
if (ord $ch == 27) { } 2 4 6
elsif (ord $ch == 28) { } 0 4 4
elsif (ord $ch == 29) { } 0 4 4
elsif (ord $ch == 30) { } 0 4 4
elsif (ord $ch == 31) { } 0 4 4
elsif (ord $ch == 125) { } 0 4 4
elsif (ord $ch == 126) { } 0 4 4
elsif (ord $ch == 127) { } 0 4 4
elsif (ord $ch == 155) { } 0 4 4
elsif (ord $ch == 156) { } 0 4 4
elsif (ord $ch == 157) { } 0 4 4
elsif (ord $ch == 158) { } 0 4 4
elsif (ord $ch == 159) { } 0 4 4
elsif (ord $ch == 253) { } 0 4 4
elsif (ord $ch == 254) { } 0 4 4
elsif (ord $ch == 255) { } 0 4 4
46 2 $self->state( $S_ESC );
47 }
48 elsif ( ord $ch == 28 ) {
49 0 $self->y( $self->y - 1 );
50 0 ▶ $self->y( 0 ) if $self->y < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->y < 0 0 0 0
51 }
52 elsif ( ord $ch == 29 ) {
53 0 $self->y( $self->y + 1 );
54 }
55 elsif ( ord $ch == 30 ) {
56 0 $self->x( $self->x - 1 );
57 0 ▶ $self->x( 0 ) if $self->x < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->x < 0 0 0 0
58 }
59 elsif ( ord $ch == 31 ) {
60 0 $self->x( $self->x + 1 );
61 0 ▶ $self->x( $self->linewrap - 1 ) if $self->x == $self->linewrap;
Branch: 1 branch
BranchTrueFalseTotal
if $self->x == $self->linewrap 0 0 0
62 }
63 elsif ( ord $ch == 125 ) {
64 0 $self->clear_screen;
65 }
66 elsif ( ord $ch == 126 ) {
67 0 $self->x( $self->x - 1 );
68 0 $self->store( ' ' );
69 0 $self->x( $self->x - 1 );
70 }
71 elsif ( ord $ch == 127 ) {
72 0 $self->tab;
73 }
74 elsif ( ord $ch == 155 ) {
75 0 $self->new_line;
76 }
77 elsif ( ord $ch == 156 ) {
78 # delete line
79 }
80 elsif ( ord $ch == 157 ) {
81 # insert line
82 }
83 elsif ( ord $ch == 158 ) {
84 # clear tab stop
85 }
86 elsif ( ord $ch == 159 ) {
87 # set tab stop
88 }
89 elsif ( ord $ch == 253 ) {
90 # buzzer
91 }
92 elsif ( ord $ch == 254 ) {
93 # delete char
94 }
95 elsif ( ord $ch == 255 ) {
96 # insert char
97 }
98 else {
99 4 $self->store( $ch );
100 }
101 }
102 elsif ( $state == $S_ESC ) {
103 2 $self->store( $ch );
104 2 $self->state( $S_TXT );
105 }
106 elsif ( $state == $S_END ) {
107 0 last;
108 }
109 else {
110 0 $self->state( $S_TXT );
111 }
112 }
113  
114 1 return $image;
115 }
116  
117 sub clear_screen {
118 0 ▶ my $self = shift;
Subroutine clear_screen: not called CC=1 SCAR=6.9
Pod documented
119 0 $self->image->clear_screen;
120 0 $self->x( 0 );
121 0 $self->y( 0 );
122 }
123  
124 sub new_line {
125 0 ▶ my $self = shift;
Subroutine new_line: not called CC=1 SCAR=6.9
Pod documented
126  
127 0 $self->y( $self->y + 1 );
128 0 $self->x( 0 );
129 }
130  
131 sub tab {
132 0 ▶ my $self = shift;
Subroutine tab: not called CC=3 SCAR=24.8
Pod documented
133 0 my $count = ( $self->x + 1 ) % $self->tabstop;
134 0 ▶ if ( $count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 0 0 0
135 0 $count = $self->tabstop - $count;
136 0 for ( 1 .. $count ) {
137 0 $self->store( ' ' );
138 }
139 }
140 }
141  
142 sub store {
143 6 ▶ my $self = shift;
Subroutine store: called CC=4 SCAR=15.5
Pod documented
144 6 my $char = shift;
145 6 my $x = shift;
146 6 my $y = shift;
147  
148 6 ▶ if ( defined $x and defined $y ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x and defined $y) { } 0 6 6
Condition33%1/3
defined $x and defined $y
!ll&&!rl&&r
600
MC/DC0%0/2
defined $x and defined $y
defined $x defined $y
Truth table33%1/3
A and B
A= defined $xB= defined $y
ABresult
0X0
100
111
149 0 $self->image->putpixel( { char => $char, bg => 0, fg => 1 }, $x, $y );
150 }
151 else {
152 6 $self->image->putpixel( { char => $char, bg => 0, fg => 1 },
153 $self->x, $self->y );
154 6 $self->x( $self->x + 1 );
155 }
156  
157 6 ▶ if ( $self->x >= $self->linewrap ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->x >= $self->linewrap) 0 6 6
158 0 $self->new_line;
159 }
160 }
161  
162 =head1 NAME
163  
164 Image::TextMode::Reader::ATASCII - Reads ATASCII files
165  
166 =head1 DESCRIPTION
167  
168 Provides reading capabilities for the ATASCII format.
169  
170 =head1 ACCESSORS
171  
172 =over 4
173  
174 =item * tabstop - every Nth character will be a tab stop location (default: 8)
175  
176 =item * x - current x (default: 0)
177  
178 =item * y - current y (default: 0)
179  
180 =item * state - state of the parser (default: C<$S_TXT>)
181  
182 =item * image - the image we're parsing into
183  
184 =item * linewrap - max width before we wrap to the next line (default: 80)
185  
186 =back
187  
188 =head1 METHODS
189  
190 =head2 clear_screen( )
191  
192 Clears all data on the canvas.
193  
194 =head2 new_line( )
195  
196 Simulates a C<\n> character.
197  
198 =head2 tab( )
199  
200 Simulates a C<\t> character.
201  
202 =head2 store( $char, $x, $y )
203  
204 Stores C<$char> at position C<$x, $y>.
205  
206 =head1 AUTHOR
207  
208 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
209  
210 =head1 COPYRIGHT AND LICENSE
211  
212 Copyright 2008-2015 by Brian Cassidy
213  
214 This library is free software; you can redistribute it and/or modify
215 it under the same terms as Perl itself.
216  
217 =cut
218  
219 1 1;
« TextMode/Reader/PCBoard.pm ↑ Summary TextMode/Reader/XBin.pm »