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>
277 lines
5.8 KiB
Go
277 lines
5.8 KiB
Go
package imgview
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"image"
|
||
"os/exec"
|
||
"strings"
|
||
"sync"
|
||
|
||
"vcom/internal/preview"
|
||
)
|
||
|
||
// Backend selects how images are drawn.
|
||
type Backend uint8
|
||
|
||
const (
|
||
None Backend = iota
|
||
Kitty
|
||
Ueberzug
|
||
)
|
||
|
||
func (b Backend) String() string { return [...]string{"none", "kitty", "ueberzugpp"}[b] }
|
||
|
||
// Rect is a cell rectangle (0-based column/row).
|
||
type Rect struct{ X, Y, W, H int }
|
||
|
||
// Desired is what the UI wants on screen; the actor converges to the latest.
|
||
type Desired struct {
|
||
Visible bool
|
||
Image preview.ImageRef
|
||
Rect Rect
|
||
Gen uint64
|
||
}
|
||
|
||
func (d Desired) same(o Desired) bool {
|
||
if d.Visible != o.Visible {
|
||
return false
|
||
}
|
||
if !d.Visible {
|
||
return true
|
||
}
|
||
return d.Rect == o.Rect && d.Image.Local == o.Image.Local && d.Image.Size == o.Image.Size &&
|
||
d.Image.ModTime.Equal(o.Image.ModTime)
|
||
}
|
||
|
||
// Event reports a problem to the UI (shown as a notification).
|
||
type Event struct {
|
||
Err error
|
||
Disabled bool // the backend was switched off (e.g. stuck helper)
|
||
}
|
||
|
||
// Options configure Start.
|
||
type Options struct {
|
||
Backend Backend
|
||
TTY *SyncTTY
|
||
// CellW, CellH: pixel size of a cell; 0 → asked from the TTY, else 8×16.
|
||
CellW, CellH int
|
||
// TmpDir enables kitty's file transmission (local terminal only); "" →
|
||
// direct transmission.
|
||
TmpDir string
|
||
Tmux bool
|
||
// Ueberzug: the helper command (default "ueberzugpp layer --silent").
|
||
Ueberzug []string
|
||
OnEvent func(Event) // called from the actor goroutine
|
||
}
|
||
|
||
// Actor owns the image output.
|
||
type Actor struct {
|
||
o Options
|
||
mu sync.Mutex
|
||
want Desired
|
||
dirty bool
|
||
signal chan struct{}
|
||
done chan struct{}
|
||
quit chan struct{}
|
||
once sync.Once
|
||
|
||
backend Backend
|
||
kitty *kitty
|
||
uz *ueberzug
|
||
shown Desired
|
||
}
|
||
|
||
// Start launches the actor goroutine. A backend that cannot start falls
|
||
// back to None with an Event.
|
||
func Start(o Options) *Actor {
|
||
a := &Actor{o: o, signal: make(chan struct{}, 1), done: make(chan struct{}), quit: make(chan struct{}), backend: o.Backend}
|
||
if a.o.CellW <= 0 || a.o.CellH <= 0 {
|
||
a.o.CellW, a.o.CellH = 8, 16
|
||
if o.TTY != nil {
|
||
if w, h, ok := o.TTY.CellSize(); ok {
|
||
a.o.CellW, a.o.CellH = w, h
|
||
}
|
||
}
|
||
}
|
||
switch o.Backend {
|
||
case Kitty:
|
||
a.kitty = &kitty{tty: o.TTY, tmpDir: o.TmpDir, tmux: o.Tmux}
|
||
case Ueberzug:
|
||
cmd := o.Ueberzug
|
||
if len(cmd) == 0 {
|
||
cmd = []string{"ueberzugpp", "layer", "--silent"}
|
||
}
|
||
uz, err := startUeberzug(cmd[0], cmd[1:]...)
|
||
if err != nil {
|
||
a.backend = None
|
||
a.event(Event{Err: err, Disabled: true})
|
||
} else {
|
||
a.uz = uz
|
||
}
|
||
}
|
||
go a.loop()
|
||
return a
|
||
}
|
||
|
||
// Backend reports the backend in use (None after a failure).
|
||
func (a *Actor) Backend() Backend {
|
||
a.mu.Lock()
|
||
defer a.mu.Unlock()
|
||
return a.backend
|
||
}
|
||
|
||
// Apply records the desired state and returns at once; only the latest
|
||
// state is drawn.
|
||
func (a *Actor) Apply(d Desired) {
|
||
a.mu.Lock()
|
||
a.want, a.dirty = d, true
|
||
a.mu.Unlock()
|
||
select {
|
||
case a.signal <- struct{}{}:
|
||
default:
|
||
}
|
||
}
|
||
|
||
// Stop hides the image, kills ueberzugpp, removes temp files and waits for
|
||
// the actor goroutine.
|
||
func (a *Actor) Stop() {
|
||
a.once.Do(func() { close(a.quit) })
|
||
<-a.done
|
||
}
|
||
|
||
func (a *Actor) event(e Event) {
|
||
if a.o.OnEvent != nil {
|
||
a.o.OnEvent(e)
|
||
}
|
||
}
|
||
|
||
func (a *Actor) take() (Desired, bool) {
|
||
a.mu.Lock()
|
||
defer a.mu.Unlock()
|
||
d, ok := a.want, a.dirty
|
||
a.dirty = false
|
||
return d, ok
|
||
}
|
||
|
||
// stale reports whether a newer desired state has arrived.
|
||
func (a *Actor) stale() bool {
|
||
a.mu.Lock()
|
||
defer a.mu.Unlock()
|
||
return a.dirty
|
||
}
|
||
|
||
func (a *Actor) loop() {
|
||
defer close(a.done)
|
||
defer a.teardown()
|
||
for {
|
||
select {
|
||
case <-a.quit:
|
||
return
|
||
case <-a.signal:
|
||
}
|
||
d, ok := a.take()
|
||
if !ok || d.same(a.shown) {
|
||
continue
|
||
}
|
||
a.draw(d)
|
||
}
|
||
}
|
||
|
||
func (a *Actor) draw(d Desired) {
|
||
if a.Backend() == None {
|
||
return
|
||
}
|
||
var err error
|
||
switch {
|
||
case !d.Visible || d.Image.Local == "":
|
||
err = a.hide()
|
||
d.Visible = d.Visible && d.Image.Local != ""
|
||
case a.kitty != nil:
|
||
var img *image.RGBA
|
||
img, err = loadScaled(context.Background(), d.Image.Local, d.Rect.W*a.o.CellW, d.Rect.H*a.o.CellH)
|
||
if err == nil && a.stale() {
|
||
return // superseded while decoding: draw the newer state instead
|
||
}
|
||
if err == nil {
|
||
err = a.kitty.show(img, d.Rect)
|
||
}
|
||
case a.uz != nil:
|
||
if err = checkSize(d.Image.Local); err == nil {
|
||
err = a.uz.show(d.Image.Local, d.Rect)
|
||
}
|
||
}
|
||
if err != nil {
|
||
disabled := errors.Is(err, ErrStuck)
|
||
if disabled {
|
||
a.disable()
|
||
}
|
||
a.event(Event{Err: err, Disabled: disabled})
|
||
a.shown = Desired{}
|
||
return
|
||
}
|
||
a.shown = d
|
||
}
|
||
|
||
func (a *Actor) hide() error {
|
||
switch {
|
||
case a.kitty != nil:
|
||
return a.kitty.hide()
|
||
case a.uz != nil:
|
||
return a.uz.hide()
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// disable kills a stuck helper and switches to None.
|
||
func (a *Actor) disable() {
|
||
if a.uz != nil {
|
||
a.uz.stop()
|
||
a.uz = nil
|
||
}
|
||
a.mu.Lock()
|
||
a.backend = None
|
||
a.mu.Unlock()
|
||
}
|
||
|
||
func (a *Actor) teardown() {
|
||
if a.kitty != nil {
|
||
a.kitty.stop()
|
||
}
|
||
if a.uz != nil {
|
||
_ = a.uz.hide()
|
||
a.uz.stop()
|
||
}
|
||
}
|
||
|
||
// checkSize refuses huge images for ueberzugpp too (it would decode them).
|
||
func checkSize(path string) error {
|
||
_, err := imageConfig(path)
|
||
return err
|
||
}
|
||
|
||
// Detect picks the backend for preview.image_backend (auto | kitty |
|
||
// ueberzugpp | none). auto: kitty in kitty, WezTerm or ghostty outside tmux,
|
||
// else ueberzugpp if installed, else none. An explicit "kitty" works in tmux
|
||
// (passthrough).
|
||
func Detect(setting string, getenv func(string) string) Backend {
|
||
inTmux := getenv("TMUX") != ""
|
||
switch strings.ToLower(setting) {
|
||
case "none":
|
||
return None
|
||
case "kitty":
|
||
return Kitty
|
||
case "ueberzugpp", "ueberzug":
|
||
return Ueberzug
|
||
}
|
||
term, prog := getenv("TERM"), getenv("TERM_PROGRAM")
|
||
kittyLike := term == "xterm-kitty" || getenv("KITTY_WINDOW_ID") != "" ||
|
||
prog == "WezTerm" || prog == "ghostty" || term == "xterm-ghostty"
|
||
if kittyLike && !inTmux {
|
||
return Kitty
|
||
}
|
||
if _, err := exec.LookPath("ueberzugpp"); err == nil {
|
||
return Ueberzug
|
||
}
|
||
return None
|
||
}
|