File: | blib/lib/Image/Caa/DriverANSI.pm |
Coverage: | 29.2% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
1 | package Image::Caa::DriverANSI; | |||||
2 | ||||||
3 | 1 1 1 | 4 2 6 | use strict; | |||
4 | 1 1 1 | 5 2 6 | use warnings; | |||
5 | ||||||
6 | sub new { | |||||
7 | 9 | 34 | my ($class) = @_; | |||
8 | ||||||
9 | 9 | 33 | my $self = bless {}, $class; | |||
10 | ||||||
11 | 9 | 319 | $self->{color_map} = { | |||
12 | int(Image::Caa::CAA_COLOR_BLACK) => [30,1], | |||||
13 | int(Image::Caa::CAA_COLOR_RED) => [31,1], | |||||
14 | int(Image::Caa::CAA_COLOR_GREEN) => [32,1], | |||||
15 | int(Image::Caa::CAA_COLOR_YELLOW) => [33,1], | |||||
16 | int(Image::Caa::CAA_COLOR_BLUE) => [34,1], | |||||
17 | int(Image::Caa::CAA_COLOR_MAGENTA) => [35,1], | |||||
18 | int(Image::Caa::CAA_COLOR_CYAN) => [36,1], | |||||
19 | int(Image::Caa::CAA_COLOR_LIGHTGRAY) => [37,1], | |||||
20 | ||||||
21 | int(Image::Caa::CAA_COLOR_DARKGRAY) => [30,0], | |||||
22 | int(Image::Caa::CAA_COLOR_LIGHTRED) => [31,0], | |||||
23 | int(Image::Caa::CAA_COLOR_LIGHTGREEN) => [32,0], | |||||
24 | int(Image::Caa::CAA_COLOR_BROWN) => [33,0], | |||||
25 | int(Image::Caa::CAA_COLOR_LIGHTBLUE) => [34,0], | |||||
26 | int(Image::Caa::CAA_COLOR_LIGHTMAGENTA) => [35,0], | |||||
27 | int(Image::Caa::CAA_COLOR_LIGHTCYAN) => [36,0], | |||||
28 | int(Image::Caa::CAA_COLOR_WHITE) => [37,0], | |||||
29 | }; | |||||
30 | ||||||
31 | 9 | 92 | $self->{color_pairs} = {}; | |||
32 | ||||||
33 | 9 | 60 | return $self; | |||
34 | } | |||||
35 | ||||||
36 | sub init { | |||||
37 | 0 | my ($self) = @_; | ||||
38 | ||||||
39 | 0 | $self->{current_color_key} = ''; | ||||
40 | 0 | $self->{last_x} = 0; | ||||
41 | } | |||||
42 | ||||||
43 | sub set_color{ | |||||
44 | 0 | my ($self, $fg, $bg) = @_; | ||||
45 | ||||||
46 | 0 | my $key = "$fg:$bg"; | ||||
47 | ||||||
48 | 0 | $self->{current_color_key} = $key; | ||||
49 | ||||||
50 | 0 | if (!defined $self->{color_pairs}->{$key}){ | ||||
51 | ||||||
52 | 0 0 | my ($fg_col, $fg_dark) = @{$self->{color_map}->{$fg}}; | ||||
53 | 0 0 | my ($bg_col, $bg_dark) = @{$self->{color_map}->{$bg}}; | ||||
54 | ||||||
55 | 0 | $bg_col += 10; | ||||
56 | ||||||
57 | 0 | $self->{color_pairs}->{$key} = "\e[${fg_col};".($fg_dark?2:1).";${bg_col};".($bg_dark?6:5)."m"; | ||||
58 | } | |||||
59 | } | |||||
60 | ||||||
61 | sub putchar{ | |||||
62 | 0 | my ($self, $x, $y, $outch) = @_; | ||||
63 | ||||||
64 | 0 | if ($x < $self->{last_x}){ | ||||
65 | ||||||
66 | 0 | print "\n"; | ||||
67 | } | |||||
68 | ||||||
69 | 0 | $self->{last_x} = $x; | ||||
70 | ||||||
71 | 0 | print $self->{color_pairs}->{$self->{current_color_key}}; | ||||
72 | 0 | print $outch; | ||||
73 | 0 | print "\e[0m"; | ||||
74 | } | |||||
75 | ||||||
76 | sub fini { | |||||
77 | 0 | my ($self) = @_; | ||||
78 | ||||||
79 | 0 | print "\n"; | ||||
80 | } | |||||
81 | ||||||
82 | 1; |