Skip to content

Commit 3841243

Browse files
authored
refactor!: createTerm({ terminfo }) takes a TerminalInfo
Match createInput: the renderer option is `terminfo`, typed `TerminalInfo`. Adds the first test that seeds createTerm from a detected TerminalInfo (previously untested; test/caps.ts helpers were unused). The stack changeset now covers only what this PR and #131 add — detectTerminal/TerminalInfo, createTerm's option, term.capabilities, and the update() signature change — since #132 carries its own changeset for the input side.
1 parent 3296571 commit 3841243

3 files changed

Lines changed: 34 additions & 23 deletions

File tree

‎.changeset/terminfo-capability-layer.md‎

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,25 @@
22
"@bomb.sh/tty": minor
33
---
44

5-
Adds `detectTerminal`, `Capabilities`, `Detection`, `CapabilityEvent`, and `KeyTable` to the public API.
5+
Adds `detectTerminal()`, `TerminalInfo`, `Capabilities`, `DetectOptions`, `KeyTable`, and `MAX_TERMINFO_ENTRY` to the public API, and a `terminfo` option to `createTerm`.
66

7-
`detectTerminal()` reads a compiled terminfo binary (from disk or injected bytes), applies environment evidence (`COLORTERM`), and returns a frozen `Detection` carrying static `Capabilities`, a `probe` query batch to write to stdout, and opaque `keys` bytes for the input parser.
7+
`detectTerminal()` reads the compiled terminfo entry for the current terminal (from the ncurses search path, or from bytes passed as `entry`), applies environment evidence (`COLORTERM`), and resolves a frozen `TerminalInfo` carrying static `capabilities`, a `probe` query batch to write to stdout, and opaque `keys` for the input parser.
88

9-
Pass `detection` to `createInput` to seed its escape-sequence trie with terminal-specific `key_*` sequences. Pass capability events from `scan()` to `term.update()` to keep the renderer's runtime capability snapshot current.
9+
Pass the same `TerminalInfo` as `terminfo` to `createTerm` and `createInput`. `term.capabilities` exposes the renderer's current capability snapshot, seeded from `terminfo.capabilities` (or the 256-color baseline when omitted).
1010

11-
`scan()` now recognizes probe responses — OSC 10/11/12/21/22, XTGETTCAP, DECRPM mode 2026, kitty keyboard, kitty graphics APC, and the DA1 fence — and surfaces them as `CapabilityEvent` values interleaved with key and mouse events. Route these to `term.update()`, which returns a `Uint8Array` of bytes to write immediately (empty when no output is needed).
12-
13-
`InputOptions.terminfo` is replaced by `InputOptions.detection`. Both parsers remain usable without a `Detection`; the 256-color baseline and xterm default key sequences apply when it is omitted.
11+
**Breaking:** `term.update()` now takes one change or an array of changes — a `{ width, height }` resize or any `InputEvent` — instead of `{ events }`. Capability events from `scan()` are folded into `term.capabilities`; other input events are ignored, so the whole `events` array can be passed through. `update()` returns a `Uint8Array` of bytes to write immediately (empty when there are none).
1412

1513
#### Migration
1614

1715
```diff
18-
-const input = await createInput({ terminfo: await Deno.readFile(terminfoPath) });
19-
+const detection = await detectTerminal({ env: process.env });
20-
+const input = await createInput({ detection });
21-
+process.stdout.write(detection.probe);
16+
-term.update({ events });
17+
+const out = term.update(events);
18+
+if (out.length) process.stdout.write(out);
2219
```
2320

2421
```diff
25-
-term.update({ events: resizeEvents });
26-
+for (const event of input.scan(bytes).events) {
27-
+ if (event.type === "resize" || event.type === "capability") {
28-
+ const out = term.update(event);
29-
+ if (out.length) process.stdout.write(out);
30-
+ }
31-
+}
22+
+const terminfo = await detectTerminal({ env: process.env });
23+
+const term = await createTerm({ width, height, terminfo });
24+
+const input = await createInput({ terminfo });
25+
+process.stdout.write(terminfo.probe);
3226
```

‎term.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { type Op, pack } from "./ops.ts";
22
import { type BoundingBox, createTermNative } from "./term-native.ts";
33
import type { CapabilityEvent, ColorDepth, InputEvent } from "./input.ts";
4-
import type { Capabilities, Detection, Rgb } from "./terminfo.ts";
4+
import type { Capabilities, Rgb, TerminalInfo } from "./terminfo.ts";
55

66
export type { BoundingBox };
77

88
export interface TermOptions {
99
height: number;
1010
width: number;
1111
/**
12-
* Detection from detectTerminal(). Initializes the renderer with the
13-
* static capabilities from the detection and seeds the private TermInfo
12+
* Terminal info from detectTerminal(). Initializes the renderer with
13+
* its static capabilities and seeds the private TermInfo
1414
* struct. When omitted, the renderer uses the 256-color baseline.
1515
*/
16-
detection?: Detection;
16+
terminfo?: TerminalInfo;
1717
}
1818

1919
/**
@@ -166,7 +166,7 @@ export interface Term {
166166
}
167167

168168
export async function createTerm(options: TermOptions): Promise<Term> {
169-
let { width, height, detection } = options;
169+
let { width, height, terminfo } = options;
170170

171171
let native = await createTermNative(
172172
width,
@@ -175,7 +175,7 @@ export async function createTerm(options: TermOptions): Promise<Term> {
175175
let { memory } = native;
176176

177177
let currentCaps: RuntimeCapabilities = runtimeFromStatic(
178-
detection?.capabilities ?? {
178+
terminfo?.capabilities ?? {
179179
colors: 256,
180180
trueColor: false,
181181
bce: true,

‎test/term.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
text,
1313
} from "../ops.ts";
1414
import { print } from "./print.ts";
15+
import { trueColorDetect } from "./caps.ts";
1516

1617
const decode = (bytes: Uint8Array) => new TextDecoder().decode(bytes);
1718
const trim = (s: string) => s.split("\n").map((l) => l.trimEnd()).join("\n");
@@ -641,6 +642,22 @@ hi
641642
});
642643
});
643644

645+
describe("capabilities", () => {
646+
it("starts from the 256-color baseline without terminfo", () => {
647+
expect(term.capabilities.colors).toBe(256);
648+
expect(term.capabilities.trueColor).toBe(false);
649+
});
650+
651+
it("seeds static capabilities from terminfo", async () => {
652+
let terminfo = await trueColorDetect();
653+
let seeded = await createTerm({ width: 40, height: 10, terminfo });
654+
expect(seeded.capabilities.trueColor).toBe(true);
655+
for (let [key, value] of Object.entries(terminfo.capabilities)) {
656+
expect(seeded.capabilities).toHaveProperty(key, value);
657+
}
658+
});
659+
});
660+
644661
describe("update", () => {
645662
let frame: Op[] = [
646663
open("root", {

0 commit comments

Comments
 (0)