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>
82 lines
2.1 KiB
Go
82 lines
2.1 KiB
Go
// Package imgview draws preview images next to the Bubble Tea UI (design
|
|
// §5.5) without side effects in View: an actor goroutine takes the latest
|
|
// desired state, decodes and scales off the UI goroutine and writes kitty
|
|
// graphics (or drives ueberzugpp) through a SyncTTY shared with the
|
|
// renderer, so escape sequences never interleave with a frame.
|
|
package imgview
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"sync"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// SyncTTY serialises writes to the terminal. Bubble Tea writes each frame
|
|
// with one Write (tea.WithOutput(tty)); image blocks go through WriteBlock
|
|
// under the same mutex. It implements term.File (Read, Write, Close, Fd).
|
|
type SyncTTY struct {
|
|
mu sync.Mutex
|
|
w io.Writer
|
|
f *os.File
|
|
}
|
|
|
|
// NewSyncTTY wraps the terminal file.
|
|
func NewSyncTTY(f *os.File) *SyncTTY { return &SyncTTY{w: f, f: f} }
|
|
|
|
// NewSyncWriter wraps any writer (tests, non-terminal output).
|
|
func NewSyncWriter(w io.Writer) *SyncTTY { return &SyncTTY{w: w} }
|
|
|
|
func (t *SyncTTY) Write(p []byte) (int, error) {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
return t.w.Write(p)
|
|
}
|
|
|
|
// WriteBlock writes several pieces atomically with respect to frames.
|
|
func (t *SyncTTY) WriteBlock(parts ...[]byte) error {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
for _, p := range parts {
|
|
if _, err := t.w.Write(p); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (t *SyncTTY) Read(p []byte) (int, error) {
|
|
if t.f == nil {
|
|
return 0, io.EOF
|
|
}
|
|
return t.f.Read(p)
|
|
}
|
|
|
|
func (t *SyncTTY) Close() error {
|
|
if t.f == nil {
|
|
return nil
|
|
}
|
|
return t.f.Close()
|
|
}
|
|
|
|
// Fd returns the terminal descriptor (^0 when not a file).
|
|
func (t *SyncTTY) Fd() uintptr {
|
|
if t.f == nil {
|
|
return ^uintptr(0)
|
|
}
|
|
return t.f.Fd()
|
|
}
|
|
|
|
// CellSize returns the pixel size of a character cell, if the terminal
|
|
// reports it (TIOCGWINSZ ws_xpixel/ws_ypixel).
|
|
func (t *SyncTTY) CellSize() (w, h int, ok bool) {
|
|
if t.f == nil {
|
|
return 0, 0, false
|
|
}
|
|
ws, err := unix.IoctlGetWinsize(int(t.f.Fd()), unix.TIOCGWINSZ)
|
|
if err != nil || ws.Col == 0 || ws.Row == 0 || ws.Xpixel == 0 || ws.Ypixel == 0 {
|
|
return 0, 0, false
|
|
}
|
|
return int(ws.Xpixel) / int(ws.Col), int(ws.Ypixel) / int(ws.Row), true
|
|
}
|