File Coverage

blib/lib/Image/TextMode/Writer/ANSIMation.pm
Criterion Covered Total %
statement 33 35 94.2
branch 9 12 75.0
condition 1 2 50.0
subroutine 4 4 100.0
pod n/a
total 47 53 88.6


line stmt bran cond sub pod time code
1             package Image::TextMode::Writer::ANSIMation;
2              
3 1     1   535 use Moo;
  1         2  
  1         8  
4 1     1   351 use charnames ':full';
  1         5  
  1         7  
5              
6             extends 'Image::TextMode::Writer';
7              
8             sub _write {
9 1     1   3 my ( $self, $anim, $fh, $options ) = @_;
10              
11             # clear screen
12 1         5 print $fh "\N{ESCAPE}[2J";
13              
14 1         3 my $prevattr = '';
15 1         2 for my $image ( @{ $anim->frames } ) {
  1         32  
16 2         19 my ( $width, $height ) = $image->dimensions;
17              
18 2         56 for my $y ( 0 .. $height - 1 ) {
19 2         7 my $max_x = $image->max_x( $y );
20              
21 2 50       6 unless ( defined $max_x ) {
22 0         0 print $fh "\n";
23 0         0 next;
24             }
25              
26 2         3 for my $x ( 0 .. $max_x ) {
27 8   50     17 my $pixel = $image->getpixel( $x, $y )
28             || { char => ' ', attr => 7 };
29 8         64 my $attr = _gen_args( $pixel->{ attr } );
30 8 50       18 if ( $attr ne $prevattr ) {
31 8         13 print $fh "\N{ESCAPE}[0;", _gen_args( $pixel->{ attr } ),
32             'm';
33 8         13 $prevattr = $attr;
34             }
35 8         18 print $fh $pixel->{ char };
36             }
37 2 50       8 print $fh "\n" unless $max_x == 79;
38             }
39              
40             # set position
41 2 100       39 if ( $image ne $anim->frames->[ -1 ] ) {
42 1         12 print $fh "\N{ESCAPE}[H";
43             }
44             }
45              
46             # clear attrs
47 1         10 print $fh "\N{ESCAPE}[0m";
48             }
49              
50             sub _gen_args {
51 16     16   19 my $attr = shift;
52 16         19 my $fg = 30 + ( $attr & 7 );
53 16         12 my $bg = 40 + ( ( $attr & 112 ) >> 4 );
54 16 100       28 my $bl = ( $attr & 128 ) ? 5 : '';
55 16 100       19 my $in = ( $attr & 8 ) ? 1 : '';
56 16         18 return join( q{;}, grep { length } ( $bl, $in, $fg, $bg ) );
  64         73  
57             }
58              
59             =head1 NAME
60              
61             Image::TextMode::Writer::ANSIMation - Writes ANSIMation files
62              
63             =head1 DESCRIPTION
64              
65             Provides writing capabilities for the ANSIMation format.
66              
67             =head1 AUTHOR
68              
69             Brian Cassidy Ebricas@cpan.orgE
70              
71             =head1 COPYRIGHT AND LICENSE
72              
73             Copyright 2008-2014 by Brian Cassidy
74              
75             This library is free software; you can redistribute it and/or modify
76             it under the same terms as Perl itself.
77              
78             =cut
79              
80             1;