The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 58 lines 2.09 KB
Raw
1 /** @jsxRuntime automatic */
2 /** @jsxImportSource @alchemy.run/sigil */
3 import { stripVTControlCharacters } from "node:util";
4 import { renderToString } from "@alchemy.run/sigil";
5 import type { RenderOptions, TerminalCapabilities, View } from "../types.ts";
6 import { CliEnvironment } from "../ui/Environment.tsx";
7 import { Text } from "../ui/Typography.tsx";
8
9 export const normalizeView = (view: View): View => {
10 if (typeof view === "string" || typeof view === "number" || typeof view === "bigint") {
11 return <Text>{String(view)}</Text>;
12 }
13 // Contract: an array of primitives renders as ONE Text line with the
14 // elements concatenated (no separator) — matching how React renders
15 // adjacent text children. Callers wanting separators must join themselves.
16 if (
17 Array.isArray(view) &&
18 view.every(
19 (item) =>
20 item === null ||
21 item === undefined ||
22 typeof item === "boolean" ||
23 typeof item === "string" ||
24 typeof item === "number" ||
25 typeof item === "bigint",
26 )
27 ) {
28 return (
29 <Text>
30 {view
31 .filter((item) => item !== null && item !== undefined && typeof item !== "boolean")
32 .map(String)
33 .join("")}
34 </Text>
35 );
36 }
37 return view;
38 };
39
40 /** Render a layout to a string without a live renderer (help, plain output, snapshots). */
41 export const formatView = (
42 view: View,
43 capabilities: TerminalCapabilities,
44 options: RenderOptions = {},
45 ): string => {
46 const colors = options.colors ?? capabilities.colors;
47 // The renderer and the components must agree on the width: components size
48 // themselves from the environment's `columns`, so an explicit render width
49 // has to flow into the capabilities too, not only into renderToString.
50 const columns = options.columns ?? capabilities.columns;
51 const output = renderToString(
52 <CliEnvironment capabilities={{ ...capabilities, colors, columns }}>
53 {normalizeView(view)}
54 </CliEnvironment>,
55 { columns, colorProfile: colors ? "truecolor" : "none" },
56 ).replace(/[\s\n]+$/, "");
57 return colors ? output : stripVTControlCharacters(output);
58 };