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
16 changes: 16 additions & 0 deletions .changeset/currentcolor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---

@bombshell-cooper bombshell-cooper Bot Oct 20, 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 should start with an approved active present-tense verb. Changeset package scope does not match the affected packages confidently.

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

Adds `color` to `open()`: the element's own foreground color, inherited by every descendant that does not set its own.

`currentcolor` is now a valid color string at every color field. In `open({ color })` and `text({ color })` it means "inherit" (same as omitting the field); in any other field — `bg`, `border.color`, `border.bg`, or a structured border side's `color`/`bg` — it resolves to the directive's own resolved foreground, then follows normal field resolution (terminal-themed, probed-theme, or sRGB passthrough). `border.color` is now optional and defaults to `currentcolor`, matching CSS.

#### Migration

`border.color` was previously required. Existing code that supplies it is unaffected. Code that relied on a missing `border.color` being a validation error must now supply an explicit color to override inheritance:

```diff
  open("box", {
    border: {
-     color: "red",   // was required; remove to inherit, or keep to override
+     color: "red",
      top: 1,
    },
  })
```

If you were checking `validate()` against a border with no `color`, that case now returns `true` instead of `false`.

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

`open()` accepts a `color` field: the element's own foreground color, inherited
by every descendant that doesn't set its own. `currentcolor` is now a valid
color string everywhere, including `color` itself, where it means "inherit"
(the same as omitting the field). Used in any other color field —
`open({ bg })`, `text({ bg })`, `border.color`, `border.bg`, or a structured
border side's `color`/`bg` — it resolves to the directive's own resolved
color (its explicit `color`, or the color it inherits), then follows the same
field resolution as any other color, so it probes the terminal theme and
transitions normally when that resolved color is an sRGB value. `border.color`
is now optional and defaults to `currentcolor`, matching CSS; a border with no
`color` set anywhere picks up the element's own foreground, same as before
this feature existed.
25 changes: 21 additions & 4 deletions color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,33 @@ function byte(unit: number): number {
return Math.round(Math.min(Math.max(unit, 0), 1) * 255);
}

/**
* A §8.5 `currentcolor` sentinel. In a `color` field it means "inherit"; in
* any other field it means "this directive's own resolved color." Both
* substitutions happen in `pack()`'s inheritance stack (ops.ts), not here.
*/
export const CURRENT_COLOR: { readonly current: true } = { current: true };
export type CurrentColor = typeof CURRENT_COLOR;

/** Narrows a `resolveColor()` result to the `currentcolor` sentinel. */
export function isCurrentColor(
value: number | TerminalColor | CurrentColor,
): value is CurrentColor {
return typeof value === "object" && "current" in value;
}

/**
* Resolves a §8.5 CSS color string to the packed ARGB number the transfer
* encoding carries, a terminal-themed color for a system color keyword, or
* `undefined` when the string is not a supported color. Channels clamp to
* the sRGB range, so out-of-gamut colors clip per channel.
* encoding carries, a terminal-themed color for a system color keyword, the
* `currentcolor` sentinel, or `undefined` when the string is not a supported
* color. Channels clamp to the sRGB range, so out-of-gamut colors clip per
* channel.
*/
export function resolveColor(
input: string,
): number | TerminalColor | undefined {
): number | TerminalColor | CurrentColor | undefined {
let lower = input.toLowerCase();
if (lower === "currentcolor") return CURRENT_COLOR;
if (Object.hasOwn(systemColors, lower)) {
return { ansi: systemColors[lower as keyof typeof systemColors] };
}
Expand Down
95 changes: 75 additions & 20 deletions ops.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { names } from "hueful";
import {
isCurrentColor,
resolveColor,
resolveForField,
type systemColors,
Expand Down Expand Up @@ -104,20 +105,22 @@ function sideFg(
side: BorderSide | undefined,
shared: Color,
theme: ThemeColors | undefined,
current: number | TerminalColor,
): number {
let color = typeof side === "object" && side.color !== undefined
? side.color
: shared;
return colorWord(color, "fg", theme);
return colorWord(color, "fg", theme, current);
}

function sideBg(
side: BorderSide | undefined,
shared: Color | undefined,
theme: ThemeColors | undefined,
current: number | TerminalColor,
): number {
let bg = typeof side === "object" && side.bg !== undefined ? side.bg : shared;
return bg === undefined ? BG_KEEP : colorWord(bg, "bg", theme);
return bg === undefined ? BG_KEEP : colorWord(bg, "bg", theme, current);
}

function packString(
Expand Down Expand Up @@ -155,11 +158,16 @@ export function pack(
let end = limit ?? mem.byteLength;
let o = offset;

// §8.5 foreground inheritance: pushed on open (the element's own resolved
// color), popped on the matching close. Scoped to this single pass.
let colorStack: (number | TerminalColor)[] = [DEFAULT_FG];

for (let op of ops) {
switch (op.directive) {
case OP_CLOSE_ELEMENT:
view.setUint32(o, op.directive, true);
o += 4;
if (colorStack.length > 1) colorStack.pop();
break;

case OP_OPEN_ELEMENT: {
Expand All @@ -169,9 +177,15 @@ export function pack(
let bytes = encoder.encode(op.id);
o = packString(view, bytes, o, end, "element id");

let ownColor = resolveOwn(
op.color,
colorStack[colorStack.length - 1],
);
colorStack.push(ownColor);

let bg = op.bg === undefined
? undefined
: colorValue(op.bg, "bg", theme);
: colorValue(op.bg, "bg", theme, ownColor);
let mask = 0;
if (op.layout) mask |= PROP_LAYOUT;
if (typeof bg === "number") mask |= PROP_BG_COLOR;
Expand Down Expand Up @@ -254,10 +268,11 @@ export function pack(
// attribute words: fg/bg pairs in top, right, bottom, left order.
// C treats the presence and order of these words as a wire-format
// invariant and only consumes the explicit values.
let sharedColor: Color = b.color ?? "currentcolor";
for (let side of [b.top, b.right, b.bottom, b.left]) {
view.setUint32(o, sideFg(side, b.color, theme), true);
view.setUint32(o, sideFg(side, sharedColor, theme, ownColor), true);
o += 4;
view.setUint32(o, sideBg(side, b.bg, theme), true);
view.setUint32(o, sideBg(side, b.bg, theme, ownColor), true);
o += 4;
}
}
Expand Down Expand Up @@ -332,11 +347,15 @@ export function pack(
view.setUint32(o, OP_TEXT, true);
o += 4;
// The fg word's bit 31 travels in the attrs byte (0x80); the C path
// ORs the attrs byte back over the high byte. No color is the
// terminal default.
let color = op.color === undefined
? { ansi: "fg-default" as const }
: colorValue(op.color, "fg", theme);
// ORs the attrs byte back over the high byte. No color inherits the
// nearest ancestor open()'s color (§8.5), terminal default at root.
let ownColor = resolveOwn(
op.color,
colorStack[colorStack.length - 1],
);
let color = typeof ownColor === "number"
? ownColor
: resolveForField(ownColor, "fg", theme);
let terminal = typeof color === "object";
view.setUint32(
o,
Expand All @@ -347,7 +366,9 @@ export function pack(

view.setUint32(
o,
op.bg === undefined ? BG_KEEP : colorWord(op.bg, "bg", theme),
op.bg === undefined
? BG_KEEP
: colorWord(op.bg, "bg", theme, ownColor),
true,
);
o += 4;
Expand Down Expand Up @@ -398,21 +419,45 @@ export type Color = number | string;
/**
* Resolves a color value to a packed ARGB number or a terminal-themed color
* (§8.5), for the field it's packed into. Numbers pass through; strings
* resolve through the CSS color grammar. A cross-field default keyword
* resolves against `theme` to a packed ARGB number, falling back to the
* field's own terminal default when `theme` lacks that color. Invalid
* strings throw rather than decoding to a garbage color.
* resolve through the CSS color grammar. `currentcolor` substitutes `current`
* — the directive's own resolved color — before field resolution runs, same
* as any other color. A cross-field default keyword resolves against `theme`
* to a packed ARGB number, falling back to the field's own terminal default
* when `theme` lacks that color. Invalid strings throw rather than decoding
* to a garbage color.
*/
function colorValue(
color: Color,
field: "fg" | "bg",
theme: ThemeColors | undefined,
current: number | TerminalColor,
): number | TerminalColor {
if (typeof color === "number") return color;
let value = resolveColor(color);
if (value === undefined) throw new TypeError(`invalid color: "${color}"`);
if (typeof value === "number") return value;
return resolveForField(value, field, theme);
let resolved = isCurrentColor(value) ? current : value;
if (typeof resolved === "number") return resolved;
return resolveForField(resolved, field, theme);
}

/**
* Resolves the canonical (pre field-resolution) color a directive's own
* `color` property names — its inheritance value for §8.5's foreground
* inheritance. `undefined` or `currentcolor` inherits `parent`; any other
* value resolves through the CSS color grammar, same as `colorValue`, but
* without field resolution — that happens where the value is later consumed.
*/
function resolveOwn(
explicit: Color | undefined,
parent: number | TerminalColor,
): number | TerminalColor {
if (explicit === undefined) return parent;
if (typeof explicit === "number") return explicit;
let value = resolveColor(explicit);
if (value === undefined) {
throw new TypeError(`invalid color: "${explicit}"`);
}
return isCurrentColor(value) ? parent : value;
}

function terminalWord(color: TerminalColor): number {
Expand All @@ -426,11 +471,15 @@ function colorWord(
color: Color,
field: "fg" | "bg",
theme: ThemeColors | undefined,
current: number | TerminalColor,
): number {
let value = colorValue(color, field, theme);
let value = colorValue(color, field, theme, current);
return typeof value === "number" ? value & 0x00FFFFFF : terminalWord(value);
}

/* The directive-chain default when no ancestor `open()` sets `color`. */
const DEFAULT_FG: TerminalColor = { ansi: "fg-default" };

export type SizingAxis =
| { type: "fit"; min?: number; max?: number }
| { type: "grow"; min?: number; max?: number }
Expand Down Expand Up @@ -468,6 +517,10 @@ export type BorderSide =
export interface OpenElement {
directive: typeof OP_OPEN_ELEMENT;
id: string;
/** The element's own foreground color (§8.5). Inherited by descendants
* that do not override it; `currentcolor` means "inherit," same as
* omitting this field. */
color?: Color;
layout?: {
width?: SizingAxis;
height?: SizingAxis;
Expand All @@ -480,7 +533,8 @@ export interface OpenElement {
bg?: Color;
cornerRadius?: { tl?: number; tr?: number; bl?: number; br?: number };
border?: {
color: Color;
/** Defaults to `currentcolor` (§8.5): the element's own resolved color. */
color?: Color;
bg?: Color;
left?: BorderSide;
right?: BorderSide;
Expand Down Expand Up @@ -640,7 +694,8 @@ type ColorFunction =
type NamedColor =
| keyof typeof names
| keyof typeof systemColors
| "transparent";
| "transparent"
| "currentcolor";

type CheckedColor<T> = T extends number ? T
: T extends string ? string extends T ? T
Expand Down
Loading
Loading