Skip to content

Commit d9354dd

Browse files
fix(mobile): commands color embedded scripts and use a monospace font on iOS
Move embeddedScripts and withEmbeddedScripts from web into client-runtime so mobile colors heredocs and inline scripts like web. Give iOS a single font-mono family; Tailwind's font stack fell back to the system font. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8d9114a commit d9354dd

12 files changed

Lines changed: 236 additions & 136 deletions

File tree

‎apps/mobile/global.css‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
/* ─── Theme tokens ──────────────────────────────────────────────────── */
66
@layer theme {
77
:root {
8+
/* Native text takes one family name, not Tailwind's font stack. */
9+
@variant ios {
10+
--font-mono: "ui-monospace";
11+
}
812
@variant android {
913
--font-mono: "monospace";
1014
}

‎apps/mobile/src/features/threads/thread-work-log.tsx‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
useEffect,
2626
useId,
2727
useLayoutEffect,
28+
useMemo,
2829
useRef,
2930
useState,
3031
type ComponentProps,
@@ -36,6 +37,7 @@ import {
3637
Pressable,
3738
ScrollView,
3839
StyleSheet,
40+
Text as RNText,
3941
useAnimatedValue,
4042
View,
4143
} from "react-native";
@@ -54,6 +56,7 @@ import { useAtomCommand } from "../../state/use-atom-command";
5456
import { toolActivityFaviconUrl } from "@t3tools/shared/favicon";
5557
import { isJavaScriptReplTool } from "@t3tools/shared/toolActivity";
5658
import { useMarkdownCodeHighlight } from "./markdownCodeHighlightState";
59+
import { highlightCommand, type HighlightedLines } from "../../lib/highlights";
5760
import {
5861
commandHighlightLanguage,
5962
withVisibleControlCharacters,
@@ -807,10 +810,35 @@ function ToolCodeText({
807810
theme,
808811
}: {
809812
readonly code: string;
810-
readonly language: "typescript" | "html" | "json" | "shellscript" | "powershell";
813+
readonly language: "typescript" | "html" | "json";
811814
readonly theme: "light" | "dark";
812815
}) {
813816
const lines = useMarkdownCodeHighlight({ code, language, theme, enabled: true });
817+
return <HighlightedCodeText code={code} lines={lines} />;
818+
}
819+
820+
function CommandCodeText({
821+
command,
822+
theme,
823+
}: {
824+
readonly command: string;
825+
readonly theme: "light" | "dark";
826+
}) {
827+
const code = withVisibleControlCharacters(command.trim());
828+
const lines = useMemo(
829+
() => highlightCommand(code, commandHighlightLanguage(code), theme),
830+
[code, theme],
831+
);
832+
return <HighlightedCodeText code={code} lines={lines} />;
833+
}
834+
835+
function HighlightedCodeText({
836+
code,
837+
lines,
838+
}: {
839+
readonly code: string;
840+
readonly lines: HighlightedLines | null;
841+
}) {
814842
let offset = 0;
815843
const highlighted = lines?.flatMap((line, lineIndex) => {
816844
const separator = lineIndex > 0 ? "\n" : "";
@@ -820,10 +848,11 @@ function ToolCodeText({
820848
...line.map((token) => {
821849
const start = offset;
822850
offset += token.content.length;
851+
// Plain RN Text inherits the mono font; AppText would reset it to sans.
823852
return (
824-
<Text key={start} style={token.color ? { color: token.color } : undefined}>
853+
<RNText key={start} style={token.color ? { color: token.color } : undefined}>
825854
{token.content}
826-
</Text>
855+
</RNText>
827856
);
828857
}),
829858
];
@@ -1184,10 +1213,9 @@ const ThreadWorkLogRow = function ThreadWorkLogRow(
11841213
) : call ? (
11851214
[
11861215
call.command ? (
1187-
<ToolCodeText
1216+
<CommandCodeText
11881217
key="command"
1189-
code={withVisibleControlCharacters(call.command.trim())}
1190-
language={commandHighlightLanguage(call.command)}
1218+
command={call.command}
11911219
theme={props.themeAppearance}
11921220
/>
11931221
) : null,

‎apps/mobile/src/lib/highlights.test.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { pierreDark, pierreLight } from "@pierre/highlights/themes";
33
import { describe, expect, it, vi } from "vite-plus/test";
44

55
import { resolveHighlightLanguage } from "./highlightLanguage";
6-
import { highlightCode, tokenizeCode } from "./highlights";
6+
import { highlightCode, highlightCommand, tokenizeCode, type HighlightedLines } from "./highlights";
77

88
vi.mock("react-native-nitro-modules", async () => {
99
const { createWasmHighlightsModule } = await import("./highlights.testing");
@@ -37,6 +37,19 @@ describe("highlightCode", () => {
3737
}
3838
});
3939

40+
it("colors a command's embedded script with that script's grammar", () => {
41+
const body = "import json\r\nprint(json.dumps({'ok': True}))";
42+
const code = `python3 - <<'PY'\r\n${body}\r\nPY`;
43+
const colors = (lines: HighlightedLines) =>
44+
lines.map((line) => line.map(({ content, color }) => [content, color]));
45+
const lines = highlightCommand(code, "shellscript", "dark");
46+
47+
expect(lines.map((line) => line.map((token) => token.content).join(""))).toEqual(
48+
code.split("\r\n"),
49+
);
50+
expect(colors(lines.slice(1, 3))).toEqual(colors(highlightCode(body, "python", "dark")));
51+
});
52+
4053
it("keeps an over-long line as one plain token", () => {
4154
const longLine = `const data = "${"x".repeat(1_000)}";`;
4255
const [first, second] = highlightCode(`let a = 1;\n${longLine}`, "typescript", "dark");

‎apps/mobile/src/lib/highlights.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import {
2+
embeddedScripts,
3+
withEmbeddedScripts,
4+
type SyntaxToken,
5+
} from "@t3tools/client-runtime/work-log/embedded-scripts";
16
import type { T3Highlights } from "@t3tools/mobile-highlights-native";
27
import { NitroModules } from "react-native-nitro-modules";
38

@@ -82,6 +87,46 @@ export function tokenizeCode(code: string, language: string): TokenizedCode {
8287
};
8388
}
8489

90+
/** Shell commands also color the scripts they embed, such as heredocs and `python -c`. */
91+
export function highlightCommand(
92+
code: string,
93+
language: "shellscript" | "powershell",
94+
scheme: HighlightScheme,
95+
): HighlightedLines {
96+
const tokenize = (text: string, lang: string) => {
97+
// A missing grammar keeps the shell colors, as on web.
98+
if (highlightLanguageId(lang) === 0) throw new Error(`No grammar for ${lang}`);
99+
return withOffsets(text, highlightCode(text, lang, scheme));
100+
};
101+
const embedded = language === "shellscript" ? embeddedScripts(code) : [];
102+
return withEmbeddedScripts(code, tokenize(code, language), embedded, tokenize).map((line) =>
103+
line.map((token) => ({
104+
content: token.content,
105+
color: token.color ?? null,
106+
fontStyle: token.fontStyle ?? null,
107+
})),
108+
);
109+
}
110+
111+
function withOffsets(code: string, lines: HighlightedLines): SyntaxToken[][] {
112+
const endings = code.match(/\r?\n/gu) ?? [];
113+
let offset = 0;
114+
return lines.map((line, index) => {
115+
const tokens = line.map((token) => {
116+
const start = offset;
117+
offset += token.content.length;
118+
return {
119+
content: token.content,
120+
offset: start,
121+
...(token.color ? { color: token.color } : {}),
122+
...(token.fontStyle ? { fontStyle: token.fontStyle } : {}),
123+
};
124+
});
125+
offset += endings[index]?.length ?? 0;
126+
return tokens;
127+
});
128+
}
129+
85130
/** LF and CRLF split lines; a trailing line break yields a final empty line. */
86131
export function highlightCode(
87132
code: string,

‎apps/web/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
"sql.js": "1.15.0",
8787
"tailwind-merge": "^3.4.0",
8888
"tiff": "7.1.3",
89-
"unbash": "^5.0.0",
9089
"unified": "catalog:",
9190
"use-sync-external-store": "^1.6.0",
9291
"vfile": "^6.0.3",
Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { embeddedScripts } from "../../lib/embeddedScripts";
4-
import { keyedLines, withEmbeddedScripts, wordsOf } from "./HighlightedTokens";
3+
import { keyedLines, wordsOf } from "./HighlightedTokens";
54

65
function tokensOf(...contents: string[]) {
76
let offset = 0;
@@ -34,51 +33,3 @@ describe("keyedLines", () => {
3433
expect(rendered).toBe(code);
3534
});
3635
});
37-
38-
describe("withEmbeddedScripts", () => {
39-
function tokenize(text: string, language: string) {
40-
if (language === "unknown") throw new Error("Language not found");
41-
let lineStart = 0;
42-
return text.split(/\r?\n/u).map((line) => {
43-
const tokens = [...line.matchAll(/\S+|\s+/gu)].map((match) => ({
44-
content: match[0],
45-
offset: lineStart + match.index,
46-
color: language,
47-
}));
48-
lineStart +=
49-
line.length + (text.slice(lineStart + line.length).match(/^\r?\n/u)?.[0].length ?? 0);
50-
return tokens;
51-
});
52-
}
53-
54-
function colorsOf(code: string, scripts = embeddedScripts(code)) {
55-
const lines = withEmbeddedScripts(code, tokenize(code, "shellscript"), scripts, tokenize);
56-
expect(lines.map((line) => line.map((token) => token.content).join("")).join("\n")).toBe(
57-
code.replaceAll("\r\n", "\n"),
58-
);
59-
return lines.flat().map((token) => [token.content, token.color]);
60-
}
61-
62-
it("colors each nested script with its own grammar, keeping the text", () => {
63-
expect(colorsOf(`bash -lc "python3 -c 'print(1)'"`)).toEqual([
64-
[`bash -lc "python3 -c '`, "shellscript"],
65-
["print(1)", "python"],
66-
[`'"`, "shellscript"],
67-
]);
68-
});
69-
70-
it("colors an escape sequence like the character it spells", () => {
71-
expect(colorsOf(`bash -c "python3 -c 'print(\\"x\\")'"`)).toContainEqual([
72-
`print(\\"x\\")`,
73-
"python",
74-
]);
75-
});
76-
77-
it("keeps the surrounding colors where a grammar is missing", () => {
78-
const code = "cat > x.ts <<'EOF'\r\nbody\r\nEOF";
79-
const [script] = embeddedScripts(code);
80-
const characterColors = (scripts: ReturnType<typeof embeddedScripts>) =>
81-
colorsOf(code, scripts).flatMap(([content, color]) => [...content!].map(() => color));
82-
expect(characterColors([{ ...script!, language: "unknown" }])).toEqual(characterColors([]));
83-
});
84-
});

‎apps/web/src/components/chat/HighlightedTokens.tsx‎

Lines changed: 6 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import { use, type CSSProperties } from "react";
22

3+
import {
4+
withEmbeddedScripts,
5+
type EmbeddedScript,
6+
type SyntaxToken,
7+
} from "@t3tools/client-runtime/work-log/embedded-scripts";
8+
39
import { resolveDiffThemeName } from "../../lib/diffRendering";
4-
import type { EmbeddedScript } from "../../lib/embeddedScripts";
510
import { getSyntaxHighlighterPromise } from "../../lib/syntaxHighlighting";
611

7-
interface SyntaxToken {
8-
readonly content: string;
9-
readonly offset: number;
10-
readonly color?: string;
11-
readonly fontStyle?: number;
12-
}
13-
14-
type TokenLines = ReadonlyArray<ReadonlyArray<SyntaxToken>>;
15-
1612
const NO_EMBEDDED_SCRIPTS: ReadonlyArray<EmbeddedScript> = [];
1713

1814
function syntaxTokenStyle(token: SyntaxToken): CSSProperties {
@@ -80,66 +76,6 @@ export function HighlightedTokens({
8076
);
8177
}
8278

83-
export function withEmbeddedScripts(
84-
code: string,
85-
lines: TokenLines,
86-
embedded: ReadonlyArray<EmbeddedScript>,
87-
tokenize: (text: string, language: string) => TokenLines,
88-
): TokenLines {
89-
if (embedded.length === 0) return lines;
90-
const styles = Array.from<SyntaxToken | undefined>({ length: code.length });
91-
for (const token of lines.flat()) {
92-
styles.fill(token, token.offset, token.offset + token.content.length);
93-
}
94-
for (const script of embedded) {
95-
let scriptLines: TokenLines;
96-
try {
97-
scriptLines = tokenize(script.text, script.language);
98-
} catch {
99-
continue;
100-
}
101-
for (const token of scriptLines.flat()) {
102-
if (token.content === "") continue;
103-
const last = token.offset + token.content.length - 1;
104-
styles.fill(token, script.starts[token.offset], script.ends[last]);
105-
}
106-
}
107-
return restyledLines(code, styles);
108-
}
109-
110-
function restyledLines(
111-
code: string,
112-
styles: ReadonlyArray<SyntaxToken | undefined>,
113-
): SyntaxToken[][] {
114-
const lines: SyntaxToken[][] = [];
115-
let start = 0;
116-
for (const lineBreak of [...code.matchAll(/\r?\n/gu), undefined]) {
117-
const end = lineBreak?.index ?? code.length;
118-
const tokens: SyntaxToken[] = [];
119-
for (let index = start; index < end;) {
120-
const style = styles[index];
121-
let next = index + 1;
122-
while (
123-
next < end &&
124-
styles[next]?.color === style?.color &&
125-
styles[next]?.fontStyle === style?.fontStyle
126-
) {
127-
next += 1;
128-
}
129-
tokens.push({
130-
content: code.slice(index, next),
131-
offset: index,
132-
...(style?.color ? { color: style.color } : {}),
133-
...(style?.fontStyle ? { fontStyle: style.fontStyle } : {}),
134-
});
135-
index = next;
136-
}
137-
lines.push(tokens);
138-
if (lineBreak) start = lineBreak.index + lineBreak[0].length;
139-
}
140-
return lines;
141-
}
142-
14379
/** Restore CRLF from source; Shiki strips both line-ending forms. */
14480
export function keyedLines<Token extends SyntaxToken>(
14581
code: string,

‎apps/web/src/components/chat/ShellCommandBlock.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import { HighlightedTokens } from "./HighlightedTokens";
1010

1111
const WORD_CLASS_NAME = "inline-block max-w-full [overflow-wrap:anywhere]";
1212

13-
let embeddedScriptsModule: Promise<typeof import("../../lib/embeddedScripts")> | undefined;
13+
let embeddedScriptsModule:
14+
| Promise<typeof import("@t3tools/client-runtime/work-log/embedded-scripts")>
15+
| undefined;
1416
function loadEmbeddedScripts() {
15-
embeddedScriptsModule ??= import("../../lib/embeddedScripts");
17+
embeddedScriptsModule ??= import("@t3tools/client-runtime/work-log/embedded-scripts");
1618
return embeddedScriptsModule;
1719
}
1820

‎packages/client-runtime/package.json‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,10 @@
423423
"types": "./src/work-log/commandLabel.ts",
424424
"default": "./src/work-log/commandLabel.ts"
425425
},
426+
"./work-log/embedded-scripts": {
427+
"types": "./src/work-log/embeddedScripts.ts",
428+
"default": "./src/work-log/embeddedScripts.ts"
429+
},
426430
"./work-log/scroll-anchor": {
427431
"types": "./src/work-log/scrollAnchor.ts",
428432
"default": "./src/work-log/scrollAnchor.ts"
@@ -574,7 +578,8 @@
574578
"@t3tools/source-control-definitions": "workspace:*",
575579
"effect": "catalog:",
576580
"jszip": "3.10.1",
577-
"three": "0.180.0"
581+
"three": "0.180.0",
582+
"unbash": "^5.0.0"
578583
},
579584
"devDependencies": {
580585
"@effect/vitest": "catalog:",

0 commit comments

Comments
 (0)