PrettyCure/Image/Terminal.pm

filter: Statementstmt 85.3% 111 / 130 Branchbran 69.5% 32 / 46 Conditioncond 76.9% 30 / 39 MC/DCmcdc 65.0% 13 / 20 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 5 totaltotal 78.7% 204 / 259 CC 50 SCAR 28.0
CC
50
Cov
80%
query_da1
72.0
detect
19.0
render
7.0
CRAP 16.4
↑ Summary PrettyCure/Image.pm »
1 1 ▶use v5.44;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use experimental qw(class defer signature_named_parameters);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # Draws an image in the terminal, by the best way the terminal has:
6 #
7 # kitty the Kitty graphics protocol (Kitty, WezTerm, Ghostty)
8 # iterm2 the inline images of iTerm2 (iTerm2, WezTerm)
9 # sixel Sixel (foot, mlterm, xterm with sixel, ...)
10 # halfblock 24-bit colors and the upper half block (▀): two pixels a cell, in any terminal
11 #
12 # print Acme::PrettyCure::Image::Terminal->render($imager, width => 40);
13 package Acme::PrettyCure::Image::Terminal;
14  
15 1 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Acme::PrettyCure::Image::PNG;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 our @PROTOCOLS = qw(kitty iterm2 sixel halfblock);
19  
20 # The picture of an Imager image as `width` cells: drawn by `protocol` (`auto`: by `detect`).
21 7 ▶sub render (
Subroutine render: called CC=7 SCAR=19.5
Pod undocumented
22 $class, $imager,
23 7 :$protocol = 'auto',
24 7 :$width = 40,
25 7 :$env = \%ENV,
26 7 :$query = \&query_da1
27 )
28 {
29 7 ▶ $protocol = $class->detect(env => $env, query => $query) if $protocol eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $protocol eq "auto" 0 7 7
30 my $drawer = {
31 kitty => 'Acme::PrettyCure::Image::Terminal::Kitty',
32 iterm2 => 'Acme::PrettyCure::Image::Terminal::ITerm2',
33 sixel => 'Acme::PrettyCure::Image::Terminal::Sixel',
34 halfblock => 'Acme::PrettyCure::Image::Terminal::HalfBlock',
35 7 ▶ }->{$protocol} // die "unknown protocol $protocol (@PROTOCOLS or auto)\n";
Condition100%2/2
{"kitty", "Acme::PrettyCure::Image::Terminal::Kitty", "iterm2", "Acme::PrettyCure::Image::Terminal::ITerm2", "sixel", "Acme::PrettyCure::Image::Terminal::Sixel", "halfblock", "Acme::PrettyCure::Image::Terminal::HalfBlock"}->{$protocol} // die("unknown protocol $protocol (@Acme::PrettyCure::Image::Terminal::PROTOCOLS or auto)\n")
l!l
61
MC/DC100%1/1
{"kitty", "Acme::PrettyCure::Image::Terminal::Kitty", "iterm2", "Acme::PrettyCure::Image::Terminal::ITerm2", "sixel", "Acme::PrettyCure::Image::Terminal::Sixel", "halfblock", "Acme::PrettyCure::Image::Terminal::HalfBlock"}->{$protocol} // die("unknown protocol $protocol (@Acme::PrettyCure::Image::Terminal::PROTOCOLS or auto)\n")
{"kitty", "Acme::PrettyCure::Image::Terminal::Kitty", "iterm2", "Acme::PrettyCure::Image::Terminal::ITerm2", "sixel", "Acme::PrettyCure::Image::Terminal::Sixel", "halfblock", "Acme::PrettyCure::Image::Terminal::HalfBlock"}->{$protocol}
Truth table100%2/2
A
A= {"kitty", "Acme::PrettyCure::Image::Terminal::Kitty", "iterm2", "Acme::PrettyCure::Image::Terminal::ITerm2", "sixel", "Acme::PrettyCure::Image::Terminal::Sixel", "halfblock", "Acme::PrettyCure::Image::Terminal::HalfBlock"}->{$protocol}
Aresult
11
00
36 6 return $drawer->new(columns => $width)->draw($imager);
37 }
38  
39 # The protocol of the terminal, from the environment (ACME_PRETTYCURE_IMAGE_PROTOCOL first), or else
40 # from its answer to DA1 (`query`, a code that returns it), or else halfblock.
41 15 ▶sub detect ($class, :$env = \%ENV, :$query = \&query_da1) {
Subroutine detect: called CC=19 SCAR=29.4
Pod undocumented
42 15 my %e = %$env;
43 15 ▶ my $want = $e{ACME_PRETTYCURE_IMAGE_PROTOCOL} // '';
Condition100%2/2
$e{"ACME_PRETTYCURE_IMAGE_PROTOCOL"} // ""
l!l
114
MC/DC100%1/1
$e{"ACME_PRETTYCURE_IMAGE_PROTOCOL"} // ""
$e{"ACME_PRETTYCURE_IMAGE_PROTOCOL"}
Truth table100%2/2
A
A= $e{"ACME_PRETTYCURE_IMAGE_PROTOCOL"}
Aresult
11
00
44 15 ▶ return $want if grep { $_ eq $want } @PROTOCOLS;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $want;} @Acme::PrettyCure::Image::Terminal::PROTOCOLS 1 14 15
45  
46 14 ▶ my $term = $e{TERM} // '';
Condition100%2/2
$e{"TERM"} // ""
l!l
86
MC/DC100%1/1
$e{"TERM"} // ""
$e{"TERM"}
Truth table100%2/2
A
A= $e{"TERM"}
Aresult
11
00
47 14 ▶ my $program = $e{TERM_PROGRAM} // '';
Condition100%2/2
$e{"TERM_PROGRAM"} // ""
l!l
311
MC/DC100%1/1
$e{"TERM_PROGRAM"} // ""
$e{"TERM_PROGRAM"}
Truth table100%2/2
A
A= $e{"TERM_PROGRAM"}
Aresult
11
00
48 return 'kitty'
49 if defined $e{KITTY_WINDOW_ID}
50 14 ▶ || $term eq 'xterm-kitty'
Branch: 1 branch
BranchTrueFalseTotal
if defined $e{"KITTY_WINDOW_ID"} or $term eq "xterm-kitty" or $term eq "xterm-ghostty" or $program eq "ghostty" or $program eq "WezTerm" 5 9 14
Condition100%12/12
defined $e{"KITTY_WINDOW_ID"} or $term eq "xterm-kitty" or $term eq "xterm-ghostty" or $program eq "ghostty" or $program eq "WezTerm"
l!l&&r!l&&!r
419
defined $e{"KITTY_WINDOW_ID"} or $term eq "xterm-kitty" or $term eq "xterm-ghostty" or $program eq "ghostty"
l!l&&r!l&&!r
3110
defined $e{"KITTY_WINDOW_ID"} or $term eq "xterm-kitty" or $term eq "xterm-ghostty"
l!l&&r!l&&!r
2111
defined $e{"KITTY_WINDOW_ID"} or $term eq "xterm-kitty"
l!l&&r!l&&!r
1112
MC/DC100%5/5
defined $e{"KITTY_WINDOW_ID"} or $term eq "xterm-kitty" or $term eq "xterm-ghostty" or $program eq "ghostty" or $program eq "WezTerm"
defined $e{"KITTY_WINDOW_ID"} $term eq "xterm-kitty" $term eq "xterm-ghostty" $program eq "ghostty" $program eq "WezTerm"
Truth table100%6/6
A or B or C or D or E
A= defined $e{"KITTY_WINDOW_ID"}B= $term eq "xterm-kitty"C= $term eq "xterm-ghostty"D= $program eq "ghostty"E= $program eq "WezTerm"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
51 || $term eq 'xterm-ghostty'
52 || $program eq 'ghostty'
53 || $program eq 'WezTerm';
54 9 ▶ return 'iterm2' if $program eq 'iTerm.app' || ($e{LC_TERMINAL} // '') eq 'iTerm2';
Branch: 1 branch
BranchTrueFalseTotal
if $program eq "iTerm.app" or ($e{"LC_TERMINAL"} // "") eq "iTerm2" 2 7 9
Condition100%5/5
$program eq "iTerm.app" or ($e{"LC_TERMINAL"} // "") eq "iTerm2"
l!l&&r!l&&!r
117
$e{"LC_TERMINAL"} // ""
l!l
17
MC/DC100%2/2
$program eq "iTerm.app" or ($e{"LC_TERMINAL"} // "") eq "iTerm2"
$program eq "iTerm.app" $e{"LC_TERMINAL"}
Truth table100%3/3
A or B
A= $program eq "iTerm.app"B= $e{"LC_TERMINAL"}
ABresult
1X1
011
000
55 7 ▶ return 'sixel' if $term =~ /sixel|\Amlterm|\Afoot|\Ayaft|\Acontour/;
Branch: 1 branch
BranchTrueFalseTotal
if $term =~ /sixel|\Amlterm|\Afoot|\Ayaft|\Acontour/u 3 4 7
56 4 ▶ return 'sixel' if $query && has_sixel($query->() // '');
Branch: 1 branch
BranchTrueFalseTotal
if $query and has_sixel(&$query() // "") 1 3 4
Condition80%4/5
$query and has_sixel(&$query() // "")
!ll&&!rl&&r
031
&$query() // ""
l!l
22
MC/DC50%1/2
$query and has_sixel(&$query() // "")
$query has_sixel(&$query() // "")
MC/DC100%1/1
&$query() // ""
&$query()
Truth table66%2/3
A and B
A= $queryB= has_sixel(&$query() // "")
ABresult
0X0
100
111
Truth table100%2/2
A
A= &$query()
Aresult
11
00
57 3 return 'halfblock';
58 }
59  
60 # Whether the answer to DA1 (CSI ? Ps ; ... c) lists 4, Sixel graphics.
61 4 ▶sub has_sixel ($answer) {
Subroutine has_sixel: called CC=2 SCAR=6.9
Pod undocumented
62 4 ▶ my ($attrs) = $answer =~ /\e\[\?([\d;]*)c/ or return false;
Branch: 1 branch
BranchTrueFalseTotal
my($attrs) = $answer =~ /\e\[\?([\d;]*)c/u or 2 2 4
63 2 return !!grep { $_ eq '4' } split /;/, $attrs;
64 }
65  
66 # Asks the terminal its attributes (DA1), when both STDIN and STDOUT are the terminal, and returns its
67 # answer, or none. It waits 0.1 seconds for each byte.
68 0 ▶sub query_da1 () {
Subroutine query_da1: not called CC=8 SCAR=42.8
Pod undocumented
69  
70 # Both are the terminal itself (not "interactive"): the question goes out and the answer comes in.
71 0 ▶ return unless -t STDIN && -t STDOUT; ## no critic (ProhibitInteractiveTest)
Branch: 1 branch
BranchTrueFalseTotal
unless -t STDIN and -t STDOUT 0 0 0
Condition0%0/3
-t STDIN and -t STDOUT
!ll&&!rl&&r
000
MC/DC0%0/2
-t STDIN and -t STDOUT
-t STDIN -t STDOUT
Truth table0%0/3
A and B
A= -t STDINB= -t STDOUT
ABresult
0X0
100
111
72 0 require POSIX;
73 0 my $fd = fileno STDIN;
74 0 my $orig = POSIX::Termios->new;
75 0 ▶ $orig->getattr($fd) or return;
Branch: 1 branch
BranchTrueFalseTotal
$orig->getattr($fd) or return 0 0 0
76 0 my $raw = POSIX::Termios->new;
77 0 $raw->getattr($fd);
78 0 $raw->setlflag($raw->getlflag & ~(POSIX::ECHO() | POSIX::ICANON()));
79 0 $raw->setcc(POSIX::VMIN(), 0);
80 0 $raw->setcc(POSIX::VTIME(), 1);
81 0 ▶ $raw->setattr($fd, POSIX::TCSANOW()) or return;
Branch: 1 branch
BranchTrueFalseTotal
$raw->setattr($fd, 0) or return 0 0 0
82 0 defer { $orig->setattr($fd, POSIX::TCSANOW()) }
83  
84 0 syswrite STDOUT, "\e[c";
85 0 my $answer = '';
86 0 ▶ while (sysread STDIN, my $byte, 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread STDIN, my $byte, 1) 0 0 0
87 0 $answer .= $byte;
88 0 ▶ last if $byte eq 'c' || length $answer > 64;
Branch: 1 branch
BranchTrueFalseTotal
if $byte eq "c" or length $answer > 64 0 0 0
Condition0%0/3
$byte eq "c" or length $answer > 64
l!l&&r!l&&!r
000
MC/DC0%0/2
$byte eq "c" or length $answer > 64
$byte eq "c" length $answer > 64
Truth table0%0/3
A or B
A= $byte eq "c"B= length $answer > 64
ABresult
1X1
011
000
89 }
90 0 return $answer;
91 }
92  
93 # The pixels of an Imager image, `width` pixels wide (never wider than it is) and as tall as keeps its
94 # shape: (width, height, rows), each row a string of R G B bytes.
95 8 ▶sub pixels ($imager, $width) {
Subroutine pixels: called CC=6 SCAR=19.0
Pod undocumented
96 8 my $img = $imager;
97 8 ▶ $img = $img->to_rgb8 if $img->type eq 'paletted';
Branch: 1 branch
BranchTrueFalseTotal
if $img->type eq "paletted" 0 8 8
98 8 ▶ $img = $img->convert(preset => 'noalpha') if $img->getchannels == 2 || $img->getchannels == 4;
Branch: 1 branch
BranchTrueFalseTotal
if $img->getchannels == 2 or $img->getchannels == 4 0 8 8
Condition33%1/3
$img->getchannels == 2 or $img->getchannels == 4
l!l&&r!l&&!r
008
MC/DC0%0/2
$img->getchannels == 2 or $img->getchannels == 4
$img->getchannels == 2 $img->getchannels == 4
Truth table33%1/3
A or B
A= $img->getchannels == 2B= $img->getchannels == 4
ABresult
1X1
011
000
99 8 ▶ $img = $img->convert(preset => 'rgb') if $img->getchannels == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $img->getchannels == 1 0 8 8
100 8 ▶ $img = $img->scale(xpixels => $width) if $width < $img->getwidth;
Branch: 1 branch
BranchTrueFalseTotal
if $width < $img->getwidth 1 7 8
101 8 my @rows = map { scalar $img->getsamples(y => $_, type => '8bit') } 0 .. $img->getheight - 1;
102 8 return ($img->getwidth, $img->getheight, \@rows);
103 }
104  
105 # How a protocol draws a picture `columns` cells wide.
106 class Acme::PrettyCure::Image::Terminal::Drawer;
107  
108 1 field $columns :param :reader;
109  
110 # The pixels a cell is drawn with, across, for the protocols that the terminal scales (about the width of
111 # a cell).
112 5 ▶method pixels_per_column { 10 }
Subroutine pixels_per_column: called CC=1 SCAR=0.0
113  
114 6 ▶method draw ($imager) {
Subroutine draw: called CC=1 SCAR=0.0
115 6 return $self->picture(
116 Acme::PrettyCure::Image::Terminal::pixels($imager, $columns * $self->pixels_per_column));
117 }
118  
119 # The Kitty graphics protocol: the RGB pixels (f=24), in chunks of 4096 bytes of base64, shown `columns`
120 # cells wide (c=).
121 class Acme::PrettyCure::Image::Terminal::Kitty :isa(Acme::PrettyCure::Image::Terminal::Drawer);
122  
123 1 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
124  
125 2 ▶method picture ($width, $height, $rows) {
Subroutine picture: called CC=4 SCAR=13.9
126 2 my $data = encode_base64(join('', @$rows), '');
127 2 my @chunks = unpack '(a4096)*', $data;
128 2 my $out = '';
129 2 for my ($i, $chunk) (builtin::indexed @chunks) {
130 11 ▶ my $more = $i < $#chunks ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$i < $#chunks ? : 9 2 11
131 11 ▶ my $keys =
Branch: 1 branch
BranchTrueFalseTotal
$i == 0 ? : 2 9 11
132 $i == 0 ? sprintf('a=T,f=24,s=%d,v=%d,c=%d,', $width, $height, $self->columns) : '';
133 11 $out .= "\e_G${keys}m=$more;$chunk\e\\";
134 }
135 2 return "$out\n";
136 }
137  
138 # The inline images of iTerm2: a PNG, shown `columns` cells wide.
139 class Acme::PrettyCure::Image::Terminal::ITerm2 :isa(Acme::PrettyCure::Image::Terminal::Drawer);
140  
141 1 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
142  
143 1 ▶method picture ($width, $height, $rows) {
Subroutine picture: called CC=1 SCAR=0.0
144 1 my $png = Acme::PrettyCure::Image::PNG->encode($width, $height, $rows);
145 1 return sprintf("\e]1337;File=inline=1;size=%d;width=%d;preserveAspectRatio=1:%s\a\n",
146 length $png, $self->columns, encode_base64($png, ''));
147 }
148  
149 # Sixel: the pixels in the colors of the 6×6×6 cube, six rows to a band, with runs of a sixel as "!n".
150 class Acme::PrettyCure::Image::Terminal::Sixel :isa(Acme::PrettyCure::Image::Terminal::Drawer);
151  
152 2 ▶method picture ($width, $height, $rows) {
Subroutine picture: called CC=6 SCAR=17.9
153  
154 # The color of each pixel: its index in the cube.
155 my @color = map {
156 2 my @rgb = unpack 'C*', $_;
157 [
158 map {
159 4 my ($r, $g, $b) = map { int($_ * 5 / 255 + 0.5) } @rgb[ $_ * 3 .. $_ * 3 + 2 ];
160 11 $r * 36 + $g * 6 + $b
161 } 0 .. $width - 1
162 ]
163 } @$rows;
164  
165 2 my %used = map { $_ => 1 } map { @$_ } @color;
166 2 my $out = sprintf("\eP0;1;0q\"1;1;%d;%d", $width, $height);
167 2 for my $c (sort { $a <=> $b } keys %used) {
168 6 my ($r, $g, $b) = (int($c / 36), int($c / 6) % 6, $c % 6);
169 6 $out .= sprintf('#%d;2;%d;%d;%d', $c, map { int($_ * 100 / 5 + 0.5) } $r, $g, $b);
170 }
171 2 ▶ for (my $top = 0 ; $top < $height ; $top += 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top < $height) 2 2 4
172 2 my @band = grep { defined } @color[ $top .. $top + 5 ];
173 2 my %in = map { $_ => 1 } map { @$_ } @band;
174 2 my @lines;
175 2 for my $c (sort { $a <=> $b } keys %in) {
176 my $sixels = join '', map {
177 6 my $x = $_;
178 15 my $bits = 0;
179 15 ▶ $band[$_][$x] == $c and $bits |= 1 << $_ for 0 .. $#band;
Branch: 1 branch
BranchTrueFalseTotal
$band[$_][$x] == $c and $bits |= 1 << $_ 11 24 35
180 15 chr(63 + $bits);
181 } 0 .. $width - 1;
182 6 $sixels =~ s/((.)\2{3,})/'!' . length($1) . $2/ge;
183 6 push @lines, "#$c$sixels";
184 }
185 2 $out .= join('$', @lines) . '-';
186 }
187 2 return "$out\e\\\n";
188 }
189  
190 # Any terminal with 24-bit colors: a cell is the upper half block (▀), its upper pixel in the color of
191 # the text and its lower pixel in the color of the background. A pixel a column.
192 class Acme::PrettyCure::Image::Terminal::HalfBlock :isa(Acme::PrettyCure::Image::Terminal::Drawer);
193  
194 1 ▶method pixels_per_column { 1 }
Subroutine pixels_per_column: called CC=1 SCAR=0.0
195  
196 1 ▶method picture ($width, $height, $rows) {
Subroutine picture: called CC=5 SCAR=16.1
197 1 my $out = '';
198 1 ▶ for (my $y = 0 ; $y < $height ; $y += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $height) 2 1 3
199 2 my @upper = unpack 'C*', $rows->[$y];
200 2 ▶ my @lower = $y + 1 < $height ? unpack('C*', $rows->[ $y + 1 ]) : ();
Branch: 1 branch
BranchTrueFalseTotal
$y + 1 < $height ? : 1 1 2
201 2 for my $x (0 .. $width - 1) {
202 4 my @u = @upper[ $x * 3 .. $x * 3 + 2 ];
203 4 ▶ my $bg =
Branch: 1 branch
BranchTrueFalseTotal
@lower ? : 2 2 4
204 @lower ? sprintf("\e[48;2;%d;%d;%dm", @lower[ $x * 3 .. $x * 3 + 2 ]) : "\e[49m";
205 4 $out .= sprintf("\e[38;2;%d;%d;%dm", @u) . $bg . '▀';
206 }
207 2 $out .= "\e[0m\n";
208 }
209 1 return $out;
210 }
211  
212 1 1;
213  
214 __END__
↑ Summary PrettyCure/Image.pm »