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