blib/lib/Text/ANSI/Image/kitty.pm

filter: Statementstmt 100% 68 / 68 Branchbran 91.6% 22 / 24 Conditioncond 66.6% 10 / 15 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 11 / 11 Podpod 83.3% 5 / 6 totaltotal 90.1% 119 / 132 CC 23 SCAR 17.0
CC
23
Cov
93%
flush
9.0
supported
6.1
version_ge
6.0
CRAP 5.5
« blib/lib/App/ansipic.pm ↑ Summary script/ansipic »
1 package Text::ANSI::Image::kitty;
2  
3 # The Unicode placeholders of the kitty graphics protocol (kitty,
4 # Ghostty, iTerm2 3.7.3 and later). An image is shown by two things:
5 #
6 # - rows of the placeholder character U+10EEEE in the text, one for
7 # a cell, with the number of the row and the column as combining
8 # characters and the id of the image as the foreground color;
9 # - its data, sent once to the terminal itself by flush, as an APC
10 # sequence which makes a "virtual placement" of the image (U=1).
11 #
12 # The rows are plain text with a color, so they are laid out as any
13 # text; less shows them with LESSUTFCHARDEF=10EEEE:p.
14  
15 18 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 18 ▶use parent 'Text::ANSI::Image';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 18 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # The combining characters for the row and column numbers, from 0, as
22 # the kitty protocol defines them: those of UnicodeData.txt 6.0.0 with
23 # "Mn;230;NSM;;" (above the base, no decomposition), but 0300-0304,
24 # 0306-030C, 030F, 0311, 0313, 0314, 0342, 0653 and 0654, which may be
25 # fused with the base by normalization. Written out, so that a perl
26 # with other Unicode data gives the same numbers.
27 18 my @DIACRITICS = map { chr hex } qw(
28 0305 030D 030E 0310 0312 033D 033E 033F 0346 034A 034B 034C
29 0350 0351 0352 0357 035B 0363 0364 0365 0366 0367 0368 0369
30 036A 036B 036C 036D 036E 036F 0483 0484 0485 0486 0487 0592
31 0593 0594 0595 0597 0598 0599 059C 059D 059E 059F 05A0 05A1
32 05A8 05A9 05AB 05AC 05AF 05C4 0610 0611 0612 0613 0614 0615
33 0616 0617 0657 0658 0659 065A 065B 065D 065E 06D6 06D7 06D8
34 06D9 06DA 06DB 06DC 06DF 06E0 06E1 06E2 06E4 06E7 06E8 06EB
35 06EC 0730 0732 0733 0735 0736 073A 073D 073F 0740 0741 0743
36 0745 0747 0749 074A 07EB 07EC 07ED 07EE 07EF 07F0 07F1 07F3
37 0816 0817 0818 0819 081B 081C 081D 081E 081F 0820 0821 0822
38 0823 0825 0826 0827 0829 082A 082B 082C 082D 0951 0953 0954
39 0F82 0F83 0F86 0F87 135D 135E 135F 17DD 193A 1A17 1A75 1A76
40 1A77 1A78 1A79 1A7A 1A7B 1A7C 1B6B 1B6D 1B6E 1B6F 1B70 1B71
41 1B72 1B73 1CD0 1CD1 1CD2 1CDA 1CDB 1CE0 1DC0 1DC1 1DC3 1DC4
42 1DC5 1DC6 1DC7 1DC8 1DC9 1DCB 1DCC 1DD1 1DD2 1DD3 1DD4 1DD5
43 1DD6 1DD7 1DD8 1DD9 1DDA 1DDB 1DDC 1DDD 1DDE 1DDF 1DE0 1DE1
44 1DE2 1DE3 1DE4 1DE5 1DE6 1DFE 20D0 20D1 20D4 20D5 20D6 20D7
45 20DB 20DC 20E1 20E7 20E9 20F0 2CEF 2CF0 2CF1 2DE0 2DE1 2DE2
46 2DE3 2DE4 2DE5 2DE6 2DE7 2DE8 2DE9 2DEA 2DEB 2DEC 2DED 2DEE
47 2DEF 2DF0 2DF1 2DF2 2DF3 2DF4 2DF5 2DF6 2DF7 2DF8 2DF9 2DFA
48 2DFB 2DFC 2DFD 2DFE 2DFF A66F A67C A67D A6F0 A6F1 A8E0 A8E1
49 A8E2 A8E3 A8E4 A8E5 A8E6 A8E7 A8E8 A8E9 A8EA A8EB A8EC A8ED
50 A8EE A8EF A8F0 A8F1 AAB0 AAB2 AAB3 AAB7 AAB8 AABE AABF AAC1
51 FE20 FE21 FE22 FE23 FE24 FE25 FE26 10A0F 10A38 1D185 1D186 1D187
52 1D188 1D189 1D1AA 1D1AB 1D1AC 1D1AD 1D242 1D243 1D244
53 );
54  
55 18 my $PLACEHOLDER = "\x{10EEEE}";
56  
57 # The terminals: kitty (which sets no TERM_PROGRAM), Ghostty (cmux is
58 # one too), and iTerm2 3.7.3 or later. TERM_PROGRAM is inherited by a
59 # terminal started from another, so it can be wrong.
60 13 ▶sub supported {
Subroutine supported: called CC=6 SCAR=18.1
Pod documented
61 13 ▶ my $prog = $ENV{TERM_PROGRAM} // '';
Condition100%2/2
$ENV{"TERM_PROGRAM"} // ""
l!l
103
MC/DC100%1/1
$ENV{"TERM_PROGRAM"} // ""
$ENV{"TERM_PROGRAM"}
Truth table100%2/2
A
A= $ENV{"TERM_PROGRAM"}
Aresult
11
00
62 13 ▶ return version_ge($ENV{TERM_PROGRAM_VERSION} // '', '3.7.3') if $prog eq 'iTerm.app';
Branch: 1 branch
BranchTrueFalseTotal
if $prog eq "iTerm.app" 6 7 13
Condition50%1/2
$ENV{"TERM_PROGRAM_VERSION"} // ""
l!l
60
MC/DC0%0/1
$ENV{"TERM_PROGRAM_VERSION"} // ""
$ENV{"TERM_PROGRAM_VERSION"}
Truth table50%1/2
A
A= $ENV{"TERM_PROGRAM_VERSION"}
Aresult
11
00
63 7 ▶ return 1 if $prog eq 'ghostty';
Branch: 1 branch
BranchTrueFalseTotal
if $prog eq "ghostty" 2 5 7
64 5 ▶ ($ENV{TERM} // '') eq 'xterm-kitty';
Condition50%1/2
$ENV{"TERM"} // ""
l!l
50
MC/DC0%0/1
$ENV{"TERM"} // ""
$ENV{"TERM"}
Truth table50%1/2
A
A= $ENV{"TERM"}
Aresult
11
00
65 }
66  
67 # The numbers of a version as far as they go: 3.6.0beta2 is 3.6.0
68 6 ▶sub version_ge {
Subroutine version_ge: called CC=6 SCAR=17.9
Pod undocumented
69 6 my($v, $min) = @_;
70 6 ▶ my @a = ($v =~ /\A([\d.]*)/)[0] =~ /(\d+)/g or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my(@a) = ($v =~ /\A([\d.]*)/u)[0] =~ /(\d+)/gu or return 0 5 1 6
71 5 my @b = split /\./, $min;
72 5 for my $i (0 .. $#b) {
73 13 ▶ my $x = $a[$i] // 0;
Condition50%1/2
$a[$i] // 0
l!l
130
MC/DC0%0/1
$a[$i] // 0
$a[$i]
Truth table50%1/2
A
A= $a[$i]
Aresult
11
00
74 13 ▶ return 1 if $x > $b[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $x > $b[$i] 2 11 13
75 11 ▶ return 0 if $x < $b[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $x < $b[$i] 2 9 11
76 }
77 1 1;
78 }
79  
80 # A row or a column is a combining character
81 32 ▶sub max_cells { scalar @DIACRITICS }
Subroutine max_cells: called CC=1 SCAR=0.0
Pod documented
82  
83 18 my $serial = 0;
84  
85 26 ▶sub render {
Subroutine render: called CC=4 SCAR=13.9
Pod documented
86 26 my($self, $file, $w, $h, $type, $cols, $rows) = @_;
87 26 ▶ my $cw = ($self->terminal)[2] || $Text::ANSI::Image::CELL[0];
Condition33%1/3
($self->terminal)[2] || $Text::ANSI::Image::CELL[0]
l!l&&r!l&&!r
2600
MC/DC0%0/2
($self->terminal)[2] || $Text::ANSI::Image::CELL[0]
($self->terminal)[2] $Text::ANSI::Image::CELL[0]
Truth table33%1/3
A || B
A= ($self->terminal)[2]B= $Text::ANSI::Image::CELL[0]
ABresult
1X1
011
000
88 26 ▶ my $png = $self->png($file, $w, $type, $cols * $cw * 2) // return;
Condition100%2/2
$self->png($file, $w, $type, $cols * $cw * 2) // (return)
l!l
251
MC/DC100%1/1
$self->png($file, $w, $type, $cols * $cw * 2) // (return)
$self->png($file, $w, $type, $cols * $cw * 2)
Truth table100%2/2
A
A= $self->png($file, $w, $type, $cols * $cw * 2)
Aresult
11
00
89 # an id of 24 bits that another program is not likely to use
90 25 my $id = ((($$ & 0xffff) << 8) | (++$serial & 0xff)) | 0x010000;
91 25 push @{$self->{_queue}}, [ $id, $cols, $rows, $png ];
92 25 my $color = sprintf "\e[38;2;%d;%d;%dm", ($id >> 16) & 255, ($id >> 8) & 255, $id & 255;
93 25 ▶ my $osc = $self->{osc} ? osc($id, $cols, $rows, $file) : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'osc'} ? : 2 23 25
94 map {
95 25 my $r = $_;
96 49 $osc . $color . join('', map { $PLACEHOLDER . $DIACRITICS[$r] . $DIACRITICS[$_] } 0 .. $cols - 1) . "\e[39m";
97 } 0 .. $rows - 1;
98 }
99  
100 # A private OSC, which terminals ignore, with the id, the size in cells
101 # and the file of an image, for a program which draws the image itself
102 # where its cells are, as ansi2pdf. On every row, so that a part of
103 # the rows still has it. None for a file name with a control
104 # character, which would end the sequence.
105 18 my $OSC = 7771;
106 2 ▶sub osc {
Subroutine osc: called CC=2 SCAR=6.9
Pod documented
107 2 my($id, $cols, $rows, $file) = @_;
108 2 my $path = File::Spec->rel2abs($file);
109 2 ▶ return '' if $path =~ /[\x00-\x1f\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /[\x00-\x1f\x7f]/u 1 1 2
110 1 "\e]$OSC;i=$id,c=$cols,r=$rows;$path\e\\";
111 }
112  
113 # Send the images to the terminal, in chunks of 4096 bytes of base64,
114 # and forget them
115 20 ▶sub flush {
Subroutine flush: called CC=9 SCAR=22.0
Pod documented
116 20 my $self = shift;
117 20 ▶ my @q = @{delete $self->{_queue} // []} or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@q) = @{delete $self->{"_queue"} // [];} or return 18 2 20
Condition100%2/2
delete $self->{"_queue"} // []
l!l
182
MC/DC100%1/1
delete $self->{"_queue"} // []
delete $self->{"_queue"}
Truth table100%2/2
A
A= delete $self->{"_queue"}
Aresult
11
00
118 18 ▶ if (open my $tty, '>>:raw', $self->{tty}) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $tty, ">>:raw", $self->{"tty"}) 18 0 18
119 18 for my $q (@q) {
120 18 my($id, $cols, $rows, $png) = @$q;
121 18 ▶ open my $fh, '<:raw', $png or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $png or next 18 0 18
122 18 my $b64 = encode_base64(do { local $/; <$fh> }, '');
123 18 my @chunk = unpack '(A4096)*', $b64;
124 18 my $out = '';
125 18 for my $i (0 .. $#chunk) {
126 20 ▶ my $more = $i < $#chunk ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$i < $#chunk ? : 2 18 20
127 20 ▶ my $key = $i == 0 ? "a=T,U=1,i=$id,f=100,q=2,c=$cols,r=$rows,m=$more" : "q=2,m=$more";
Branch: 1 branch
BranchTrueFalseTotal
$i == 0 ? : 18 2 20
128 20 $out .= "\e_G$key;$chunk[$i]\e\\";
129 }
130 18 print $tty $out;
131 }
132 18 close $tty;
133 }
134 18 $self->temp_done;
135 }
136  
137 18 1;
138  
139 __END__
« blib/lib/App/ansipic.pm ↑ Summary script/ansipic »