Adds golang.org/x/image v0.39.0 (draw, webp; vendored; x/sys and x/text unchanged). SyncTTY serialises renderer frames and image blocks under one mutex. The actor keeps only the latest Desired state (100 Apply → ≤ 2 decodes), refuses images above 50 Mpx from the header alone, scales with ApproxBiLinear and writes kitty graphics as one ESC7/CUP/APC/ESC8 block (raw RGBA, chunked or via a temp file, tmux passthrough). ueberzugpp gets JSON lines through a pipe with a 200 ms write deadline; a helper that stops reading is killed with its process group and the backend falls back to none with an event. Stop removes the placement, temp files and processes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
2.6 KiB
Go
105 lines
2.6 KiB
Go
package imgview
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
"image"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// kittyImageID identifies vcom's single placement.
|
|
const kittyImageID = 31337
|
|
|
|
// kitty draws with the kitty graphics protocol. Each draw is one block
|
|
// "ESC 7, CUP, APC…, ESC 8" under the SyncTTY mutex: the cursor does not
|
|
// move and the renderer is not disturbed. With a local terminal the pixels
|
|
// go through a temp file (t=t), avoiding base64 of megabytes.
|
|
type kitty struct {
|
|
tty *SyncTTY
|
|
tmpDir string // "" → direct transmission
|
|
tmux bool
|
|
shown bool
|
|
}
|
|
|
|
// kittyBlock builds the escape sequences for img at rect.
|
|
func (k *kitty) kittyBlock(img *image.RGBA, r Rect) ([]byte, error) {
|
|
w, h := img.Rect.Dx(), img.Rect.Dy()
|
|
ctrl := fmt.Sprintf("a=T,f=32,s=%d,v=%d,i=%d,c=%d,r=%d,C=1,q=2", w, h, kittyImageID, r.W, r.H)
|
|
var b strings.Builder
|
|
b.WriteString("\x1b7")
|
|
fmt.Fprintf(&b, "\x1b[%d;%dH", r.Y+1, r.X+1)
|
|
if k.tmpDir != "" {
|
|
f, err := os.CreateTemp(k.tmpDir, "tty-graphics-protocol-*.rgba")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_, err = f.Write(img.Pix)
|
|
if cerr := f.Close(); err == nil {
|
|
err = cerr
|
|
}
|
|
if err != nil {
|
|
os.Remove(f.Name())
|
|
return nil, err
|
|
}
|
|
// t=t: the terminal reads and deletes the file itself.
|
|
b.WriteString(k.apc(ctrl+",t=t", base64.StdEncoding.EncodeToString([]byte(f.Name()))))
|
|
} else {
|
|
payload := base64.StdEncoding.EncodeToString(img.Pix)
|
|
const chunk = 4096
|
|
for i := 0; i < len(payload); i += chunk {
|
|
end := min(i+chunk, len(payload))
|
|
more := 0
|
|
if end < len(payload) {
|
|
more = 1
|
|
}
|
|
c := fmt.Sprintf("m=%d", more)
|
|
if i == 0 {
|
|
c = ctrl + "," + c
|
|
}
|
|
b.WriteString(k.apc(c, payload[i:end]))
|
|
}
|
|
}
|
|
b.WriteString("\x1b8")
|
|
return []byte(b.String()), nil
|
|
}
|
|
|
|
// apc wraps one graphics command (in tmux passthrough if needed).
|
|
func (k *kitty) apc(ctrl, payload string) string {
|
|
s := "\x1b_G" + ctrl + ";" + payload + "\x1b\\"
|
|
if k.tmux {
|
|
s = "\x1bPtmux;" + strings.ReplaceAll(s, "\x1b", "\x1b\x1b") + "\x1b\\"
|
|
}
|
|
return s
|
|
}
|
|
|
|
func (k *kitty) show(img *image.RGBA, r Rect) error {
|
|
block, err := k.kittyBlock(img, r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := k.tty.WriteBlock([]byte(k.apc(fmt.Sprintf("a=d,d=I,i=%d,q=2", kittyImageID), "")), block); err != nil {
|
|
return err
|
|
}
|
|
k.shown = true
|
|
return nil
|
|
}
|
|
|
|
func (k *kitty) hide() error {
|
|
if !k.shown {
|
|
return nil
|
|
}
|
|
k.shown = false
|
|
return k.tty.WriteBlock([]byte(k.apc(fmt.Sprintf("a=d,d=I,i=%d,q=2", kittyImageID), "")))
|
|
}
|
|
|
|
func (k *kitty) stop() {
|
|
_ = k.hide()
|
|
if k.tmpDir != "" {
|
|
files, _ := filepath.Glob(filepath.Join(k.tmpDir, "tty-graphics-protocol-*"))
|
|
for _, f := range files {
|
|
os.Remove(f)
|
|
}
|
|
}
|
|
}
|