Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/dom-style-rendering.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/ui': patch
---

Apply element styles to terminal layout, spacing, borders, and colors, including default display modes and inherited foreground colors.
97 changes: 94 additions & 3 deletions packages/ui/src/layout.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { close, open, text } from '@bomb.sh/tty';
import { describe, expect, it } from 'vitest';
import { close, open, rgba, snapshot, text, type Op } from '@bomb.sh/tty';
import { assert, describe, expect, it } from 'vitest';
import { Document } from './dom.ts';
import { Layout } from './layout.ts';

Expand Down Expand Up @@ -33,7 +33,7 @@ describe('Layout', () => {
expect([...layout.layout(child)]).toEqual([open('DOCUMENT: 1'), text('inside'), close()]);
});

it('lays out a deeply nested tree without recursive generator delegation', () => {
it('lays out a deeply nested tree without recursive traversal', () => {
const document = new Document(() => {});
const root = document.body;
const depth = 20_000;
Expand All @@ -53,6 +53,9 @@ describe('Layout', () => {
expect(iterator.next().value).toEqual(close());
}
expect(iterator.next().done).toBe(true);

root.style.display = 'inline';
expect([...new Layout(root).layout()]).toEqual([text('leaf')]);
});

it('closes active elements when iteration is returned early', () => {
Expand All @@ -71,3 +74,91 @@ describe('Layout', () => {
expect(iterator.next().done).toBe(true);
});
});
describe('sparse layout options', () => {
it('omits default layout options and saves the whole packed layout block', () => {
const document = new Document(() => {});
const { body } = document;
// Explicit defaults have the same packing requirements as undeclared defaults.
body.style.display = 'flex';
body.style.flexDirection = 'row';
body.style.gap = '0px';
body.style.paddingLeft = '0px';
body.style.borderColor = 'blue';
const ops = [...new Layout(body).layout()];
const id = rootId(ops);
const dense = [
open(id, {
layout: {
direction: 'ltr',
gap: 0,
padding: { top: 0, right: 0, bottom: 0, left: 0 },
},
}),
close(),
];
expect(ops).toEqual([open(id), close()]);
expect(packed(dense).byteLength - packed(ops).byteLength).toBe(36);
});

it('packs nondefault layout and border options identically to explicit zero sides', () => {
const document = new Document(() => {});
const { body } = document;
body.style.flexDirection = 'column';
body.style.gap = '2px';
body.style.paddingLeft = '2px';
body.style.borderStyle = 'solid';
body.style.borderColor = 'blue';
body.style.borderTopWidth = '0px';
body.style.borderRightWidth = '0px';
body.style.borderBottomWidth = '0px';
const ops = [...new Layout(body).layout()];
const id = rootId(ops);
expect(ops).toEqual([
open(id, {
layout: { direction: 'ttb', gap: 2, padding: { left: 2 } },
border: { color: rgba(0, 0, 255), left: 1 },
}),
close(),
]);
expect(packed(ops)).toEqual(
packed([
open(id, {
layout: { direction: 'ttb', gap: 2, padding: { top: 0, right: 0, bottom: 0, left: 2 } },
border: { color: rgba(0, 0, 255), top: 0, right: 0, bottom: 0, left: 1 },
}),
close(),
]),
);
});

it('omits terminal-default text color but preserves inherited packed zero', () => {
const document = new Document(() => {});
const { body } = document;
const span = document.createElement('span');
span.appendChild(document.createTextNode('inline'));
body.appendChild(document.createTextNode('direct'));
body.appendChild(span);
const layout = new Layout(body);
const ops = [...layout.layout()];
const id = rootId(ops);
expect(ops).toEqual([open(id), text('direct'), text('inline'), close()]);
body.style.color = 'transparent';
expect([...layout.layout()]).toEqual([
open(id),
text('direct', { color: 0 }),
text('inline', { color: 0 }),
close(),
]);
});
});

function rootId(ops: Op[]): string {
const root = ops[0];
assert(root && 'id' in root);
return root.id;
}

function packed(ops: Op[]): Uint8Array {
const result = snapshot(ops) as { data: Uint8Array };
return result.data;
}
192 changes: 181 additions & 11 deletions packages/ui/src/layout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { close, open, text, type Op } from '@bomb.sh/tty';
/* oxlint-disable bombshell-dev/max-params -- Layout helpers pass the element and rendering context explicitly. */
import { close, open, rgba, text, type Op } from '@bomb.sh/tty';
import { convert } from 'hueful';
import { Element, Text } from './dom.ts';
import type { StyleValues } from './style.ts';

type TTYContainerOptions = NonNullable<Parameters<typeof open>[1]>;
type TTYTextOptions = NonNullable<Parameters<typeof text>[1]>;

export class Layout {
#lastIdNumber = 0;
Expand All @@ -11,17 +17,27 @@ export class Layout {
}

/** Serialize an element to TTY Ops. */
*layout(element: Element = this.#root): IterableIterator<Op> {
yield this.#open(element);
*layout(element: Element = this.#root, inheritedColor?: number): IterableIterator<Op> {
const style = computeStyle(element);
const color = resolveColorValue(style.color.value, inheritedColor);
if (style.display.value === 'inline') {
const content = collectInlineText(element);
if (content !== '') {
yield text(content, ttyTextOptions(color));
}
return;
}

const stack = [element.childNodes[Symbol.iterator]()];
const stack = [{ children: element.childNodes[Symbol.iterator](), color }];
try {
yield this.#open(element, style, color);
let content = '';
while (stack.length > 0) {
const next = stack[stack.length - 1]!.next();
const frame = stack[stack.length - 1]!;
const next = frame.children.next();
if (next.done) {
if (content !== '') {
yield text(content);
yield text(content, ttyTextOptions(frame.color));
content = '';
}
stack.pop();
Expand All @@ -33,11 +49,20 @@ export class Layout {
content += child.data;
} else if (child instanceof Element) {
if (content !== '') {
yield text(content);
yield text(content, ttyTextOptions(frame.color));
content = '';
}
yield this.#open(child);
stack.push(child.childNodes[Symbol.iterator]());
const childStyle = computeStyle(child);
const childColor = resolveColorValue(childStyle.color.value, frame.color);
if (childStyle.display.value === 'inline') {
const inlineContent = collectInlineText(child);
if (inlineContent !== '') {
yield text(inlineContent, ttyTextOptions(childColor));
}
} else {
stack.push({ children: child.childNodes[Symbol.iterator](), color: childColor });
yield this.#open(child, childStyle, childColor);
}
}
}
} finally {
Expand All @@ -48,12 +73,157 @@ export class Layout {
}
}

#open(element: Element): Op {
#open(element: Element, style: StyleValues, color?: number): Op {
let stableId = this.#stableIds.get(element);
if (!stableId) {
stableId = `${element.nodeName}: ${this.#lastIdNumber++}`;
this.#stableIds.set(element, stableId);
}
return open(stableId);
return open(stableId, ttyContainerOptions(style, color));
}
}

/** Inline elements collect descendant text using the outer element's styles. */
function collectInlineText(element: Element): string {
let content = '';
const stack = [element.childNodes[Symbol.iterator]()];
while (stack.length > 0) {
const next = stack[stack.length - 1]!.next();
if (next.done) {
stack.pop();
} else if (next.value instanceof Text) {
content += next.value.data;
} else if (next.value instanceof Element) {
stack.push(next.value.childNodes[Symbol.iterator]());
}
}
return content;
}

/**
* Convert a color which is stored internally as a hueful `ParseResult` into
* an RGB numeric value which is what TTY speaks natively.
*/
function resolveColorValue(
value: StyleValues['color']['value'],
inheritedColor?: number,
): number | undefined {
if (value === 'currentColor') {
return inheritedColor;
}
const [r, g, b, alpha = 1] = convert(value.space, 'rgb', value.value);
const channel = (component: number): number => Math.round(Math.max(0, Math.min(255, component)));
return rgba(channel(r), channel(g), channel(b), channel(alpha * 255));
}

/**
* Map a layout container element's styles to TTY options. The mapping is
* sparse so that even though we have a completely known computed style,
* we only send through to WASM enough information to render.
*/
function ttyContainerOptions(style: StyleValues, color?: number): TTYContainerOptions {
const props: TTYContainerOptions = {};
const layout: NonNullable<typeof props.layout> = {};
if (style.flexDirection.value === 'column') {
layout.direction = 'ttb';
}
if (style.gap.value !== 0) {
layout.gap = style.gap.value;
}

let top = style.paddingTop.value;
let right = style.paddingRight.value;
let bottom = style.paddingBottom.value;
let left = style.paddingLeft.value;
const borderTop = style.borderTopWidth.value;
const borderRight = style.borderRightWidth.value;
const borderBottom = style.borderBottomWidth.value;
const borderLeft = style.borderLeftWidth.value;
if (
style.borderStyle.value === 'solid' &&
(borderTop || borderRight || borderBottom || borderLeft)
) {
const borderColor = resolveColorValue(style.borderColor.value, color);
// TTY requires an explicit border color. Keep the space when it cannot draw the border.
if (borderColor !== undefined) {
props.border = { color: borderColor };
if (borderTop !== 0) {
props.border.top = borderTop;
}
if (borderRight !== 0) {
props.border.right = borderRight;
}
if (borderBottom !== 0) {
props.border.bottom = borderBottom;
}
if (borderLeft !== 0) {
props.border.left = borderLeft;
}
} else {
// TTY cannot draw borders with the terminal-default foreground yet.
// Reserve their space with padding until https://github.com/bombshell-dev/tty/issues/155
// is implemented; then emit the border and remove this fallback.
top += borderTop;
right += borderRight;
bottom += borderBottom;
left += borderLeft;
}
}

if (top || right || bottom || left) {
layout.padding = {};
if (top !== 0) {
layout.padding.top = top;
}
if (right !== 0) {
layout.padding.right = right;
}
if (bottom !== 0) {
layout.padding.bottom = bottom;
}
if (left !== 0) {
layout.padding.left = left;
}
}
// Even an empty layout object makes TTY pack the entire layout block.
if (Object.keys(layout).length > 0) {
props.layout = layout;
}
return props;
}

function ttyTextOptions(color?: number): TTYTextOptions {
return color === undefined ? {} : { color };
}

function computeStyle(element: Element): StyleValues {
const defaults = computeDefaultStyles(element);
return applyInlineStyles(element, defaults);
}

function computeDefaultStyles(element: Element): StyleValues {
const display =
element.namespaceURI === 'http://www.w3.org/1999/xhtml' && element.localName === 'span'
? 'inline'
: 'flex';
return {
color: { value: 'currentColor', representation: 'currentColor' },
display: { value: display, representation: display },
flexDirection: { value: 'row', representation: 'row' },
gap: { value: 0, representation: '0px' },
paddingTop: { value: 0, representation: '0px' },
paddingRight: { value: 0, representation: '0px' },
paddingBottom: { value: 0, representation: '0px' },
paddingLeft: { value: 0, representation: '0px' },
borderStyle: { value: 'none', representation: 'none' },
borderTopWidth: { value: 1, representation: '1px' },
borderRightWidth: { value: 1, representation: '1px' },
borderBottomWidth: { value: 1, representation: '1px' },
borderLeftWidth: { value: 1, representation: '1px' },
borderColor: { value: 'currentColor', representation: 'currentColor' },
};
}

function applyInlineStyles(element: Element, defaults: StyleValues): StyleValues {
return { ...defaults, ...element.style.values };
}
Loading
Loading