11import type { names } from "hueful" ;
22import {
3+ isCurrentColor ,
34 resolveColor ,
45 resolveForField ,
56 type systemColors ,
@@ -104,20 +105,22 @@ function sideFg(
104105 side : BorderSide | undefined ,
105106 shared : Color ,
106107 theme : ThemeColors | undefined ,
108+ current : number | TerminalColor ,
107109) : number {
108110 let color = typeof side === "object" && side . color !== undefined
109111 ? side . color
110112 : shared ;
111- return colorWord ( color , "fg" , theme ) ;
113+ return colorWord ( color , "fg" , theme , current ) ;
112114}
113115
114116function sideBg (
115117 side : BorderSide | undefined ,
116118 shared : Color | undefined ,
117119 theme : ThemeColors | undefined ,
120+ current : number | TerminalColor ,
118121) : number {
119122 let bg = typeof side === "object" && side . bg !== undefined ? side . bg : shared ;
120- return bg === undefined ? BG_KEEP : colorWord ( bg , "bg" , theme ) ;
123+ return bg === undefined ? BG_KEEP : colorWord ( bg , "bg" , theme , current ) ;
121124}
122125
123126function packString (
@@ -155,11 +158,16 @@ export function pack(
155158 let end = limit ?? mem . byteLength ;
156159 let o = offset ;
157160
161+ // §8.5 foreground inheritance: pushed on open (the element's own resolved
162+ // color), popped on the matching close. Scoped to this single pass.
163+ let colorStack : ( number | TerminalColor ) [ ] = [ DEFAULT_FG ] ;
164+
158165 for ( let op of ops ) {
159166 switch ( op . directive ) {
160167 case OP_CLOSE_ELEMENT :
161168 view . setUint32 ( o , op . directive , true ) ;
162169 o += 4 ;
170+ if ( colorStack . length > 1 ) colorStack . pop ( ) ;
163171 break ;
164172
165173 case OP_OPEN_ELEMENT : {
@@ -169,9 +177,15 @@ export function pack(
169177 let bytes = encoder . encode ( op . id ) ;
170178 o = packString ( view , bytes , o , end , "element id" ) ;
171179
180+ let ownColor = resolveOwn (
181+ op . color ,
182+ colorStack [ colorStack . length - 1 ] ,
183+ ) ;
184+ colorStack . push ( ownColor ) ;
185+
172186 let bg = op . bg === undefined
173187 ? undefined
174- : colorValue ( op . bg , "bg" , theme ) ;
188+ : colorValue ( op . bg , "bg" , theme , ownColor ) ;
175189 let mask = 0 ;
176190 if ( op . layout ) mask |= PROP_LAYOUT ;
177191 if ( typeof bg === "number" ) mask |= PROP_BG_COLOR ;
@@ -254,10 +268,11 @@ export function pack(
254268 // attribute words: fg/bg pairs in top, right, bottom, left order.
255269 // C treats the presence and order of these words as a wire-format
256270 // invariant and only consumes the explicit values.
271+ let sharedColor : Color = b . color ?? "currentcolor" ;
257272 for ( let side of [ b . top , b . right , b . bottom , b . left ] ) {
258- view . setUint32 ( o , sideFg ( side , b . color , theme ) , true ) ;
273+ view . setUint32 ( o , sideFg ( side , sharedColor , theme , ownColor ) , true ) ;
259274 o += 4 ;
260- view . setUint32 ( o , sideBg ( side , b . bg , theme ) , true ) ;
275+ view . setUint32 ( o , sideBg ( side , b . bg , theme , ownColor ) , true ) ;
261276 o += 4 ;
262277 }
263278 }
@@ -332,11 +347,15 @@ export function pack(
332347 view . setUint32 ( o , OP_TEXT , true ) ;
333348 o += 4 ;
334349 // The fg word's bit 31 travels in the attrs byte (0x80); the C path
335- // ORs the attrs byte back over the high byte. No color is the
336- // terminal default.
337- let color = op . color === undefined
338- ? { ansi : "fg-default" as const }
339- : colorValue ( op . color , "fg" , theme ) ;
350+ // ORs the attrs byte back over the high byte. No color inherits the
351+ // nearest ancestor open()'s color (§8.5), terminal default at root.
352+ let ownColor = resolveOwn (
353+ op . color ,
354+ colorStack [ colorStack . length - 1 ] ,
355+ ) ;
356+ let color = typeof ownColor === "number"
357+ ? ownColor
358+ : resolveForField ( ownColor , "fg" , theme ) ;
340359 let terminal = typeof color === "object" ;
341360 view . setUint32 (
342361 o ,
@@ -347,7 +366,9 @@ export function pack(
347366
348367 view . setUint32 (
349368 o ,
350- op . bg === undefined ? BG_KEEP : colorWord ( op . bg , "bg" , theme ) ,
369+ op . bg === undefined
370+ ? BG_KEEP
371+ : colorWord ( op . bg , "bg" , theme , ownColor ) ,
351372 true ,
352373 ) ;
353374 o += 4 ;
@@ -398,21 +419,45 @@ export type Color = number | string;
398419/**
399420 * Resolves a color value to a packed ARGB number or a terminal-themed color
400421 * (§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.
422+ * resolve through the CSS color grammar. `currentcolor` substitutes `current`
423+ * — the directive's own resolved color — before field resolution runs, same
424+ * as any other color. A cross-field default keyword resolves against `theme`
425+ * to a packed ARGB number, falling back to the field's own terminal default
426+ * when `theme` lacks that color. Invalid strings throw rather than decoding
427+ * to a garbage color.
405428 */
406429function colorValue (
407430 color : Color ,
408431 field : "fg" | "bg" ,
409432 theme : ThemeColors | undefined ,
433+ current : number | TerminalColor ,
410434) : number | TerminalColor {
411435 if ( typeof color === "number" ) return color ;
412436 let value = resolveColor ( color ) ;
413437 if ( value === undefined ) throw new TypeError ( `invalid color: "${ color } "` ) ;
414- if ( typeof value === "number" ) return value ;
415- return resolveForField ( value , field , theme ) ;
438+ let resolved = isCurrentColor ( value ) ? current : value ;
439+ if ( typeof resolved === "number" ) return resolved ;
440+ return resolveForField ( resolved , field , theme ) ;
441+ }
442+
443+ /**
444+ * Resolves the canonical (pre field-resolution) color a directive's own
445+ * `color` property names — its inheritance value for §8.5's foreground
446+ * inheritance. `undefined` or `currentcolor` inherits `parent`; any other
447+ * value resolves through the CSS color grammar, same as `colorValue`, but
448+ * without field resolution — that happens where the value is later consumed.
449+ */
450+ function resolveOwn (
451+ explicit : Color | undefined ,
452+ parent : number | TerminalColor ,
453+ ) : number | TerminalColor {
454+ if ( explicit === undefined ) return parent ;
455+ if ( typeof explicit === "number" ) return explicit ;
456+ let value = resolveColor ( explicit ) ;
457+ if ( value === undefined ) {
458+ throw new TypeError ( `invalid color: "${ explicit } "` ) ;
459+ }
460+ return isCurrentColor ( value ) ? parent : value ;
416461}
417462
418463function terminalWord ( color : TerminalColor ) : number {
@@ -426,11 +471,15 @@ function colorWord(
426471 color : Color ,
427472 field : "fg" | "bg" ,
428473 theme : ThemeColors | undefined ,
474+ current : number | TerminalColor ,
429475) : number {
430- let value = colorValue ( color , field , theme ) ;
476+ let value = colorValue ( color , field , theme , current ) ;
431477 return typeof value === "number" ? value & 0x00FFFFFF : terminalWord ( value ) ;
432478}
433479
480+ /* The directive-chain default when no ancestor `open()` sets `color`. */
481+ const DEFAULT_FG : TerminalColor = { ansi : "fg-default" } ;
482+
434483export type SizingAxis =
435484 | { type : "fit" ; min ?: number ; max ?: number }
436485 | { type : "grow" ; min ?: number ; max ?: number }
@@ -468,6 +517,10 @@ export type BorderSide =
468517export interface OpenElement {
469518 directive : typeof OP_OPEN_ELEMENT ;
470519 id : string ;
520+ /** The element's own foreground color (§8.5). Inherited by descendants
521+ * that do not override it; `currentcolor` means "inherit," same as
522+ * omitting this field. */
523+ color ?: Color ;
471524 layout ?: {
472525 width ?: SizingAxis ;
473526 height ?: SizingAxis ;
@@ -480,7 +533,8 @@ export interface OpenElement {
480533 bg ?: Color ;
481534 cornerRadius ?: { tl ?: number ; tr ?: number ; bl ?: number ; br ?: number } ;
482535 border ?: {
483- color : Color ;
536+ /** Defaults to `currentcolor` (§8.5): the element's own resolved color. */
537+ color ?: Color ;
484538 bg ?: Color ;
485539 left ?: BorderSide ;
486540 right ?: BorderSide ;
@@ -640,7 +694,8 @@ type ColorFunction =
640694type NamedColor =
641695 | keyof typeof names
642696 | keyof typeof systemColors
643- | "transparent" ;
697+ | "transparent"
698+ | "currentcolor" ;
644699
645700type CheckedColor < T > = T extends number ? T
646701 : T extends string ? string extends T ? T
0 commit comments