Skip to content

Render images inline where the terminal supports it #1

Description

@viict

Markdown documents reference images, and tread currently shows the alt text
and nothing else. In a docs corpus with diagrams that is exactly where the
reader gives up and you go open the file somewhere else.

The cheap part

The two widely-supported inline-image protocols accept encoded image data and
decode it themselves:

  • kitty graphics protocol — takes PNG directly (f=100). kitty, Ghostty,
    WezTerm, recent Konsole.
  • iTerm2OSC 1337 File=, takes PNG/JPEG/GIF. iTerm2, WezTerm.

So tread can read the file, base64 it (already hand-written for OSC 52) and
hand the bytes over. No image decoder, and therefore no dependencies — the
terminal does the work.

The expensive part, deliberately out of scope for a first pass

  • Sixel (xterm, foot, Windows Terminal) needs us to decode and quantise
    ourselves — a PNG decoder, which means writing inflate first.
  • A block-character fallback (what chafa does) needs the same decoder.

Terminals without kitty/iTerm2 support keep showing alt text. That is an honest
degradation; a wrong-looking picture would be worse than none.

The actual work

Not the protocol — the pager. Images have to be placed, cleared and re-placed as
the viewport scrolls, and tread repaints whole frames. kitty addresses this
with placement ids that can be deleted and re-issued; it is tractable, but it is
where the time goes.

Also to settle:

  • Detecting support. TERM/TERM_PROGRAM sniffing is unreliable; querying the
    terminal and waiting for a reply is correct but needs a timeout.
  • Reserving rows in the layout for the image and letting the terminal scale into
    that box, so no image dimensions have to be parsed.
  • --plain, piped output and the non-interactive dump path must never emit
    image escapes.
  • Only local relative paths. tread does not fetch over the network and should
    not start here.

Acceptance

  • A local PNG referenced from a markdown document renders inline in kitty
  • The same in iTerm2
  • Alt text everywhere else, unchanged
  • Scrolling and resizing leave no orphaned images on screen
  • Nothing is emitted on the dump path
  • No new dependencies

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions