Skip to content

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

herdr: a new client found while the start reads the font is read again

razorback166a72dc3e542b
diff --git a/hooks/lib/env.ts b/hooks/lib/env.ts
index 193166b..5ea1abd 100644
--- a/hooks/lib/env.ts
+++ b/hooks/lib/env.ts
@@ -14,7 +14,7 @@ import { claudeTmpOf } from './images'
import { applyTheme } from './themes'
import { registerCommand } from './command'
import { readHerdrClient, readHostMode, setOuter, termFontOf, watchHost } from './host'
-import { readFrame, setFrame, watchFrame } from './frame'
+import { readFrame, refontAfterStart, setFrame, watchFrame } from './frame'
import { offerSetup } from './setup'
import { findClipWay } from './clipboard'
import { findTypst, measureFonts } from './typst'
@@ -172,6 +172,7 @@ export function setUp($: EngineInterface): Promise<Env> {
metrics: typst && cache ? await measureFonts($, typst, root, cache, termFont, S.look?.dir ?? `${root}/render/themes/${BASE_THEME}`) : DEFAULT_METRICS,
}
refresh($)
+ refontAfterStart($)
// The start is done: the update check now, and nothing waits for it.
void checkUpdates($)
return S.env
diff --git a/hooks/lib/frame.ts b/hooks/lib/frame.ts
index 8500164..04c6481 100644
--- a/hooks/lib/frame.ts
+++ b/hooks/lib/frame.ts
@@ -320,6 +320,15 @@ export async function followWatcher(
// time; a change while it runs reads once more.
let fonting: Promise<boolean> | null = null
let fontAgain = false
+let refontAtStart = false
+
+// The end of setUp: a refont asked while it ran reads the font again now
+// (a new epoch when it changed).
+export function refontAfterStart($: EngineInterface) {
+ if (!refontAtStart) return
+ refontAtStart = false
+ void refont($).then(changed => void (changed && beginEpoch($)), () => undefined)
+}
export function refont($: EngineInterface): Promise<boolean> {
if (fonting) {
fontAgain = true
@@ -330,7 +339,11 @@ export function refont($: EngineInterface): Promise<boolean> {
do {
fontAgain = false
const env = S.env
- if (!env) return changed
+ // Before the start is done (its font may be the old one): setUp reads again.
+ if (!env) {
+ refontAtStart = true
+ return changed
+ }
const termFont = (await termFontOf($, env.isMac).catch(() => '')) || env.termFont
const 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
diff --git a/tests/termfont.test.ts b/tests/termfont.test.ts
index fb7f8e7..75e7cda 100644
--- a/tests/termfont.test.ts
+++ b/tests/termfont.test.ts
@@ -487,6 +487,41 @@ pollTest('herdr: /iui theme whose measure ends after a new terminal font measure
expect(measuresOf(w).filter(m => m === 'Fira Code|vscode').length).toBe(before + 1)
})
+pollTest('herdr: a new client found while the start still measures the old font: the new font is read once the start is done', async ($, on) => {
+ let client = ghosttyClient
+ let release = () => {}
+ let hold: Promise<void> | null = new Promise<void>(r => (release = r))
+ let held = false
+ const w = world(on, {
+ isMac: true,
+ env: HERDR_SERVER_ENV,
+ families: ['Iosevka', 'Fira Code'],
+ files: { '/h/kc/kitty.conf': 'font_family Fira Code\n' },
+ run: argv => {
+ if (hold && argv[1] === 'eval') {
+ 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)
+ },
+ })
+ const first = details($)
+ // The start reads files first: real time, not only microtasks.
+ for (let i = 0; i < 200 && !held; i++) await new Promise(r => (globalThis as any).setTimeout(r, 1))
+ expect(measuresOf(w).map(m => m.split('|')[0])).toEqual(['Iosevka'])
+ // kitty's client while the start measures Ghostty's font.
+ client = kittyClient
+ const scanned = scansOf(w)
+ await w.clock.advance(HERDR_SCAN_MS)
+ await until(() => scansOf(w) > scanned)
+ release()
+ await first
+ await until(() => measuresOf(w).at(-1)?.startsWith('Fira Code|') === true)
+ expect(await details($)).toContain('terminal font Fira Code.')
+})
+
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 = ghosttyClient
let front: string | null = 'com.mitchellh.ghostty'