xonecas.io
70160
Ce

cel-tui / commit 2c6d4ea

feat(core): layout-driven painting with text wrapping and clipping

- Painter rewritten to use LayoutNode rects from layout engine
- Text painting: content, multiline (\n), repeat/fill, word-wrap
- Style painting: fgColor, bgColor, bold, italic, underline
- Hard clipping at rect boundaries
- cel.ts pipeline updated: render fn → layout → paint → emit
- 13 paint tests (text rendering, positioning, nested layout)
- 101 total tests, all passing

xonecas2026-04-03 2c6d4eacaeef255787e669bbca0f5367746761f1 parent bcc6f88

3 files changed

packages/core/src/cel.ts+5−3
@@ -1,8 +1,9 @@
11 import type { Node } from "@cel-tui/types";
22 import { CellBuffer } from "./cell-buffer.js";
33 import { emitBuffer } from "./emitter.js";
4+import { layout } from "./layout.js";
5+import { paint } from "./paint.js";
46 import type { Terminal } from "./terminal.js";
5-import { paintNode } from "./paint.js";
67
78 type RenderFn = () => Node | Node[];
89
@@ -33,9 +34,10 @@ function doRender(): void {
3334 const tree = renderFn();
3435 const layers = Array.isArray(tree) ? tree : [tree];
3536
36- // Paint each layer into the buffer
37+ // Layout and paint each layer into the buffer
3738 for (const layer of layers) {
38- paintNode(layer, currentBuffer, 0, 0, width, height);
39+ const layoutTree = layout(layer, width, height);
40+ paint(layoutTree, currentBuffer);
3941 }
4042
4143 // Emit to terminal
packages/core/src/paint.test.ts+185−0
@@ -0,0 +1,185 @@
1+import { describe, expect, test } from "bun:test";
2+import { CellBuffer } from "./cell-buffer.js";
3+import { layout } from "./layout.js";
4+import { paint } from "./paint.js";
5+import { VStack, HStack } from "./primitives/stacks.js";
6+import { Text } from "./primitives/text.js";
7+
8+/** Read a row from the buffer as a string (trimming trailing spaces). */
9+function readRow(buf: CellBuffer, y: number): string {
10+ let row = "";
11+ for (let x = 0; x < buf.width; x++) {
12+ row += buf.get(x, y).char;
13+ }
14+ return row.trimEnd();
15+}
16+
17+/** Read all rows as strings. */
18+function readAll(buf: CellBuffer): string[] {
19+ const rows: string[] = [];
20+ for (let y = 0; y < buf.height; y++) {
21+ rows.push(readRow(buf, y));
22+ }
23+ return rows;
24+}
25+
26+describe("paint", () => {
27+ describe("Text rendering", () => {
28+ test("paints text at correct position", () => {
29+ const node = VStack({ width: 20, height: 3 }, [Text("Hello")]);
30+ const ln = layout(node, 20, 3);
31+ const buf = new CellBuffer(20, 3);
32+ paint(ln, buf);
33+ expect(readRow(buf, 0)).toBe("Hello");
34+ });
35+
36+ test("clips text at rect boundary", () => {
37+ const node = VStack({ width: 5, height: 1 }, [Text("Hello, world!")]);
38+ const ln = layout(node, 5, 1);
39+ const buf = new CellBuffer(5, 1);
40+ paint(ln, buf);
41+ expect(readRow(buf, 0)).toBe("Hello");
42+ });
43+
44+ test("paints multiline text from newlines", () => {
45+ const node = VStack({ width: 20, height: 5 }, [
46+ Text("line1\nline2\nline3"),
47+ ]);
48+ const ln = layout(node, 20, 5);
49+ const buf = new CellBuffer(20, 5);
50+ paint(ln, buf);
51+ expect(readRow(buf, 0)).toBe("line1");
52+ expect(readRow(buf, 1)).toBe("line2");
53+ expect(readRow(buf, 2)).toBe("line3");
54+ });
55+
56+ test("repeat fill fills the width", () => {
57+ const node = VStack({ width: 10, height: 1 }, [
58+ Text("-", { repeat: "fill" }),
59+ ]);
60+ const ln = layout(node, 10, 1);
61+ const buf = new CellBuffer(10, 1);
62+ paint(ln, buf);
63+ expect(readRow(buf, 0)).toBe("----------");
64+ });
65+
66+ test("repeat number repeats exact count", () => {
67+ const node = VStack({ width: 20, height: 1 }, [
68+ Text("ab", { repeat: 3 }),
69+ ]);
70+ const ln = layout(node, 20, 1);
71+ const buf = new CellBuffer(20, 1);
72+ paint(ln, buf);
73+ expect(readRow(buf, 0)).toBe("ababab");
74+ });
75+
76+ test("applies foreground color", () => {
77+ const node = VStack({ width: 10, height: 1 }, [
78+ Text("Hi", { fgColor: "red" }),
79+ ]);
80+ const ln = layout(node, 10, 1);
81+ const buf = new CellBuffer(10, 1);
82+ paint(ln, buf);
83+ expect(buf.get(0, 0).fgColor).toBe("red");
84+ expect(buf.get(1, 0).fgColor).toBe("red");
85+ });
86+
87+ test("applies background color", () => {
88+ const node = VStack({ width: 10, height: 1 }, [
89+ Text("Hi", { bgColor: "blue" }),
90+ ]);
91+ const ln = layout(node, 10, 1);
92+ const buf = new CellBuffer(10, 1);
93+ paint(ln, buf);
94+ expect(buf.get(0, 0).bgColor).toBe("blue");
95+ });
96+
97+ test("applies bold/italic/underline", () => {
98+ const node = VStack({ width: 10, height: 1 }, [
99+ Text("X", { bold: true, italic: true, underline: true }),
100+ ]);
101+ const ln = layout(node, 10, 1);
102+ const buf = new CellBuffer(10, 1);
103+ paint(ln, buf);
104+ const cell = buf.get(0, 0);
105+ expect(cell.bold).toBe(true);
106+ expect(cell.italic).toBe(true);
107+ expect(cell.underline).toBe(true);
108+ });
109+ });
110+
111+ describe("layout-driven positioning", () => {
112+ test("VStack children paint at correct vertical positions", () => {
113+ const node = VStack({ width: 20, height: 10 }, [
114+ Text("Header"),
115+ Text("Body"),
116+ ]);
117+ const ln = layout(node, 20, 10);
118+ const buf = new CellBuffer(20, 10);
119+ paint(ln, buf);
120+ expect(readRow(buf, 0)).toBe("Header");
121+ expect(readRow(buf, 1)).toBe("Body");
122+ });
123+
124+ test("HStack children paint at correct horizontal positions", () => {
125+ const node = HStack({ width: 20, height: 1 }, [
126+ VStack({ width: 8 }, [Text("Left")]),
127+ VStack({ width: 12 }, [Text("Right")]),
128+ ]);
129+ const ln = layout(node, 20, 1);
130+ const buf = new CellBuffer(20, 1);
131+ paint(ln, buf);
132+ expect(readRow(buf, 0)).toBe("Left Right");
133+ });
134+
135+ test("flex children fill space correctly", () => {
136+ const node = HStack({ width: 20, height: 1 }, [
137+ VStack({ width: 5 }, [Text("A")]),
138+ VStack({ flex: 1 }, [Text("B")]),
139+ ]);
140+ const ln = layout(node, 20, 1);
141+ const buf = new CellBuffer(20, 1);
142+ paint(ln, buf);
143+ expect(buf.get(0, 0).char).toBe("A");
144+ expect(buf.get(5, 0).char).toBe("B");
145+ });
146+
147+ test("padding offsets children", () => {
148+ const node = VStack({ width: 20, height: 5, padding: { x: 2, y: 1 } }, [
149+ Text("Hi"),
150+ ]);
151+ const ln = layout(node, 20, 5);
152+ const buf = new CellBuffer(20, 5);
153+ paint(ln, buf);
154+ // Row 0 should be empty (padding), text starts at row 1, col 2
155+ expect(readRow(buf, 0)).toBe("");
156+ expect(buf.get(2, 1).char).toBe("H");
157+ expect(buf.get(3, 1).char).toBe("i");
158+ });
159+ });
160+
161+ describe("nested layout", () => {
162+ test("editor-like layout paints correctly", () => {
163+ const node = HStack({ width: 30, height: 5 }, [
164+ VStack({ width: 10 }, [Text("sidebar")]),
165+ VStack({ flex: 1 }, [
166+ VStack({ height: 1 }, [Text("tab bar")]),
167+ VStack({ flex: 1 }, [Text("editor")]),
168+ VStack({ height: 1 }, [Text("status")]),
169+ ]),
170+ ]);
171+ const ln = layout(node, 30, 5);
172+ const buf = new CellBuffer(30, 5);
173+ paint(ln, buf);
174+
175+ // Sidebar at col 0
176+ expect(readRow(buf, 0).startsWith("sidebar")).toBe(true);
177+ // Tab bar at col 10, row 0
178+ expect(buf.get(10, 0).char).toBe("t");
179+ // Editor at col 10, row 1
180+ expect(buf.get(10, 1).char).toBe("e");
181+ // Status at col 10, row 4
182+ expect(buf.get(10, 4).char).toBe("s");
183+ });
184+ });
185+});
packages/core/src/paint.ts+77−73
@@ -1,42 +1,41 @@
1-import type { Node, Color } from "@cel-tui/types";
1+import type { Color } from "@cel-tui/types";
22 import type { Cell } from "./cell-buffer.js";
33 import { CellBuffer } from "./cell-buffer.js";
4+import type { LayoutNode, Rect } from "./layout.js";
45
56 /**
6- * Paint a node into the cell buffer within the given rect.
7+ * Paint a laid-out tree into a cell buffer.
78 *
8- * This is a minimal implementation for phase 2 — handles Text nodes
9- * with basic styling. Layout engine will replace the hardcoded rect
10- * passing in phase 3.
9+ * Walks the {@link LayoutNode} tree and writes styled cells into the
10+ * buffer within each node's computed rect. Content is clipped at
11+ * rect boundaries.
1112 *
12- * @param node - The node to paint.
13+ * @param root - The root of the laid-out tree (from {@link layout}).
1314 * @param buf - Target cell buffer.
14- * @param x - Left edge of the rect.
15- * @param y - Top edge of the rect.
16- * @param w - Width of the rect.
17- * @param h - Height of the rect.
1815 */
19-export function paintNode(
20- node: Node,
21- buf: CellBuffer,
22- x: number,
23- y: number,
24- w: number,
25- h: number,
26-): void {
16+export function paint(root: LayoutNode, buf: CellBuffer): void {
17+ paintLayoutNode(root, buf);
18+}
19+
20+function paintLayoutNode(ln: LayoutNode, buf: CellBuffer): void {
21+ const { node, rect } = ln;
22+
2723 switch (node.type) {
2824 case "text":
29- paintText(node.content, node.props, buf, x, y, w, h);
30- break;
31- case "vstack":
32- paintVStack(node, buf, x, y, w, h);
33- break;
34- case "hstack":
35- paintHStack(node, buf, x, y, w, h);
25+ paintText(node.content, node.props, rect, buf);
3626 break;
3727 case "textinput":
3828 // TODO: implement in phase 7
3929 break;
30+ case "vstack":
31+ case "hstack":
32+ // Containers just paint their children
33+ break;
34+ }
35+
36+ // Recurse into children
37+ for (const child of ln.children) {
38+ paintLayoutNode(child, buf);
4039 }
4140 }
4241
@@ -62,14 +61,22 @@ function makeCell(
6261
6362 function paintText(
6463 content: string,
65- props: Node extends { type: "text"; props: infer P } ? P : never,
64+ props: {
65+ repeat?: number | "fill";
66+ wrap?: "none" | "word";
67+ fgColor?: Color;
68+ bgColor?: Color;
69+ bold?: boolean;
70+ italic?: boolean;
71+ underline?: boolean;
72+ },
73+ rect: Rect,
6674 buf: CellBuffer,
67- x: number,
68- y: number,
69- w: number,
70- h: number,
7175 ): void {
72- // Handle repeat: "fill"
76+ const { x, y, width: w, height: h } = rect;
77+ if (w <= 0 || h <= 0) return;
78+
79+ // Resolve repeat
7380 let text = content;
7481 if (props.repeat === "fill" && content.length > 0) {
7582 text = content.repeat(Math.ceil(w / content.length)).slice(0, w);
@@ -77,9 +84,24 @@ function paintText(
7784 text = content.repeat(props.repeat);
7885 }
7986
80- // Split into lines (preserve \n)
81- const lines = text.split("\n");
87+ // Split into lines
88+ const rawLines = text.split("\n");
8289
90+ // Word-wrap if enabled
91+ const lines: string[] = [];
92+ if (props.wrap === "word") {
93+ for (const rawLine of rawLines) {
94+ if (rawLine.length <= w) {
95+ lines.push(rawLine);
96+ } else {
97+ wrapLine(rawLine, w, lines);
98+ }
99+ }
100+ } else {
101+ lines.push(...rawLines);
102+ }
103+
104+ // Paint lines, clipped to rect
83105 for (let row = 0; row < lines.length && row < h; row++) {
84106 const line = lines[row]!;
85107 for (let col = 0; col < line.length && col < w; col++) {
@@ -89,50 +111,32 @@ function paintText(
89111 }
90112
91113 /**
92- * Minimal VStack painter — divides height equally among children.
93- * Will be replaced by the proper layout engine in phase 3.
114+ * Simple word-wrap: break a line into multiple lines at word boundaries.
94115 */
95-function paintVStack(
96- node: Extract<Node, { type: "vstack" }>,
97- buf: CellBuffer,
98- x: number,
99- y: number,
100- w: number,
101- h: number,
102-): void {
103- if (node.children.length === 0) return;
104-
105- // Simple equal distribution for now
106- const childHeight = Math.floor(h / node.children.length);
107- let currentY = y;
116+function wrapLine(line: string, width: number, out: string[]): void {
117+ if (width <= 0) return;
108118
109- for (let i = 0; i < node.children.length; i++) {
110- const ch = i === node.children.length - 1 ? y + h - currentY : childHeight;
111- paintNode(node.children[i]!, buf, x, currentY, w, ch);
112- currentY += ch;
113- }
114-}
119+ let current = "";
120+ const words = line.split(" ");
115121
116-/**
117- * Minimal HStack painter — divides width equally among children.
118- * Will be replaced by the proper layout engine in phase 3.
119- */
120-function paintHStack(
121- node: Extract<Node, { type: "hstack" }>,
122- buf: CellBuffer,
123- x: number,
124- y: number,
125- w: number,
126- h: number,
127-): void {
128- if (node.children.length === 0) return;
122+ for (const word of words) {
123+ if (current.length === 0) {
124+ current = word;
125+ } else if (current.length + 1 + word.length <= width) {
126+ current += " " + word;
127+ } else {
128+ out.push(current);
129+ current = word;
130+ }
129131
130- const childWidth = Math.floor(w / node.children.length);
131- let currentX = x;
132+ // Handle words longer than width (break by character)
133+ while (current.length > width) {
134+ out.push(current.slice(0, width));
135+ current = current.slice(width);
136+ }
137+ }
132138
133- for (let i = 0; i < node.children.length; i++) {
134- const cw = i === node.children.length - 1 ? x + w - currentX : childWidth;
135- paintNode(node.children[i]!, buf, currentX, y, cw, h);
136- currentX += cw;
139+ if (current.length > 0) {
140+ out.push(current);
137141 }
138142 }