Claude's answers, rendered: Markdown, math, charts and diagrams drawn in the Claude Code terminal. Independent project, not affiliated with Anthropic.
TypeScript claude-code-mod
Fonts: a new terminal font and /iui theme never write their measure over each other
razorback16bce7f1a53ae1
diff --git a/hooks/lib/frame.ts b/hooks/lib/frame.tsindex e4da827..8500164 100644--- a/hooks/lib/frame.ts+++ b/hooks/lib/frame.ts@@ -334,6 +334,11 @@ export function refont($: EngineInterface): Promise<boolean> {const termFont = (await termFontOf($, env.isMac).catch(() => '')) || env.termFontconst themeDir = S.look?.dir ?? `${env.root}/render/themes/${BASE_THEME}`const read = env.typst ? await measureFonts($, env.typst, env.root, env.cache, termFont, themeDir).catch(() => DEFAULT_METRICS) : env.metrics+ // A theme set meanwhile (/iui theme): its fonts are measured again, never an older theme's written.+ if ((S.look?.dir ?? `${env.root}/render/themes/${BASE_THEME}`) !== themeDir) {+ fontAgain = true+ continue+ }const metrics = read === DEFAULT_METRICS ? env.metrics : readif (termFont === env.termFont && sameMetrics(metrics, env.metrics)) continueenv.termFont = termFontdiff --git a/hooks/register.tsx b/hooks/register.tsxindex e28e59a..0114ade 100644--- a/hooks/register.tsx+++ b/hooks/register.tsx@@ -776,8 +776,16 @@ async function setTheme($: EngineInterface, name: string | undefined): Promise<sreturn `No usable theme named ${name}.\n\n${themeListing(themes, problems, was?.name ?? DEFAULT_THEME, userThemesOf(ready.home), ready.home)}`}if (S.look?.name !== was?.name || S.look?.dir !== was?.dir || S.look?.stamp !== was?.stamp) {- // The theme's fonts set the column width and the code size.- if (ready.typst && S.look) ready.metrics = await measureFonts($, ready.typst, ready.root, ready.cache, ready.termFont, S.look.dir)+ // The theme's fonts set the column width and the code size; measured again+ // when the terminal font changed meanwhile (refont), never for an older font.+ if (ready.typst && S.look) {+ let font: string+ do {+ font = ready.termFont+ const metrics = await measureFonts($, ready.typst, ready.root, ready.cache, font, S.look.dir)+ if (font === ready.termFont) ready.metrics = metrics+ } while (font !== ready.termFont)+ }beginEpoch($)}if (name) await save($, 'theme-name', chosen.name)diff --git a/tests/termfont.test.ts b/tests/termfont.test.tsindex 213e7ad..fb7f8e7 100644--- a/tests/termfont.test.ts+++ b/tests/termfont.test.ts@@ -37,6 +37,8 @@ function world(on: any, o: {measure?: stringrun?: (argv: string[]) => ReturnType<typeof ok> | undefinedstored?: Record<string, unknown>+ // The built-in themes claude-code and vscode (their folders and theme.json).+ themes?: boolean}): World {S.fontPaths = ''mock.store(on, o.stored)@@ -85,7 +87,13 @@ function world(on: any, o: {return ok('', 1)})const files = o.files ?? {}- on('fs.read', async ($: any, e: any, next: any) => (e.path in files ? { value: files[e.path] } : e.path.startsWith('/h/') || e.path.startsWith('/c/') ? { deny: 'ENOENT' } : next(e)))+ const theme = (path: string) => (o.themes ? /\/render\/themes\/(claude-code|vscode)\/theme\.json$/.exec(path)?.[1] : undefined)+ on('fs.read', async ($: any, e: any, next: any) =>+ e.path in files ? { value: files[e.path] } : theme(e.path) ? { value: `{"name":"${theme(e.path)}"}` } : e.path.startsWith('/h/') || e.path.startsWith('/c/') ? { deny: 'ENOENT' } : next(e))+ if (o.themes) {+ on('fs.list', async ($: any, e: any, next: any) =>+ e.path.endsWith('/render/themes') ? { value: ['claude-code', 'vscode'].map(name => ({ name, kind: 'dir', size: 0, mtimeMs: 0, isLink: false })) } : next(e))+ }on('fs.exists', async ($: any, e: any, next: any) => (e.path.startsWith('/h/') ? { value: e.path in files } : e.path === '/usr/bin/typst' ? { value: true } : next(e)))return { ran, fontPaths, copies: () => ran.filter(argv => argv.includes(PROGRAM_FONTS_SH)), clock, envs }}@@ -397,6 +405,88 @@ pollTest('herdr: the end of the session stops the check each second', async ($,expect(checksSince(w, from)).toBe(0)})+// The fonts measured (typst eval of measure.typ): the terminal font and the theme folder's name.+const measuresOf = (w: World) =>+ w.ran.filter(argv => argv[1] === 'eval').map(argv => `${argAfter(argv, '--input').replace(/^term-font=/, '')}|${argv.at(-1)!.replace(/^.*\//, '')}`)+// Microtasks until `done()`, for a hook chain that runs no timer.+const until = async (done: () => boolean) => {+ for (let i = 0; i < 2000 && !done(); i++) await Promise.resolve()+}++pollTest('herdr: a new terminal font measured while /iui theme sets another theme is measured again for that theme', async ($, on) => {+ let client = ghosttyClient+ let hold: Promise<void> | null = null+ let held = false+ let release = () => {}+ const w = world(on, {+ isMac: true,+ env: HERDR_SERVER_ENV,+ families: ['Iosevka', 'Fira Code'],+ files: { '/h/kc/kitty.conf': 'font_family Fira Code\n' },+ themes: true,+ run: argv => {+ if (hold && argv[1] === 'eval' && argv.includes('term-font=Fira Code')) {+ const h = hold+ hold = null+ held = true+ return h.then(() => ok('{}')) as any+ }+ return clientIs(() => client)(argv) ?? tickIs(() => null)(argv) ?? shows('font-family = Iosevka\n')(argv)+ },+ })+ await details($)+ // kitty's client: the full scan finds it, and its font's measure waits.+ client = kittyClient+ hold = new Promise<void>(r => (release = r))+ await w.clock.advance(HERDR_SCAN_MS)+ await until(() => held)+ expect(held).toBe(true)+ expect(measuresOf(w).at(-1)).toBe('Fira Code|claude-code')+ // A theme is set meanwhile (with the font known then), then the old measure ends.+ expect((await $.command.run({ command: 'iui', args: 'theme vscode' } as any)).text).toContain('Theme set to vscode.')+ expect(measuresOf(w).at(-1)).toBe('Iosevka|vscode')+ release()+ await until(() => measuresOf(w).at(-1) === 'Fira Code|vscode')+ expect(measuresOf(w).at(-1)).toBe('Fira Code|vscode')+})++pollTest('herdr: /iui theme whose measure ends after a new terminal font measures again with that font', async ($, on) => {+ let client = ghosttyClient+ let hold: Promise<void> | null = null+ let held = false+ let release = () => {}+ const w = world(on, {+ isMac: true,+ env: HERDR_SERVER_ENV,+ families: ['Iosevka', 'Fira Code'],+ files: { '/h/kc/kitty.conf': 'font_family Fira Code\n' },+ themes: true,+ run: argv => {+ if (hold && argv[1] === 'eval' && argv.includes('term-font=Iosevka') && argv.at(-1)!.endsWith('/vscode')) {+ const h = hold+ hold = null+ held = true+ return h.then(() => ok('{}')) as any+ }+ return clientIs(() => client)(argv) ?? tickIs(() => null)(argv) ?? shows('font-family = Iosevka\n')(argv)+ },+ })+ await details($)+ hold = new Promise<void>(r => (release = r))+ const theme = $.command.run({ command: 'iui', args: 'theme vscode' } as any)+ await until(() => held)+ expect(held).toBe(true)+ // kitty's client meanwhile: its font is read and measured.+ client = kittyClient+ await w.clock.advance(HERDR_SCAN_MS)+ await until(() => measuresOf(w).at(-1) === 'Fira Code|vscode')+ const before = measuresOf(w).filter(m => m === 'Fira Code|vscode').length+ release()+ expect((await theme).text).toContain('Theme set to vscode.')+ // The theme's measure for the old font is not kept: one more with the new font.+ expect(measuresOf(w).filter(m => m === 'Fira Code|vscode').length).toBe(before + 1)+})+fontTest('herdr: a terminal in front with no known client makes a full scan at the next second, and only once while it stays in front', async ($, on) => {let client = ghosttyClientlet front: string | null = 'com.mitchellh.ghostty'