Skip to content

Commit f010a29

Browse files
committed
feat: resolve cross-field default keywords against the probed theme
pack() gains an optional trailing theme parameter (RuntimeCapabilities.theme shape); term.render() passes currentCaps.theme every frame. color.ts's systemColors now tags each default keyword with which attribute it names (fg-default/bg-default) instead of a single undifferentiated "default", and resolveForField() picks the terminal default for a same-field keyword or the probed theme color for a cross-field one, falling back to the field's own default when that color hasn't been probed yet. Covers pack-level byte identity against rgba(), the no-theme fallback, and render-through-Term cases (text bg, element bg, border side fg) that pick up a theme color after term.update() folds in an OSC 10/11 CapabilityEvent.
1 parent 437d564 commit f010a29

8 files changed

Lines changed: 278 additions & 61 deletions

File tree

‎.changeset/system-colors-ansi16.md‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@ ANSI 16-color palette (`LinkText` is blue, `GrayText` is bright black, and so
1010
on), so output follows the user's light or dark terminal theme. The
1111
default-color keywords mean the default of the attribute they set, so
1212
`text({ bg: "Canvas" })` paints the terminal default background over any
13-
enclosing element background, while an omitted `bg` still keeps it. Packed
14-
`rgba()` colors and other color strings render exactly as before.
13+
enclosing element background, while an omitted `bg` still keeps it. Named in
14+
the other field instead, a default keyword now paints the terminal's actual
15+
default color once it's known: `text({ bg: "CanvasText" })` renders the
16+
terminal's real default foreground color as an opaque truecolor background,
17+
learned from the terminal's OSC 10/11 theme report, falling back to the
18+
default background until that report arrives. Packed `rgba()` colors and
19+
other color strings render exactly as before.

‎color.ts‎

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
import { convert, parse } from "hueful";
22

33
/**
4-
* A §8.5 terminal-themed color: an ANSI palette index (0–15), or the
5-
* terminal default color of whichever attribute the field sets.
4+
* A §8.5 terminal-themed color: an ANSI palette index (0–15), or a default
5+
* keyword naming the terminal default foreground ("fg-default") or
6+
* background ("bg-default"). Which default it names is fixed by the
7+
* keyword; whether it stays terminal-themed or resolves to a probed sRGB
8+
* color depends on the field it's packed into (see resolveForField).
69
*/
710
export interface TerminalColor {
8-
ansi: number | "default";
11+
ansi: number | "fg-default" | "bg-default";
12+
}
13+
14+
/** An 8-bit RGB color, structurally compatible with terminfo.ts's Rgb. */
15+
export interface ThemeRgb {
16+
r: number;
17+
g: number;
18+
b: number;
19+
}
20+
21+
/** The theme colors `pack()` resolves cross-field default keywords against. */
22+
export interface ThemeColors {
23+
readonly foreground?: ThemeRgb;
24+
readonly background?: ThemeRgb;
925
}
1026

1127
export const systemColors = {
12-
canvas: "default",
13-
buttonface: "default",
14-
field: "default",
15-
canvastext: "default",
16-
buttontext: "default",
17-
fieldtext: "default",
28+
canvas: "bg-default",
29+
buttonface: "bg-default",
30+
field: "bg-default",
31+
canvastext: "fg-default",
32+
buttontext: "fg-default",
33+
fieldtext: "fg-default",
1834
activetext: 1,
1935
mark: 3,
2036
linktext: 4,
@@ -53,3 +69,31 @@ export function resolveColor(
5369
return (byte(alpha) << 24) | (byte(r / 255) << 16) | (byte(g / 255) << 8) |
5470
byte(b / 255);
5571
}
72+
73+
/**
74+
* Resolves a terminal-themed color for the field it was packed into (§8.5).
75+
* A same-field default keyword and a palette entry stay terminal-themed. A
76+
* cross-field default keyword (a fg-default keyword in a bg field, or a
77+
* bg-default keyword in a fg field) resolves to the probed theme color for
78+
* the attribute it names, packed as an ordinary opaque ARGB number; absent
79+
* that probe, it falls back to the field's own terminal default.
80+
*/
81+
export function resolveForField(
82+
color: TerminalColor,
83+
field: "fg" | "bg",
84+
theme: ThemeColors | undefined,
85+
): number | TerminalColor {
86+
let { ansi } = color;
87+
if (ansi !== "fg-default" && ansi !== "bg-default") return color;
88+
if (
89+
(ansi === "fg-default" && field === "fg") ||
90+
(ansi === "bg-default" && field === "bg")
91+
) {
92+
return color;
93+
}
94+
let rgb = ansi === "fg-default" ? theme?.foreground : theme?.background;
95+
if (rgb === undefined) {
96+
return { ansi: field === "fg" ? "fg-default" : "bg-default" };
97+
}
98+
return (255 << 24) | (rgb.r << 16) | (rgb.g << 8) | rgb.b;
99+
}

‎ops.ts‎

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { names } from "hueful";
22
import {
33
resolveColor,
4+
resolveForField,
45
type systemColors,
56
type TerminalColor,
7+
type ThemeColors,
68
} from "./color.ts";
79
import { type PointerShape, pointerShapeIndex } from "./pointer-shape.ts";
810

@@ -98,19 +100,24 @@ function sideWidth(side: BorderSide | undefined): number {
98100
return typeof side === "number" ? side : side?.width ?? 0;
99101
}
100102

101-
function sideFg(side: BorderSide | undefined, shared: Color): number {
103+
function sideFg(
104+
side: BorderSide | undefined,
105+
shared: Color,
106+
theme: ThemeColors | undefined,
107+
): number {
102108
let color = typeof side === "object" && side.color !== undefined
103109
? side.color
104110
: shared;
105-
return colorWord(color);
111+
return colorWord(color, "fg", theme);
106112
}
107113

108114
function sideBg(
109115
side: BorderSide | undefined,
110116
shared: Color | undefined,
117+
theme: ThemeColors | undefined,
111118
): number {
112119
let bg = typeof side === "object" && side.bg !== undefined ? side.bg : shared;
113-
return bg === undefined ? BG_KEEP : colorWord(bg);
120+
return bg === undefined ? BG_KEEP : colorWord(bg, "bg", theme);
114121
}
115122

116123
function packString(
@@ -142,6 +149,7 @@ export function pack(
142149
mem: ArrayBufferLike,
143150
offset: number,
144151
limit?: number,
152+
theme?: ThemeColors,
145153
): number {
146154
let view = new DataView(mem);
147155
let end = limit ?? mem.byteLength;
@@ -161,7 +169,9 @@ export function pack(
161169
let bytes = encoder.encode(op.id);
162170
o = packString(view, bytes, o, end, "element id");
163171

164-
let bg = op.bg === undefined ? undefined : colorValue(op.bg);
172+
let bg = op.bg === undefined
173+
? undefined
174+
: colorValue(op.bg, "bg", theme);
165175
let mask = 0;
166176
if (op.layout) mask |= PROP_LAYOUT;
167177
if (typeof bg === "number") mask |= PROP_BG_COLOR;
@@ -245,9 +255,9 @@ export function pack(
245255
// C treats the presence and order of these words as a wire-format
246256
// invariant and only consumes the explicit values.
247257
for (let side of [b.top, b.right, b.bottom, b.left]) {
248-
view.setUint32(o, sideFg(side, b.color), true);
258+
view.setUint32(o, sideFg(side, b.color, theme), true);
249259
o += 4;
250-
view.setUint32(o, sideBg(side, b.bg), true);
260+
view.setUint32(o, sideBg(side, b.bg, theme), true);
251261
o += 4;
252262
}
253263
}
@@ -325,8 +335,8 @@ export function pack(
325335
// ORs the attrs byte back over the high byte. No color is the
326336
// terminal default.
327337
let color = op.color === undefined
328-
? { ansi: "default" as const }
329-
: colorValue(op.color);
338+
? { ansi: "fg-default" as const }
339+
: colorValue(op.color, "fg", theme);
330340
let terminal = typeof color === "object";
331341
view.setUint32(
332342
o,
@@ -337,7 +347,7 @@ export function pack(
337347

338348
view.setUint32(
339349
o,
340-
op.bg === undefined ? BG_KEEP : colorWord(op.bg),
350+
op.bg === undefined ? BG_KEEP : colorWord(op.bg, "bg", theme),
341351
true,
342352
);
343353
o += 4;
@@ -387,23 +397,37 @@ export type Color = number | string;
387397

388398
/**
389399
* Resolves a color value to a packed ARGB number or a terminal-themed color
390-
* (§8.5). Numbers pass through; strings resolve through the CSS color
391-
* grammar. Invalid strings throw rather than decoding to a garbage color.
400+
* (§8.5), for the field it's packed into. Numbers pass through; strings
401+
* resolve through the CSS color grammar. A cross-field default keyword
402+
* resolves against `theme` to a packed ARGB number, falling back to the
403+
* field's own terminal default when `theme` lacks that color. Invalid
404+
* strings throw rather than decoding to a garbage color.
392405
*/
393-
function colorValue(color: Color): number | TerminalColor {
406+
function colorValue(
407+
color: Color,
408+
field: "fg" | "bg",
409+
theme: ThemeColors | undefined,
410+
): number | TerminalColor {
394411
if (typeof color === "number") return color;
395412
let value = resolveColor(color);
396413
if (value === undefined) throw new TypeError(`invalid color: "${color}"`);
397-
return value;
414+
if (typeof value === "number") return value;
415+
return resolveForField(value, field, theme);
398416
}
399417

400418
function terminalWord(color: TerminalColor): number {
401-
return color.ansi === "default" ? TERMINAL : TERMINAL | PALETTE | color.ansi;
419+
return typeof color.ansi === "number"
420+
? TERMINAL | PALETTE | color.ansi
421+
: TERMINAL;
402422
}
403423

404424
/* The color word text bg and border attributes carry; alpha is dropped. */
405-
function colorWord(color: Color): number {
406-
let value = colorValue(color);
425+
function colorWord(
426+
color: Color,
427+
field: "fg" | "bg",
428+
theme: ThemeColors | undefined,
429+
): number {
430+
let value = colorValue(color, field, theme);
407431
return typeof value === "number" ? value & 0x00FFFFFF : terminalWord(value);
408432
}
409433

‎specs/renderer-spec.md‎

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -815,42 +815,41 @@ color; the terminal applies its own theme when it draws the cell.
815815
| `HighlightText`, `SelectedItemText` | palette 15 (bright white) |
816816
| `MarkText`, `AccentColorText` | palette 0 (black) |
817817

818-
Each default keyword names the terminal default color of one specific
819-
attribute: `Canvas`, `ButtonFace`, and `Field` name the default background;
820-
`CanvasText`, `ButtonText`, and `FieldText` name the default foreground.
821-
**Used in its own field** — a background-default keyword in a background
822-
field (`open({ bg })`, `text({ bg })`, `border.bg`, a border side's `bg`), or
823-
a foreground-default keyword in a foreground field (`text({ color })`,
824-
`border.color`, a border side's `color`) — it resolves to that field's
825-
terminal default, unchanged from the table above.
818+
Each default keyword names the terminal default color of one specific attribute:
819+
`Canvas`, `ButtonFace`, and `Field` name the default background; `CanvasText`,
820+
`ButtonText`, and `FieldText` name the default foreground. **Used in its own
821+
field** — a background-default keyword in a background field (`open({ bg })`,
822+
`text({ bg })`, `border.bg`, a border side's `bg`), or a foreground-default
823+
keyword in a foreground field (`text({ color })`, `border.color`, a border
824+
side's `color`) — it resolves to that field's terminal default, unchanged from
825+
the table above.
826826

827827
**Used in the other field** — a foreground-default keyword in a background
828-
field, or a background-default keyword in a foreground field, including a
829-
border side's `color` — it resolves to the terminal's probed color for the
830-
attribute it names, not to that field's default. The renderer reads this from
831-
the OSC 10/11 theme probe, surfaced as `RuntimeCapabilities.theme.foreground`
832-
/ `.background` (terminfo-spec §6.4) and passed into `pack()` as the theme
833-
snapshot `term.render()` holds at render time (§12.5). The result is an
834-
ordinary opaque sRGB color (alpha 255): it packs exactly like the equivalent
835-
`rgba()` value, renders as truecolor, and transitions interpolate it like any
836-
other RGB color — unlike the terminal-themed colors described below. When the
837-
theme has not yet reported that color, the keyword falls back to its field's
838-
own terminal default instead (the same-field behavior above). A theme report
839-
that arrives after a frame renders takes effect starting with the next
840-
render.
828+
field, or a background-default keyword in a foreground field, including a border
829+
side's `color` — it resolves to the terminal's probed color for the attribute it
830+
names, not to that field's default. The renderer reads this from the OSC 10/11
831+
theme probe, surfaced as `RuntimeCapabilities.theme.foreground` / `.background`
832+
(terminfo-spec §6.4) and passed into `pack()` as the theme snapshot
833+
`term.render()` holds at render time (§12.5). The result is an ordinary opaque
834+
sRGB color (alpha 255): it packs exactly like the equivalent `rgba()` value,
835+
renders as truecolor, and transitions interpolate it like any other RGB color —
836+
unlike the terminal-themed colors described below. When the theme has not yet
837+
reported that color, the keyword falls back to its field's own terminal default
838+
instead (the same-field behavior above). A theme report that arrives after a
839+
frame renders takes effect starting with the next render.
841840

842841
For example, once the terminal has reported its default foreground color,
843842
`text({ bg: "CanvasText" })` paints that color as an opaque truecolor
844843
background. Until then it falls back to the default background: a terminal
845-
offers no way to paint its default foreground color as a cell background
846-
without knowing its RGB value, and the renderer never guesses one.
844+
offers no way to paint its default foreground color as a cell background without
845+
knowing its RGB value, and the renderer never guesses one.
847846

848-
A terminal-themed color (a same-field default, or a palette entry) is opaque.
849-
A palette color in a foreground field is emitted as SGR `30`–`37` for palette
850-
0–7 and `90`–`97` for palette 8–15; in a background field, as SGR `40`–`47`
851-
and `100`–`107`. A terminal default color is emitted as SGR `39` (foreground)
852-
or `49` (background), or by omitting the foreground or background SGR after
853-
an SGR reset, which leaves the same default in effect.
847+
A terminal-themed color (a same-field default, or a palette entry) is opaque. A
848+
palette color in a foreground field is emitted as SGR `30`–`37` for palette 0–7
849+
and `90`–`97` for palette 8–15; in a background field, as SGR `40`–`47` and
850+
`100`–`107`. A terminal default color is emitted as SGR `39` (foreground) or
851+
`49` (background), or by omitting the foreground or background SGR after an SGR
852+
reset, which leaves the same default in effect.
854853

855854
A terminal default background is a color, not the absence of one. An explicit
856855
`Canvas` background (or `ButtonFace`, `Field`, or any default keyword in a
@@ -1368,11 +1367,11 @@ Exported and used in tests.
13681367
serializes a directive array into the transfer encoding described in Section
13691368
12.1. Currently exported but not public API; its exposure is incidental to the
13701369
module structure. The optional trailing `theme` carries the probed default
1371-
foreground/background colors (`RuntimeCapabilities.theme`, terminfo-spec
1372-
§6.4), used to resolve a cross-field default keyword (§8.5) to the terminal's
1373-
actual default color. `term.render()` passes its current capability
1374-
snapshot's `theme`; a caller that omits it gets the field-default fallback for
1375-
every cross-field keyword.
1370+
foreground/background colors (`RuntimeCapabilities.theme`, terminfo-spec §6.4),
1371+
used to resolve a cross-field default keyword (§8.5) to the terminal's actual
1372+
default color. `term.render()` passes its current capability snapshot's `theme`;
1373+
a caller that omits it gets the field-default fallback for every cross-field
1374+
keyword.
13761375

13771376
---
13781377

‎term.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ export async function createTerm(options: TermOptions): Promise<Term> {
205205
memory.buffer,
206206
native.opsBuf,
207207
memory.buffer.byteLength,
208+
currentCaps.theme,
208209
);
209210
let mode = options?.mode === "line" ? 1 : 0;
210211
let row = options?.row ?? 1;

‎test/border.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,4 +723,28 @@ describe("system colors", () => {
723723
expect(at(parsed, 3, 0).fg).toBeUndefined();
724724
expect(at(parsed, 3, 0).bg).toBeUndefined();
725725
});
726+
727+
it("renders a probed default background as a truecolor border side fg once the theme reports it", async () => {
728+
let term = await createTerm({ width: 12, height: 5 });
729+
term.update([
730+
{
731+
type: "capability",
732+
key: "background-color",
733+
value: { r: 200, g: 210, b: 220 },
734+
},
735+
]);
736+
let ansi = decode(
737+
term.render([
738+
open("box", {
739+
layout: { width: fixed(8), height: fixed(4) },
740+
border: { color: "Canvas", top: 1 },
741+
}),
742+
close(),
743+
], { mode: "line" }).output,
744+
);
745+
let parsed = cells(ansi);
746+
747+
expect(at(parsed, 3, 0).ch).toBe("─");
748+
expect(at(parsed, 3, 0).fg).toBe("\x1b[38;2;200;210;220");
749+
});
726750
});

0 commit comments

Comments
 (0)