@alchemy.run/sigil 0.0.0-alpha.1 → 0.0.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +499 -313
- package/THIRD_PARTY_NOTICES.md +70 -23
- package/dist/Text-DV9CuzAT.d.ts +452 -0
- package/dist/ansi.d.ts +217 -0
- package/dist/ansi.js +87 -0
- package/dist/capabilities.d.ts +5 -0
- package/dist/capabilities.js +3 -0
- package/dist/cell-_ZVhbfl0.js +44 -0
- package/dist/color-CkbalRqK.js +2 -0
- package/dist/color-policy-BAC9-TZX.js +592 -0
- package/dist/color-policy-CCxuHIdD.d.ts +22 -0
- package/dist/color-profile-CyeHnG1T.d.ts +97 -0
- package/dist/color-profile-DHhQHY55.js +36 -0
- package/dist/color.d.ts +21 -0
- package/dist/color.js +3 -0
- package/dist/cursor-position-D2LAkRG0.d.ts +7 -0
- package/dist/detect-B3dL4Q11.js +374 -0
- package/dist/detect-Db6GbKOm.d.ts +195 -0
- package/dist/{devtools-QpCMm9JH.mjs → devtools-DbthxoD1.js} +23 -24
- package/dist/env-YVw64yZS.js +9 -0
- package/dist/escapes-CB_6CWOE.d.ts +72 -0
- package/dist/geometry-BxXOzJgo.d.ts +11 -0
- package/dist/index-D48vQhhe.d.ts +21 -0
- package/dist/index-DDVME65c.d.ts +919 -0
- package/dist/index.d.ts +1310 -0
- package/dist/index.js +3209 -0
- package/dist/osc-BFKKSqpg.js +71 -0
- package/dist/osc-Cn0fw77g.d.ts +23 -0
- package/dist/paint-Cx-zC_sX.d.ts +81 -0
- package/dist/query-BNc2B8GD.d.ts +152 -0
- package/dist/router.d.ts +392 -0
- package/dist/router.js +709 -0
- package/dist/sample-Cqw1bjUL.js +445 -0
- package/dist/screen-CgC2WlVM.d.ts +49 -0
- package/dist/screen-CiPytswf.js +342 -0
- package/dist/screen.d.ts +5 -0
- package/dist/screen.js +5 -0
- package/dist/semantic-text-style-DIMzC7xt.js +91 -0
- package/dist/serialize-BTkAZgw1.js +79 -0
- package/dist/session-DDQ5V300.js +723 -0
- package/dist/sgr-BhwaWAJB.js +246 -0
- package/dist/store-C1P5fOUi.d.ts +72 -0
- package/dist/string-width-CijQwpIk.js +69 -0
- package/dist/strip-BvU4toXG.js +6 -0
- package/dist/terminal.d.ts +121 -0
- package/dist/terminal.js +2 -0
- package/dist/tokenize-AjqbvtiT.js +1242 -0
- package/dist/tokenize-Dx1y_l5H.d.ts +57 -0
- package/dist/truncate-D31fhU6i.js +562 -0
- package/dist/use-focus-CcCIdCWA.js +1337 -0
- package/dist/yoga-5jKhYCJC.js +3465 -0
- package/dist/yoga.d.ts +2 -0
- package/dist/yoga.js +2 -0
- package/package.json +70 -24
- package/src/ansi/chalk.ts +138 -0
- package/src/ansi/cursor.ts +46 -0
- package/src/ansi/east-asian-width.ts +259 -0
- package/src/ansi/escapes.ts +120 -0
- package/src/ansi/graphemes.ts +8 -0
- package/src/ansi/hyperlink.ts +44 -0
- package/src/ansi/index.ts +27 -0
- package/src/ansi/osc.ts +77 -0
- package/src/ansi/sgr.ts +234 -0
- package/src/ansi/slice.ts +43 -0
- package/src/ansi/string-width.ts +121 -0
- package/src/ansi/strip.ts +13 -0
- package/src/ansi/tokenize.ts +380 -0
- package/src/ansi/truncate.ts +196 -0
- package/src/ansi/wrap.ts +765 -0
- package/src/ansi-tokenizer.ts +510 -0
- package/src/capabilities/color-policy.ts +34 -0
- package/src/capabilities/detect.ts +608 -0
- package/src/capabilities/index.ts +37 -0
- package/src/capabilities/query.ts +679 -0
- package/src/capabilities/store.ts +394 -0
- package/src/code-excerpt.ts +39 -0
- package/src/color/index.ts +3 -0
- package/src/color/paint.ts +169 -0
- package/src/color/palette.ts +48 -0
- package/src/color/sample.ts +323 -0
- package/src/color.ts +1 -0
- package/src/components/AccessibilityContext.ts +5 -0
- package/src/components/AnimationContext.ts +24 -0
- package/src/components/AnsiText.tsx +42 -0
- package/src/components/App.tsx +878 -0
- package/src/components/AppContext.ts +111 -0
- package/src/components/BackgroundContext.ts +7 -0
- package/src/components/Box.tsx +100 -0
- package/src/components/CursorContext.ts +19 -0
- package/src/components/ErrorBoundary.tsx +39 -0
- package/src/components/ErrorOverview.tsx +135 -0
- package/src/components/FocusContext.ts +30 -0
- package/src/components/Hyperlink.tsx +56 -0
- package/src/components/Newline.tsx +16 -0
- package/src/components/Spacer.tsx +11 -0
- package/src/components/Static.tsx +60 -0
- package/src/components/StderrContext.ts +24 -0
- package/src/components/StdinContext.ts +48 -0
- package/src/components/StdoutContext.ts +26 -0
- package/src/components/TerminalOscContext.ts +25 -0
- package/src/components/Text.tsx +122 -0
- package/src/components/Transform.tsx +38 -0
- package/src/components/VirtualList.tsx +128 -0
- package/src/cursor-position.ts +103 -0
- package/src/devtools.ts +103 -0
- package/src/dom.ts +301 -0
- package/src/env.ts +12 -0
- package/src/get-max-width.ts +11 -0
- package/src/global.d.ts +38 -0
- package/src/glyphs.ts +99 -0
- package/src/hooks/use-animation.ts +142 -0
- package/src/hooks/use-app.ts +8 -0
- package/src/hooks/use-box-metrics.ts +134 -0
- package/src/hooks/use-capabilities.ts +73 -0
- package/src/hooks/use-cursor.ts +33 -0
- package/src/hooks/use-focus-manager.ts +62 -0
- package/src/hooks/use-focus.ts +82 -0
- package/src/hooks/use-input.ts +267 -0
- package/src/hooks/use-is-screen-reader-enabled.ts +12 -0
- package/src/hooks/use-paste.ts +78 -0
- package/src/hooks/use-stderr.ts +8 -0
- package/src/hooks/use-stdin.ts +10 -0
- package/src/hooks/use-stdout.ts +8 -0
- package/src/hooks/use-terminal-osc.ts +59 -0
- package/src/hooks/use-virtual-scroll.ts +84 -0
- package/src/hooks/use-window-size.ts +37 -0
- package/src/index.ts +96 -0
- package/src/ink.tsx +1452 -0
- package/src/input-parser.ts +303 -0
- package/src/instances.ts +9 -0
- package/src/kitty-keyboard.ts +185 -0
- package/src/measure-element.ts +62 -0
- package/src/measure-text.ts +31 -0
- package/src/paint-tree.ts +220 -0
- package/src/parse-keypress.ts +515 -0
- package/src/parse-stack-line.ts +138 -0
- package/src/patch-console.ts +106 -0
- package/src/quick-lru.ts +85 -0
- package/src/reconciler.ts +425 -0
- package/src/render-background.ts +59 -0
- package/src/render-border.ts +167 -0
- package/src/render-frame.ts +83 -0
- package/src/render-to-string.ts +146 -0
- package/src/render.ts +284 -0
- package/src/router/components.tsx +343 -0
- package/src/router/context.ts +41 -0
- package/src/router/history.ts +194 -0
- package/src/router/hooks.tsx +391 -0
- package/src/router/index.ts +34 -0
- package/src/router/matcher.ts +571 -0
- package/src/sanitize-ansi.ts +33 -0
- package/src/screen/ansi.ts +184 -0
- package/src/screen/canvas.ts +160 -0
- package/src/screen/cell.ts +138 -0
- package/src/screen/color-profile.ts +47 -0
- package/src/screen/geometry.ts +9 -0
- package/src/screen/index.ts +6 -0
- package/src/screen/screen.ts +305 -0
- package/src/screen/serialize.ts +129 -0
- package/src/screen.ts +1 -0
- package/src/semantic-text-style.ts +118 -0
- package/src/signal-exit.ts +106 -0
- package/src/squash-text-nodes.ts +37 -0
- package/src/stream.ts +28 -0
- package/src/structured-text.ts +325 -0
- package/src/styles.ts +753 -0
- package/src/terminal/index.ts +2 -0
- package/src/terminal/inline-presenter.ts +120 -0
- package/src/terminal/input.ts +92 -0
- package/src/terminal/render-scheduler.ts +37 -0
- package/src/terminal/screen-presenter.ts +242 -0
- package/src/terminal/session.ts +408 -0
- package/src/terminal-size.ts +58 -0
- package/src/terminal.ts +1 -0
- package/src/testing/browser.ts +588 -0
- package/src/testing/emulators.ts +205 -0
- package/src/testing/explorer-app/index.html +12 -0
- package/src/testing/explorer-app/main.ts +381 -0
- package/src/testing/explorer-app/style.css +194 -0
- package/src/testing/explorer-app/tsconfig.json +15 -0
- package/src/testing/explorer-app/vite-env.d.ts +1 -0
- package/src/testing/index.ts +26 -0
- package/src/testing/keys.ts +56 -0
- package/src/testing/live.ts +85 -0
- package/src/testing/matchers.ts +70 -0
- package/src/testing/public.ts +94 -0
- package/src/testing/terminal.ts +361 -0
- package/src/testing/vitest.ts +157 -0
- package/src/throttle.ts +73 -0
- package/src/transform-adapter.ts +14 -0
- package/src/types.ts +10 -0
- package/src/virtual-scroll.ts +133 -0
- package/src/wrap-text.ts +54 -0
- package/src/yoga/config.ts +57 -0
- package/src/yoga/core/absoluteLayout.ts +626 -0
- package/src/yoga/core/baseline.ts +66 -0
- package/src/yoga/core/cache.ts +136 -0
- package/src/yoga/core/calculateLayout.ts +2926 -0
- package/src/yoga/core/config.ts +104 -0
- package/src/yoga/core/flexLine.ts +177 -0
- package/src/yoga/core/helpers.ts +293 -0
- package/src/yoga/core/layoutResults.ts +167 -0
- package/src/yoga/core/node.ts +611 -0
- package/src/yoga/core/numeric.ts +44 -0
- package/src/yoga/core/pixelGrid.ts +151 -0
- package/src/yoga/core/style.ts +887 -0
- package/src/yoga/core/types.ts +224 -0
- package/src/yoga/generated/YGEnums.ts +263 -0
- package/src/yoga/index.ts +19 -0
- package/src/yoga/node.ts +1140 -0
- package/dist/index.d.mts +0 -2379
- package/dist/index.mjs +0 -10072
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { graphemes } from "#/ansi/graphemes.ts";
|
|
2
|
+
import { stringWidth } from "#/ansi/string-width.ts";
|
|
3
|
+
import type { Paint, PaintContext } from "#/color/paint.ts";
|
|
4
|
+
import { samplePaint } from "#/color/sample.ts";
|
|
5
|
+
import { type DOMNode } from "#/dom.ts";
|
|
6
|
+
import { BOXES } from "#/glyphs.ts";
|
|
7
|
+
import type { Canvas } from "#/screen/canvas.ts";
|
|
8
|
+
import type { Rect } from "#/screen/geometry.ts";
|
|
9
|
+
import { cellAttributes, createCell, type Cell } from "#/screen/index.ts";
|
|
10
|
+
|
|
11
|
+
const stylePiece = (
|
|
12
|
+
segment: string,
|
|
13
|
+
x: number,
|
|
14
|
+
y: number,
|
|
15
|
+
bounds: Rect,
|
|
16
|
+
context: PaintContext,
|
|
17
|
+
fg?: Paint,
|
|
18
|
+
bg?: Paint,
|
|
19
|
+
dim?: boolean,
|
|
20
|
+
): Cell[] => {
|
|
21
|
+
let column = x;
|
|
22
|
+
return [...graphemes(segment)].map((grapheme) => {
|
|
23
|
+
const foreground = fg ? samplePaint(fg, column, y, bounds, context) : undefined;
|
|
24
|
+
const background = bg ? samplePaint(bg, column, y, bounds, context) : undefined;
|
|
25
|
+
const width = Math.max(1, stringWidth(grapheme));
|
|
26
|
+
column += width;
|
|
27
|
+
return createCell(grapheme, width, {
|
|
28
|
+
...(foreground ? { foreground } : {}),
|
|
29
|
+
...(background ? { background } : {}),
|
|
30
|
+
underline: "none",
|
|
31
|
+
attributes: dim ? cellAttributes.faint : cellAttributes.none,
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const renderBorder = (x: number, y: number, node: DOMNode, output: Canvas): void => {
|
|
37
|
+
if (node.style.borderStyle) {
|
|
38
|
+
const width = node.yogaNode!.getComputedWidth();
|
|
39
|
+
const height = node.yogaNode!.getComputedHeight();
|
|
40
|
+
const bounds = { x, y, width, height };
|
|
41
|
+
const box =
|
|
42
|
+
typeof node.style.borderStyle === "string"
|
|
43
|
+
? BOXES[node.style.borderStyle]
|
|
44
|
+
: node.style.borderStyle;
|
|
45
|
+
|
|
46
|
+
const topBorderColor = node.style.borderTopColor ?? node.style.borderColor;
|
|
47
|
+
const bottomBorderColor = node.style.borderBottomColor ?? node.style.borderColor;
|
|
48
|
+
const leftBorderColor = node.style.borderLeftColor ?? node.style.borderColor;
|
|
49
|
+
const rightBorderColor = node.style.borderRightColor ?? node.style.borderColor;
|
|
50
|
+
|
|
51
|
+
const topBorderBackgroundColor =
|
|
52
|
+
node.style.borderTopBackgroundColor ?? node.style.borderBackgroundColor;
|
|
53
|
+
const bottomBorderBackgroundColor =
|
|
54
|
+
node.style.borderBottomBackgroundColor ?? node.style.borderBackgroundColor;
|
|
55
|
+
const leftBorderBackgroundColor =
|
|
56
|
+
node.style.borderLeftBackgroundColor ?? node.style.borderBackgroundColor;
|
|
57
|
+
const rightBorderBackgroundColor =
|
|
58
|
+
node.style.borderRightBackgroundColor ?? node.style.borderBackgroundColor;
|
|
59
|
+
|
|
60
|
+
const dimTopBorderColor = node.style.borderTopDimColor ?? node.style.borderDimColor;
|
|
61
|
+
|
|
62
|
+
const dimBottomBorderColor = node.style.borderBottomDimColor ?? node.style.borderDimColor;
|
|
63
|
+
|
|
64
|
+
const dimLeftBorderColor = node.style.borderLeftDimColor ?? node.style.borderDimColor;
|
|
65
|
+
|
|
66
|
+
const dimRightBorderColor = node.style.borderRightDimColor ?? node.style.borderDimColor;
|
|
67
|
+
|
|
68
|
+
const showTopBorder = node.style.borderTop !== false;
|
|
69
|
+
const showBottomBorder = node.style.borderBottom !== false;
|
|
70
|
+
const showLeftBorder = node.style.borderLeft !== false;
|
|
71
|
+
const showRightBorder = node.style.borderRight !== false;
|
|
72
|
+
|
|
73
|
+
const contentWidth = width - (showLeftBorder ? 1 : 0) - (showRightBorder ? 1 : 0);
|
|
74
|
+
|
|
75
|
+
const topBorder = showTopBorder
|
|
76
|
+
? (showLeftBorder ? box.topLeft : "") +
|
|
77
|
+
box.top.repeat(contentWidth) +
|
|
78
|
+
(showRightBorder ? box.topRight : "")
|
|
79
|
+
: undefined;
|
|
80
|
+
|
|
81
|
+
let verticalBorderHeight = height;
|
|
82
|
+
|
|
83
|
+
if (showTopBorder) {
|
|
84
|
+
verticalBorderHeight -= 1;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (showBottomBorder) {
|
|
88
|
+
verticalBorderHeight -= 1;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const offsetY = showTopBorder ? 1 : 0;
|
|
92
|
+
|
|
93
|
+
const bottomBorder = showBottomBorder
|
|
94
|
+
? (showLeftBorder ? box.bottomLeft : "") +
|
|
95
|
+
box.bottom.repeat(contentWidth) +
|
|
96
|
+
(showRightBorder ? box.bottomRight : "")
|
|
97
|
+
: undefined;
|
|
98
|
+
|
|
99
|
+
if (topBorder) {
|
|
100
|
+
output.writeCells(x, y, [
|
|
101
|
+
stylePiece(
|
|
102
|
+
topBorder,
|
|
103
|
+
x,
|
|
104
|
+
y,
|
|
105
|
+
bounds,
|
|
106
|
+
output.paintContext,
|
|
107
|
+
topBorderColor,
|
|
108
|
+
topBorderBackgroundColor,
|
|
109
|
+
dimTopBorderColor,
|
|
110
|
+
),
|
|
111
|
+
]);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (showLeftBorder) {
|
|
115
|
+
output.writeCells(
|
|
116
|
+
x,
|
|
117
|
+
y + offsetY,
|
|
118
|
+
Array.from({ length: verticalBorderHeight }, (_, row) =>
|
|
119
|
+
stylePiece(
|
|
120
|
+
box.left,
|
|
121
|
+
x,
|
|
122
|
+
y + offsetY + row,
|
|
123
|
+
bounds,
|
|
124
|
+
output.paintContext,
|
|
125
|
+
leftBorderColor,
|
|
126
|
+
leftBorderBackgroundColor,
|
|
127
|
+
dimLeftBorderColor,
|
|
128
|
+
),
|
|
129
|
+
),
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (showRightBorder) {
|
|
134
|
+
output.writeCells(
|
|
135
|
+
x + width - 1,
|
|
136
|
+
y + offsetY,
|
|
137
|
+
Array.from({ length: verticalBorderHeight }, (_, row) =>
|
|
138
|
+
stylePiece(
|
|
139
|
+
box.right,
|
|
140
|
+
x + width - 1,
|
|
141
|
+
y + offsetY + row,
|
|
142
|
+
bounds,
|
|
143
|
+
output.paintContext,
|
|
144
|
+
rightBorderColor,
|
|
145
|
+
rightBorderBackgroundColor,
|
|
146
|
+
dimRightBorderColor,
|
|
147
|
+
),
|
|
148
|
+
),
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (bottomBorder) {
|
|
153
|
+
output.writeCells(x, y + height - 1, [
|
|
154
|
+
stylePiece(
|
|
155
|
+
bottomBorder,
|
|
156
|
+
x,
|
|
157
|
+
y + height - 1,
|
|
158
|
+
bounds,
|
|
159
|
+
output.paintContext,
|
|
160
|
+
bottomBorderColor,
|
|
161
|
+
bottomBorderBackgroundColor,
|
|
162
|
+
dimBottomBorderColor,
|
|
163
|
+
),
|
|
164
|
+
]);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { PaintContext } from "#/color/paint.ts";
|
|
2
|
+
import { type DOMElement } from "#/dom.ts";
|
|
3
|
+
import { paintTree, renderAccessibleText } from "#/paint-tree.ts";
|
|
4
|
+
import { Canvas } from "#/screen/canvas.ts";
|
|
5
|
+
import { type ColorProfile } from "#/screen/index.ts";
|
|
6
|
+
import type { Screen } from "#/screen/screen.ts";
|
|
7
|
+
|
|
8
|
+
type Result = {
|
|
9
|
+
screen?: Screen;
|
|
10
|
+
staticScreen?: Screen;
|
|
11
|
+
accessibleText?: string;
|
|
12
|
+
staticAccessibleText?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type RendererOptions = {
|
|
16
|
+
colorProfile?: ColorProfile;
|
|
17
|
+
paintContext?: PaintContext;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const renderDimension = (value: number): number =>
|
|
21
|
+
Number.isFinite(value) ? Math.max(0, Math.ceil(value)) : 0;
|
|
22
|
+
|
|
23
|
+
export const renderFrame = (
|
|
24
|
+
node: DOMElement,
|
|
25
|
+
isScreenReaderEnabled: boolean,
|
|
26
|
+
options: RendererOptions = {},
|
|
27
|
+
): Result => {
|
|
28
|
+
if (node.yogaNode) {
|
|
29
|
+
if (isScreenReaderEnabled) {
|
|
30
|
+
const output = renderAccessibleText(node, {
|
|
31
|
+
skipStaticElements: true,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
let staticOutput = "";
|
|
35
|
+
|
|
36
|
+
if (node.staticNode) {
|
|
37
|
+
staticOutput = renderAccessibleText(node.staticNode, {
|
|
38
|
+
skipStaticElements: false,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
accessibleText: output,
|
|
44
|
+
staticAccessibleText: staticOutput,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const output = new Canvas({
|
|
49
|
+
width: renderDimension(node.yogaNode.getComputedWidth()),
|
|
50
|
+
height: renderDimension(node.yogaNode.getComputedHeight()),
|
|
51
|
+
colorProfile: options.colorProfile,
|
|
52
|
+
paintContext: options.paintContext,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
paintTree(node, output, {
|
|
56
|
+
skipStaticElements: true,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
let staticOutput;
|
|
60
|
+
|
|
61
|
+
if (node.staticNode?.yogaNode) {
|
|
62
|
+
staticOutput = new Canvas({
|
|
63
|
+
width: renderDimension(node.staticNode.yogaNode.getComputedWidth()),
|
|
64
|
+
height: renderDimension(node.staticNode.yogaNode.getComputedHeight()),
|
|
65
|
+
colorProfile: options.colorProfile,
|
|
66
|
+
paintContext: options.paintContext,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
paintTree(node.staticNode, staticOutput, {
|
|
70
|
+
skipStaticElements: false,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
screen: output.finish(),
|
|
76
|
+
...(staticOutput ? { staticScreen: staticOutput.finish() } : {}),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
accessibleText: "",
|
|
82
|
+
};
|
|
83
|
+
};
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { LegacyRoot } from "react-reconciler/constants.js";
|
|
3
|
+
|
|
4
|
+
import { detectColorLevel } from "#/capabilities/detect.ts";
|
|
5
|
+
import { createNode, type DOMElement } from "#/dom.ts";
|
|
6
|
+
import { reconciler } from "#/reconciler.ts";
|
|
7
|
+
import { renderFrame } from "#/render-frame.ts";
|
|
8
|
+
import { colorProfileFromLevel, type ColorProfile } from "#/screen/index.ts";
|
|
9
|
+
import { serializeScreen } from "#/screen/serialize.ts";
|
|
10
|
+
import { Yoga } from "#/yoga/index.ts";
|
|
11
|
+
|
|
12
|
+
export type RenderToStringOptions = {
|
|
13
|
+
/**
|
|
14
|
+
Width of the virtual terminal in columns.
|
|
15
|
+
|
|
16
|
+
@default 80
|
|
17
|
+
*/
|
|
18
|
+
columns?: number;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
Color profile used for deterministic serialization. By default this retains
|
|
22
|
+
the process color level for Ink compatibility.
|
|
23
|
+
*/
|
|
24
|
+
colorProfile?: ColorProfile;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
Render a React element to a string synchronously. Unlike `render()`, this function does not write to stdout, does not set up any terminal event listeners, and returns the rendered output as a string.
|
|
29
|
+
|
|
30
|
+
Useful for generating documentation, writing output to files, testing, or any scenario where you need the rendered output as a string without starting a persistent terminal application.
|
|
31
|
+
|
|
32
|
+
**Notes:**
|
|
33
|
+
|
|
34
|
+
- Terminal-specific hooks (`useInput`, `useStdin`, `useStdout`, `useStderr`, `useApp`, `useFocus`, `useFocusManager`) return default no-op values since there is no terminal session. They will not throw, but they will not function as in a live terminal.
|
|
35
|
+
- `useEffect` callbacks will execute during rendering (due to synchronous rendering mode), but state updates they trigger will not affect the returned output, which reflects the initial render.
|
|
36
|
+
- `useLayoutEffect` callbacks fire synchronously during commit, so state updates they trigger **will** be reflected in the output.
|
|
37
|
+
- The `<Static>` component is supported — its output is prepended to the dynamic output.
|
|
38
|
+
- If a component throws during rendering, the error is propagated to the caller after cleanup.
|
|
39
|
+
|
|
40
|
+
@example
|
|
41
|
+
```
|
|
42
|
+
import {renderToString, Text, Box} from 'ink';
|
|
43
|
+
|
|
44
|
+
const output = renderToString(
|
|
45
|
+
<Box padding={1}>
|
|
46
|
+
<Text color="green">Hello World</Text>
|
|
47
|
+
</Box>,
|
|
48
|
+
{columns: 40}
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
console.log(output);
|
|
52
|
+
```
|
|
53
|
+
*/
|
|
54
|
+
export const renderToString = (node: ReactNode, options?: RenderToStringOptions): string => {
|
|
55
|
+
const columns = options?.columns ?? 80;
|
|
56
|
+
const colorProfile = options?.colorProfile ?? colorProfileFromLevel(detectColorLevel());
|
|
57
|
+
|
|
58
|
+
// Create a standalone root node — no stdout, stdin, or terminal bindings
|
|
59
|
+
const rootNode: DOMElement = createNode("ink-root");
|
|
60
|
+
|
|
61
|
+
// Capture static output from intermediate renders.
|
|
62
|
+
// The <Static> component uses useLayoutEffect to clear its children after
|
|
63
|
+
// the first commit. The reconciler's resetAfterCommit calls onImmediateRender
|
|
64
|
+
// when static content is dirty (and returns early, skipping the normal
|
|
65
|
+
// onRender callback), giving us a chance to capture it before it's cleared
|
|
66
|
+
// by the subsequent re-render.
|
|
67
|
+
let capturedStaticOutput = "";
|
|
68
|
+
|
|
69
|
+
rootNode.onComputeLayout = () => {
|
|
70
|
+
rootNode.yogaNode!.setWidth(columns);
|
|
71
|
+
rootNode.yogaNode!.calculateLayout(undefined, undefined, Yoga.DIRECTION_LTR);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
rootNode.onImmediateRender = () => {
|
|
75
|
+
const { staticScreen } = renderFrame(rootNode, false, { colorProfile });
|
|
76
|
+
const staticOutput = staticScreen
|
|
77
|
+
? `${serializeScreen(staticScreen, { colorProfile, styles: colorProfile !== "none" })}\n`
|
|
78
|
+
: "";
|
|
79
|
+
if (staticOutput && staticOutput !== "\n") {
|
|
80
|
+
capturedStaticOutput += staticOutput;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// Capture the first uncaught error so we can re-throw it after cleanup.
|
|
85
|
+
// React's reconciler catches component errors internally and reports them
|
|
86
|
+
// via onUncaughtError rather than letting them propagate. For a synchronous
|
|
87
|
+
// utility like renderToString, callers expect errors to throw.
|
|
88
|
+
let uncaughtError: unknown;
|
|
89
|
+
|
|
90
|
+
// Create a reconciler container in legacy (synchronous) mode.
|
|
91
|
+
// The four trailing callbacks are: onUncaughtError, onCaughtError,
|
|
92
|
+
// onRecoverableError, and onHostTransitionComplete.
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
94
|
+
const container = reconciler.createContainer(
|
|
95
|
+
rootNode,
|
|
96
|
+
LegacyRoot,
|
|
97
|
+
null,
|
|
98
|
+
false,
|
|
99
|
+
null,
|
|
100
|
+
"render-to-string",
|
|
101
|
+
(error: unknown) => {
|
|
102
|
+
uncaughtError ??= error;
|
|
103
|
+
},
|
|
104
|
+
() => {},
|
|
105
|
+
() => {},
|
|
106
|
+
() => {},
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
// Synchronously render the React tree into the container
|
|
110
|
+
reconciler.updateContainerSync(node, container, null, () => {});
|
|
111
|
+
reconciler.flushSyncWork();
|
|
112
|
+
|
|
113
|
+
// Yoga layout has already been calculated by onComputeLayout during commit.
|
|
114
|
+
// Render the DOM tree to a string — this captures the dynamic (non-static) output.
|
|
115
|
+
const { screen } = renderFrame(rootNode, false, { colorProfile });
|
|
116
|
+
const output = screen
|
|
117
|
+
? serializeScreen(screen, { colorProfile, styles: colorProfile !== "none" })
|
|
118
|
+
: "";
|
|
119
|
+
|
|
120
|
+
// Tear down: unmount the tree so the reconciler cleans up child nodes
|
|
121
|
+
// and runs effect cleanup functions. The reconciler detaches removed
|
|
122
|
+
// subtrees (removeChildFromContainer → detachYogaSubtree); the garbage
|
|
123
|
+
// collector reclaims the Yoga nodes.
|
|
124
|
+
reconciler.updateContainerSync(null, container, null, () => {});
|
|
125
|
+
reconciler.flushSyncWork();
|
|
126
|
+
|
|
127
|
+
// Re-throw after full cleanup so callers see the original error.
|
|
128
|
+
if (uncaughtError !== undefined) {
|
|
129
|
+
throw uncaughtError instanceof Error
|
|
130
|
+
? uncaughtError
|
|
131
|
+
: // eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
132
|
+
new Error(String(uncaughtError));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Static terminal output ends with a newline so dynamic output starts on a
|
|
136
|
+
// fresh line. Strip it here so renderToString returns clean output.
|
|
137
|
+
const normalizedStaticOutput = capturedStaticOutput.endsWith("\n")
|
|
138
|
+
? capturedStaticOutput.slice(0, -1)
|
|
139
|
+
: capturedStaticOutput;
|
|
140
|
+
|
|
141
|
+
if (normalizedStaticOutput && output) {
|
|
142
|
+
return normalizedStaticOutput + "\n" + output;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return normalizedStaticOutput || output;
|
|
146
|
+
};
|
package/src/render.ts
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { Stream, type Writable } from "node:stream";
|
|
2
|
+
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
|
|
5
|
+
import type { ClipboardSelection, TerminalProgressState } from "#/ansi/osc.ts";
|
|
6
|
+
import { createInk, type Ink, type Options as InkOptions, type RenderMetrics } from "#/ink.tsx";
|
|
7
|
+
import { instances } from "#/instances.ts";
|
|
8
|
+
import { type KittyKeyboardOptions } from "#/kitty-keyboard.ts";
|
|
9
|
+
import { type ColorProfile } from "#/screen/color-profile.ts";
|
|
10
|
+
|
|
11
|
+
export type RenderOptions = {
|
|
12
|
+
/**
|
|
13
|
+
Output stream where the app will be rendered.
|
|
14
|
+
|
|
15
|
+
@default process.stdout
|
|
16
|
+
*/
|
|
17
|
+
stdout?: NodeJS.WritableStream;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
Input stream where app will listen for input.
|
|
21
|
+
|
|
22
|
+
@default process.stdin
|
|
23
|
+
*/
|
|
24
|
+
stdin?: NodeJS.ReadableStream;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
Error stream.
|
|
28
|
+
@default process.stderr
|
|
29
|
+
*/
|
|
30
|
+
stderr?: NodeJS.WritableStream;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
If true, each update will be rendered as separate output, without replacing the previous one.
|
|
34
|
+
|
|
35
|
+
@default false
|
|
36
|
+
*/
|
|
37
|
+
debug?: boolean;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
Configure whether Ink should listen for Ctrl+C keyboard input and exit the app. This is needed in case `process.stdin` is in raw mode, because then Ctrl+C is ignored by default and the process is expected to handle it manually.
|
|
41
|
+
|
|
42
|
+
@default true
|
|
43
|
+
*/
|
|
44
|
+
exitOnCtrlC?: boolean;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
Patch console methods to ensure console output doesn't mix with Ink's output.
|
|
48
|
+
|
|
49
|
+
Pass `"stdio"` to additionally intercept direct `stdout.write` / `stderr.write` calls on the streams Ink renders to (output from dependencies, native warnings, child tooling). Captured chunks are line-buffered and spliced above the live frame like console output; partial lines are flushed at unmount. Use `onCapturedOutput` to observe captured chunks or take over their display.
|
|
50
|
+
|
|
51
|
+
Note: Once unmount starts, Ink restores the native console (and stream writes) before React cleanup runs. Teardown-time output then follows the normal behavior instead of being rerouted through Ink.
|
|
52
|
+
|
|
53
|
+
@default true
|
|
54
|
+
*/
|
|
55
|
+
patchConsole?: boolean | "stdio";
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
Observe output captured by `patchConsole` before Ink displays it.
|
|
59
|
+
|
|
60
|
+
Receives each captured chunk with its origin (`"console"` or `"stdio"`). Return `true` to take ownership of the chunk: Ink will not display it, so the app can render it itself — for example inside a `<Static>` transcript.
|
|
61
|
+
*/
|
|
62
|
+
onCapturedOutput?: (
|
|
63
|
+
stream: "stdout" | "stderr",
|
|
64
|
+
data: string,
|
|
65
|
+
source: "console" | "stdio",
|
|
66
|
+
) => boolean | undefined | void;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
Runs the given callback after each render and re-render with render metrics.
|
|
70
|
+
|
|
71
|
+
Note: this callback runs after Ink commits a frame, but it does not wait for `stdout`/`stderr` stream callbacks.
|
|
72
|
+
To run code after output is flushed, use `waitUntilRenderFlush()`.
|
|
73
|
+
*/
|
|
74
|
+
onRender?: (metrics: RenderMetrics) => void;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
Enable screen reader support. See https://github.com/vadimdemedes/ink/blob/master/readme.md#screen-reader-support
|
|
78
|
+
|
|
79
|
+
@default process.env['SIGIL_SCREEN_READER'] === 'true'
|
|
80
|
+
*/
|
|
81
|
+
isScreenReaderEnabled?: boolean;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
Maximum frames per second for render updates.
|
|
85
|
+
This controls how frequently the UI can update to prevent excessive re-rendering.
|
|
86
|
+
Higher values allow more frequent updates but may impact performance.
|
|
87
|
+
|
|
88
|
+
@default 30
|
|
89
|
+
*/
|
|
90
|
+
maxFps?: number;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
Override the output color profile for this render instance. When omitted,
|
|
94
|
+
the profile follows the capabilities of `stdout` and capability upgrades
|
|
95
|
+
redraw the live frame. Static content already written to scrollback is not
|
|
96
|
+
replayed or recolored.
|
|
97
|
+
*/
|
|
98
|
+
colorProfile?: ColorProfile;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
Enable React Concurrent Rendering mode.
|
|
102
|
+
|
|
103
|
+
When enabled:
|
|
104
|
+
- Suspense boundaries work correctly with async data
|
|
105
|
+
- `useTransition` and `useDeferredValue` are fully functional
|
|
106
|
+
- Updates can be interrupted for higher priority work
|
|
107
|
+
|
|
108
|
+
Note: Concurrent mode changes the timing of renders. Some tests may need to use `act()` to properly await updates. Reusing the same stdout across multiple `render()` calls without unmounting is unsupported. Call `unmount()` first if you need to change the rendering mode or create a fresh instance.
|
|
109
|
+
|
|
110
|
+
@default false
|
|
111
|
+
*/
|
|
112
|
+
concurrent?: boolean;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
Configure kitty keyboard protocol support for enhanced keyboard input.
|
|
116
|
+
Enables additional modifiers (super, hyper, capsLock, numLock) and
|
|
117
|
+
disambiguated key events in terminals that support the protocol.
|
|
118
|
+
|
|
119
|
+
@see https://sw.kovidgoyal.net/kitty/keyboard-protocol/
|
|
120
|
+
*/
|
|
121
|
+
kittyKeyboard?: KittyKeyboardOptions;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
Override automatic interactive mode detection.
|
|
125
|
+
|
|
126
|
+
By default, Ink detects whether the environment is interactive based on CI detection (the `CI` environment variable) and `stdout.isTTY`. Most users should not need to set this.
|
|
127
|
+
|
|
128
|
+
When non-interactive, Ink disables ANSI erase sequences, cursor manipulation, synchronized output, resize handling, and kitty keyboard auto-detection, writing only the final frame at unmount.
|
|
129
|
+
|
|
130
|
+
Set to `false` to force non-interactive mode or `true` to force interactive mode when the automatic detection doesn't suit your use case.
|
|
131
|
+
|
|
132
|
+
Note: Reusing the same stdout across multiple `render()` calls without unmounting is unsupported. Call `unmount()` first if you need to change this option or create a fresh instance.
|
|
133
|
+
|
|
134
|
+
@default true (false if in CI or `stdout.isTTY` is falsy)
|
|
135
|
+
*/
|
|
136
|
+
interactive?: boolean;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
Render the app in the terminal's alternate screen buffer. When enabled, the app renders on a separate screen, and the original terminal content is restored when the app exits. This is the same mechanism used by programs like vim, htop, and less.
|
|
140
|
+
|
|
141
|
+
Note: The terminal's scrollback buffer is not available while in the alternate screen. This is standard terminal behavior; programs like vim use the alternate screen specifically to avoid polluting the user's scrollback history.
|
|
142
|
+
|
|
143
|
+
Note: Ink intentionally treats alternate-screen teardown output as disposable. It does not preserve or replay teardown-time frames, hook writes, or `console.*` output after restoring the primary screen.
|
|
144
|
+
|
|
145
|
+
Only works in interactive mode. Ignored when `interactive` is `false` or in a non-interactive environment (CI, piped stdout).
|
|
146
|
+
|
|
147
|
+
Note: Reusing the same stdout across multiple `render()` calls without unmounting is unsupported. Call `unmount()` first if you need to change this option or create a fresh instance.
|
|
148
|
+
|
|
149
|
+
@default false
|
|
150
|
+
*/
|
|
151
|
+
alternateScreen?: boolean;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
export type Instance = {
|
|
155
|
+
/**
|
|
156
|
+
Replace the previous root node with a new one or update props of the current root node.
|
|
157
|
+
*/
|
|
158
|
+
rerender: Ink["render"];
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
Manually unmount the whole Ink app.
|
|
162
|
+
*/
|
|
163
|
+
unmount: Ink["unmount"];
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
Returns a promise that settles when the app is unmounted.
|
|
167
|
+
|
|
168
|
+
It resolves with the value passed to `exit(value)` and rejects with the error passed to `exit(error)`.
|
|
169
|
+
When `unmount()` is called manually, it settles after unmount-related stdout writes complete.
|
|
170
|
+
|
|
171
|
+
@example
|
|
172
|
+
```jsx
|
|
173
|
+
const {unmount, waitUntilExit} = render(<MyApp />);
|
|
174
|
+
|
|
175
|
+
setTimeout(unmount, 1000);
|
|
176
|
+
|
|
177
|
+
await waitUntilExit(); // resolves after `unmount()` is called
|
|
178
|
+
```
|
|
179
|
+
*/
|
|
180
|
+
waitUntilExit: Ink["waitUntilExit"];
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
Returns a promise that settles after pending render output is flushed to stdout.
|
|
184
|
+
|
|
185
|
+
This can be used after `rerender()` when you need to run code only after the frame is written.
|
|
186
|
+
|
|
187
|
+
@example
|
|
188
|
+
```jsx
|
|
189
|
+
const {rerender, waitUntilRenderFlush} = render(<MyApp step="loading" />);
|
|
190
|
+
|
|
191
|
+
rerender(<MyApp step="ready" />);
|
|
192
|
+
await waitUntilRenderFlush(); // output for "ready" is flushed
|
|
193
|
+
|
|
194
|
+
runNextCommand();
|
|
195
|
+
```
|
|
196
|
+
*/
|
|
197
|
+
waitUntilRenderFlush: Ink["waitUntilRenderFlush"];
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
Unmount the current app and remove the internal Ink instance for this stdout.
|
|
201
|
+
|
|
202
|
+
This is mostly useful for advanced cases where you need `render()` to create a fresh instance for the same stream without leaving terminal state such as the alternate screen behind.
|
|
203
|
+
*/
|
|
204
|
+
cleanup: () => void;
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
Clear output.
|
|
208
|
+
*/
|
|
209
|
+
clear: () => void;
|
|
210
|
+
|
|
211
|
+
/** Copy text through the renderer-owned terminal session. */
|
|
212
|
+
copyToClipboard: (text: string, selection?: ClipboardSelection) => boolean;
|
|
213
|
+
|
|
214
|
+
/** Update terminal-native progress through the renderer-owned session. */
|
|
215
|
+
setProgress: (state: TerminalProgressState, value?: number) => boolean;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
Mount a component and render the output.
|
|
220
|
+
*/
|
|
221
|
+
export const render = (node: ReactNode, options?: Writable | RenderOptions): Instance => {
|
|
222
|
+
const inkOptions: InkOptions = {
|
|
223
|
+
stdout: process.stdout,
|
|
224
|
+
stdin: process.stdin,
|
|
225
|
+
stderr: process.stderr,
|
|
226
|
+
debug: false,
|
|
227
|
+
exitOnCtrlC: true,
|
|
228
|
+
patchConsole: true,
|
|
229
|
+
maxFps: 30,
|
|
230
|
+
concurrent: false,
|
|
231
|
+
alternateScreen: false,
|
|
232
|
+
...getOptions(options),
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const instance: Ink = getInstance(inkOptions.stdout, () => createInk(inkOptions));
|
|
236
|
+
instance.render(node);
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
rerender: instance.render,
|
|
240
|
+
unmount() {
|
|
241
|
+
instance.unmount();
|
|
242
|
+
},
|
|
243
|
+
waitUntilExit: instance.waitUntilExit,
|
|
244
|
+
waitUntilRenderFlush: instance.waitUntilRenderFlush,
|
|
245
|
+
cleanup() {
|
|
246
|
+
instance.unmount();
|
|
247
|
+
},
|
|
248
|
+
clear: instance.clear,
|
|
249
|
+
copyToClipboard: instance.copyToClipboard,
|
|
250
|
+
setProgress: instance.setProgress,
|
|
251
|
+
};
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
const getOptions = (stdout: Writable | RenderOptions | undefined = {}): RenderOptions => {
|
|
255
|
+
if (stdout instanceof Stream) {
|
|
256
|
+
return {
|
|
257
|
+
stdout,
|
|
258
|
+
stdin: process.stdin,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return stdout;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
const getInstance = (stdout: NodeJS.WritableStream, createInstance: () => Ink): Ink => {
|
|
266
|
+
const instance = instances.get(stdout);
|
|
267
|
+
|
|
268
|
+
if (instance === undefined) {
|
|
269
|
+
const newInstance = createInstance();
|
|
270
|
+
instances.set(stdout, newInstance);
|
|
271
|
+
return newInstance;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Ink keeps one live renderer per stdout. Reusing the same stream without
|
|
275
|
+
// unmounting is unsupported, but return the existing instance so we don't
|
|
276
|
+
// create two renderers that compete for the same output. Write the warning
|
|
277
|
+
// directly to native stderr so an existing alternate-screen renderer cannot
|
|
278
|
+
// swallow it via patchConsole.
|
|
279
|
+
process.stderr.write(
|
|
280
|
+
"Warning: render() was called again for the same stdout before the previous Ink instance was unmounted. Reusing stdout across multiple render() calls is unsupported. Call unmount() first.\n",
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
return instance;
|
|
284
|
+
};
|