Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .changeset/img-element.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---

@bombshell-cooper bombshell-cooper Bot Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changeset needs revision.

Changeset package scope does not match the affected packages confidently.

View the proposed replacement
---
'@bomb.sh/tty': minor
---

Adds the `img()` directive for declarative image rendering in terminal UIs.

The renderer walks a fidelity ladder — kitty graphics protocol, shape-aware ASCII character art, or alt text — resolved from `RuntimeCapabilities.kittyGraphics` evidence. Images are registered as caller-owned RGBA8 buffers via `setImage` / `removeImage`; transmission is lazy and re-transmission only occurs on data change, so steady-state renders are byte-silent.

```ts
term.setImage(1, { width: 128, height: 64, pixels: rgba8 });
term.removeImage(1); // returns deletion bytes to write immediately

term.render([img("cover", { image: 1, alt: "cover art" })]);
```

`createTerm` accepts an `imagePoolBytes` option (default 4 MiB) that sizes the fixed pixel pool. `update()` emits cleanup bytes immediately when a capability denial or resize invalidates live placements. Line mode caps the ladder at `ascii`. Errors surface as `IMAGE_NOT_FOUND`, `IMAGE_PLACEMENTS_EXCEEDED`, and `IMAGE_PLACEMENT_COLLISION`.

#### Migration

`@bomb.sh/tty` is pre-1.0; this minor bump is a breaking change to the `Term` interface — `setImage` and `removeImage` are new required methods. Code that constructs a `Term` directly (rather than via `createTerm` or `validated`) must add both methods:

```diff
  const myTerm: Term = {
    render(ops) { … },
    update(events) { … },
+   setImage(id, data) { … },
+   removeImage(id) { … },
    get capabilities() { … },
  };
```

"@bomb.sh/tty": minor
---

Adds the `img()` directive — declarative images rendered through a fidelity ladder: kitty graphics protocol pixels, shape-aware ASCII character art, or alt text, resolved from capability evidence (`RuntimeCapabilities.kittyGraphics`).

Surfaces are caller-owned decoded RGBA buffers managed by the Term:

```ts
term.setImage(1, { width: 128, height: 64, pixels: rgba8 });
term.render([img("cover", { image: 1, alt: "cover art" })]);
term.removeImage(1); // the next render deletes it from the terminal
```

`createTerm` takes an `imagePoolBytes` option (default 4 MiB) sizing the fixed pixel pool. Transmission is lazy; re-transmission only on data change; steady-state renders are byte-silent. Like `update()`, `removeImage()` returns nothing: cleanup after removal, capability denial, or resize is emitted at the start of the next render. `bg` accepts any color value `open({ bg })` does. Line mode caps the ladder at ascii. Errors surface as `IMAGE_NOT_FOUND`, `IMAGE_PLACEMENTS_EXCEEDED`, and `IMAGE_PLACEMENT_COLLISION`.
26 changes: 26 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ What it shows:
feature indicator (green ● supported, red ■ not) pinned to the right edge
- a final frame on exit that restores the default pointer (§7.9 restore)

## Images

Path: `examples/img/index.ts`

Run it with:

```sh
deno run examples/img/index.ts
# or
node examples/img/index.ts
```

What it shows:

- one generated image (shaded sphere, alpha checker, or animated plasma) in four
panels: the auto tier ladder, kitty forced, ascii forced, and alt text
(graphics-spec §9)
- kitty graphics pixels in terminals that answer the graphics probe (kitty,
ghostty, WezTerm); shape-aware ascii art everywhere else
- a status line counting each frame's transmissions, placements, and deletions:
denying the capability (`k`) or removing an image (`x`) emits nothing until
the next frame, which opens with the queued deletions (§5.7)
- the plasma re-set through `setImage()` every tick, re-transmitting and
re-placing each revision
- a final frame on exit that deletes every image from the terminal

## Keyboard

Path: `examples/keyboard/index.ts`
Expand Down
132 changes: 132 additions & 0 deletions examples/img/images.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Generated in-process: the renderer consumes decoded RGBA only
// (graphics-spec INV-I1), so the demo performs no IO.
//
// sphere smooth gradients and a specular highlight
// checker saturated hues with round alpha holes
// plasma re-set through setImage() every tick (graphics-spec §4.4)

export interface Image {
width: number;
height: number;
pixels: Uint8Array; // RGBA8, straight alpha
}

export type ImageId = 1 | 2 | 3;

export function sphere(size = 64): Image {
let pixels = new Uint8Array(size * size * 4);
let light = norm3(-0.45, -0.6, 0.66);
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
let u = ((x + 0.5) / size) * 2 - 1;
let v = ((y + 0.5) / size) * 2 - 1;
let i = (y * size + x) * 4;
if (u * u + v * v > 1) {
drawBackdrop(pixels, i, x, y);
continue;
}
let z = Math.sqrt(1 - u * u - v * v);
let diff = Math.max(0, u * light[0] + v * light[1] + z * light[2]);
let spec = Math.pow(diff, 48) * 0.9;
let rim = Math.pow(1 - z, 3) * 0.35;
let r = 60 * (0.25 + 0.75 * diff) + 255 * spec + 20 * rim;
let g = 120 * (0.25 + 0.75 * diff) + 255 * spec + 150 * rim;
let b = 220 * (0.25 + 0.75 * diff) + 255 * spec + 180 * rim;
pixels[i] = clamp8(r);
pixels[i + 1] = clamp8(g);
pixels[i + 2] = clamp8(b);
pixels[i + 3] = 255;
}
}
return { width: size, height: size, pixels };
}

export function checker(size = 64): Image {
let pixels = new Uint8Array(size * size * 4);
let hues = [
[226, 60, 60],
[60, 178, 72],
[66, 118, 230],
[240, 200, 48],
[184, 78, 204],
[48, 200, 210],
];
let cell = size / 8;
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
let i = (y * size + x) * 4;
let cx = Math.floor(x / cell);
let cy = Math.floor(y / cell);
let [r, g, b] = hues[(cx + cy * 3) % hues.length];
// every other diagonal cell carries a round alpha hole (soft edge)
let hole = (cx + cy) % 2 === 1;
let a = 255;
if (hole) {
let du = (x % cell) / cell - 0.5;
let dv = (y % cell) / cell - 0.5;
let d = Math.sqrt(du * du + dv * dv) * 2;
a = clamp8((0.72 - d) * 8);
a = Math.round(a * 2.55);
}
pixels[i] = r;
pixels[i + 1] = g;
pixels[i + 2] = b;
pixels[i + 3] = a;
}
}
return { width: size, height: size, pixels };
}

export function plasma(size = 64, t = 0): Image {
let pixels = new Uint8Array(size * size * 4);
// palette stops: deep blue → violet → orange → pale yellow
let stops = [
[12, 16, 48],
[90, 40, 160],
[230, 120, 40],
[255, 230, 140],
];
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
let i = (y * size + x) * 4;
let v = Math.sin(x * 0.11 + t) +
Math.sin(y * 0.13 + t * 1.31) +
Math.sin((x + y) * 0.09 + t * 0.77) +
Math.sin(Math.sqrt(x * x + y * y) * 0.14 - t * 0.53);
let f = (v + 4) / 8; // [0, 1)
let seg = Math.min(2.999, f * 3);
let k = Math.floor(seg);
let mix = seg - k;
let [r0, g0, b0] = stops[k];
let [r1, g1, b1] = stops[k + 1];
pixels[i] = clamp8(r0 + (r1 - r0) * mix);
pixels[i + 1] = clamp8(g0 + (g1 - g0) * mix);
pixels[i + 2] = clamp8(b0 + (b1 - b0) * mix);
pixels[i + 3] = 255;
}
}
return { width: size, height: size, pixels };
}

function drawBackdrop(
pixels: Uint8Array,
i: number,
x: number,
y: number,
): void {
// near-black with a faint grid so the sphere's silhouette reads
let grid = x % 8 === 0 || y % 8 === 0;
pixels[i] = grid ? 62 : 44;
pixels[i + 1] = grid ? 62 : 44;
pixels[i + 2] = grid ? 76 : 52;
pixels[i + 3] = 255;
}

function clamp8(v: number): number {
return v < 0 ? 0 : v > 255 ? 255 : Math.round(v);
}

function norm3(x: number, y: number, z: number): [number, number, number] {
let n = Math.sqrt(x * x + y * y + z * z);
return [x / n, y / n, z / n];
}
Loading
Loading