Skip to content

fix(tui): keep kitty image redraws inside TUI#4261

Merged
badlogic merged 2 commits into
mainfrom
kitty-image-ids
May 7, 2026
Merged

fix(tui): keep kitty image redraws inside TUI#4261
badlogic merged 2 commits into
mainfrom
kitty-image-ids

Conversation

@mitsuhiko

Copy link
Copy Markdown
Member

Kitty images are not normal text, so clearing a TUI line does not necessarily remove the old graphic. I have a repro extension which forces image rendering corruption by drawing inside a reduced scroll region: Kitty moved the terminal cursor after placing the image, while the TUI also thought it owned cursor positioning, so later redraws happened from the wrong place and stale image placements remained visible.

This change keeps cursor movement under TUI control by asking Kitty not to move the cursor after placement (C=1) and then manually restoring the cursor to where the TUI expects it. Additionally it always assigns Kitty images an id, remembers ids from the previous render, and deletes old placements before redrawing changed or full frames. I did not found a way to do this without ids to remove the graphic.

This also takes out the cmux image disabling because I verified that this now works with cmux too.

Fixes #4208.

@badlogic badlogic added the inprogress Issue is being worked on label May 7, 2026
Comment thread packages/tui/src/tui.ts

const KITTY_SEQUENCE_PATTERN = /\x1b_G([^;\x1b]*);/g;

function extractKittyImageIds(line: string): number[] {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will scan the full base64 image payload. not ideal, but probably better than api breaking alternatives, e.g. Tui becoming an image (id) registry, and Image requiring a refernce to tui to allocate/delete image ids.

@badlogic badlogic merged commit 2c28e2e into main May 7, 2026
1 check passed
@badlogic

badlogic commented May 7, 2026

Copy link
Copy Markdown
Collaborator

Follow-up: I replaced the Kitty image ID scan with a bounded parser. It now reads only the first Kitty sequence header on a rendered line to extract i=... instead of using a global regex that can continue scanning through the base64 payload.

Validation:

  • cd packages/tui && node --test --import tsx test/terminal-image.test.ts test/tui-render.test.ts
  • npm run check

larsboes pushed a commit to larsboes/pi-mono that referenced this pull request May 11, 2026
fix(tui): keep kitty image redraws inside TUI
larsboes pushed a commit to larsboes/pi-mono that referenced this pull request May 17, 2026
fix(tui): keep kitty image redraws inside TUI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

inprogress Issue is being worked on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inline image previews corrupt terminal rendering in cmux/Ghostty

2 participants