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: the check each second only while it is of use
razorback16c1dd4165110d
diff --git a/hooks/lib/env.ts b/hooks/lib/env.tsindex ca69c04..193166b 100644--- a/hooks/lib/env.ts+++ b/hooks/lib/env.ts@@ -98,6 +98,7 @@ export function setUp($: EngineInterface): Promise<Env> {S.userFontPaths = (await getenv($, 'TYPST_FONT_PATHS')) ?? ''if (S.setting === 'auto') S.host = (await readHostMode($).catch(() => null)) ?? S.hostS.theme = resolveMode(S.setting, S.host)+ S.herdrDraws = S.isHerdr && canDrawwatchHost($)// The stored settings, but not one an /iui command set meanwhile (settingsSet).const kept = Number(await load($, 'size').catch(() => undefined))@@ -118,7 +119,11 @@ export function setUp($: EngineInterface): Promise<Env> {// An engine with no version call is older than picture select.S.engine = await Promise.resolve().then(() => ver($)).then(v => v.base ?? v.version, () => '')const enabled = await load($, 'enabled').catch(() => undefined)- if (enabled === false && !settingsSet.has('enabled')) await update($, isOn, () => false).catch(() => undefined)+ if (enabled === false && !settingsSet.has('enabled')) {+ S.isOff = true+ watchHost($)+ await update($, isOn, () => false).catch(() => undefined)+ }const isMac = os.trim() === 'Darwin'// Linux, on this computer: the clipboard tools, for the copy.if (S.hostOs === 'Linux' && !ssh) void findClipWay($).catch(() => undefined)diff --git a/hooks/lib/host.ts b/hooks/lib/host.tsindex 5ab346b..4716fda 100644--- a/hooks/lib/host.ts+++ b/hooks/lib/host.ts@@ -760,14 +760,15 @@ async function readLinuxScheme($: EngineInterface): Promise<LinuxScheme | null>// each HOST_POLL_MS (every third poll), at once for another terminal, and// with `modeNow`.export async function pollHost($: EngineInterface, modeNow = false) {- if (S.checking || (S.setting !== 'auto' && !S.isHerdr)) return+ const herdr = herdrPolled()+ if (S.checking || (S.setting !== 'auto' && !herdr)) returnS.checking = truelet refonted = falseconst flipWas = isFlipHere()try {- const moved = S.isHerdr && (await checkHerdrClient($).catch(() => false))+ const moved = herdr && (await checkHerdrClient($).catch(() => false))if (moved) await setOuter($)- const due = !S.isHerdr || modeNow || moved || ++S.hostTicks >= HOST_POLL_MS / HERDR_POLL_MS+ const due = !herdr || modeNow || moved || ++S.hostTicks >= HOST_POLL_MS / HERDR_POLL_MSif (due) S.hostTicks = 0if (S.setting === 'auto' && due) S.host = (await readHostMode($).catch(() => null)) ?? S.hostif (moved) refonted = await refont($).catch(() => false)@@ -788,11 +789,15 @@ export function showMode($: EngineInterface) {beginEpoch($)}-// Polls while the setting is auto or under herdr (its client can change), and-// only then: each HOST_POLL_MS, under herdr each HERDR_POLL_MS.+// herdr's client is checked: under herdr, with pictures drawn and the mod on.+const herdrPolled = () => S.isHerdr && S.herdrDraws && !S.isOff++// Polls while the setting is auto or herdr's client is checked (it can+// change), and only then, until the session ends: each HOST_POLL_MS, while+// herdr's client is checked each HERDR_POLL_MS.export function watchHost($: EngineInterface) {- const wanted = S.setting === 'auto' || S.isHerdr- const ms = S.isHerdr ? HERDR_POLL_MS : HOST_POLL_MS+ const wanted = !S.hostEnded && (S.setting === 'auto' || herdrPolled())+ const ms = herdrPolled() ? HERDR_POLL_MS : HOST_POLL_MS// Started before setUp knew of herdr: started again with its period.if (S.watch && (!wanted || S.watchMs !== ms)) {S.watch.cancel()@@ -807,3 +812,10 @@ export function watchHost($: EngineInterface) {}}}++// The session ends (not a /clear): no more polls.+export function stopHost() {+ S.hostEnded = true+ S.watch?.cancel()+ S.watch = null+}diff --git a/hooks/lib/state.ts b/hooks/lib/state.tsindex 36e9abd..594ffa2 100644--- a/hooks/lib/state.ts+++ b/hooks/lib/state.ts@@ -216,6 +216,12 @@ export const S = {watch: null as { cancel: () => void } | null,// The poll timer's period (watchHost).watchMs: 0,+ // herdr's client check is of use: herdr, with pictures drawn (setUp), and+ // the mod not turned off (/iui off, or off when it starts). The poll ends+ // with the session (stopHost).+ herdrDraws: false,+ isOff: false,+ hostEnded: false,checking: false,// Linux: the tool that reads the desktop portal's colour scheme (host.ts;// null: not tried yet, '': none works, so GNOME's gsettings is read).diff --git a/hooks/register.tsx b/hooks/register.tsxindex 9baa605..e28e59a 100644--- a/hooks/register.tsx+++ b/hooks/register.tsx@@ -31,7 +31,7 @@ import { escapeControls, luminanceOf, modeLine, modeOfLuminance, parseOsc11, setimport { applyTheme, themeListing, userThemesOf } from './lib/themes'import { commandSuggestions, parseCommand, registerCommand } from './lib/command'import { runSetup } from './lib/setup'-import { pollHost, PROBE_RUNS, probeOsc11, showMode, USE_OSC11, watchHost } from './lib/host'+import { pollHost, PROBE_RUNS, probeOsc11, showMode, stopHost, USE_OSC11, watchHost } from './lib/host'import { measureFonts } from './lib/typst'import { drawPages, linkRowsOf, linksFor, takesSelect, type ShownLink } from './lib/draw'import {@@ -799,7 +799,10 @@ export const register: Register = on => {// child starts.on('session.end', async ($, e, next) => {if (e.reason === 'clear' || e.reason === 'resume') stopClipboard($)- else stopWatch()+ else {+ stopWatch()+ stopHost()+ }return next(e)}).catch(($, e, next) => next(e))@@ -892,6 +895,9 @@ export const register: Register = on => {if (asked.kind === 'dump') return { text: await dump($, await read($, isOn)) }if (asked.kind === 'on' || asked.kind === 'off') {settingsSet.add('enabled')+ S.isOff = asked.kind === 'off'+ // Under herdr, its client is checked only while on.+ watchHost($)await update($, isOn, () => asked.kind === 'on')// On: the clipboard part starts with the next picture drawn.if (asked.kind === 'off') {diff --git a/tests/termfont.test.ts b/tests/termfont.test.tsindex fa1d8c4..213e7ad 100644--- a/tests/termfont.test.ts+++ b/tests/termfont.test.ts@@ -36,9 +36,10 @@ function world(on: any, o: {copied?: stringmeasure?: stringrun?: (argv: string[]) => ReturnType<typeof ok> | undefined+ stored?: Record<string, unknown>}): World {S.fontPaths = ''- mock.store(on)+ mock.store(on, o.stored)const clock = mock.clock(on)mock.env(on, { HOME: '/h', ...o.env })const ran: string[][] = []@@ -339,6 +340,63 @@ fontTest('herdr: the check each second runs no full scan; a new client (Ghosttyexpect(w.fontPaths.length).toBeGreaterThan(measured)})+// The herdr checks run since `from` (ran index): light checks and full scans.+const checksSince = (w: World, from: number) => w.ran.slice(from).filter(argv => argv[2] === HERDR_TICK_SH || argv[2] === HERDR_CLIENT_SH).length+// A test that leaves the poll state as it found it.+const pollTest = (name: string, fn: ($: any, on: any) => Promise<void>) =>+ fontTest(name, async ($, on) => {+ try {+ await fn($, on)+ } finally {+ S.isOff = false+ S.hostEnded = false+ }+ })++pollTest('herdr: no check each second while the mod is off; /iui on starts it again', async ($, on) => {+ const w = world(on, { isMac: true, env: HERDR_SERVER_ENV, families: ['Iosevka'], run: argv => clientIs(() => ghosttyClient)(argv) ?? tickIs(() => null)(argv) ?? shows('font-family = Iosevka\n')(argv) })+ await details($)+ let from = w.ran.length+ await w.clock.advance(3000)+ expect(checksSince(w, from)).toBe(3)+ await $.command.run({ command: 'iui', args: 'off' } as any)+ from = w.ran.length+ await w.clock.advance(5000)+ expect(checksSince(w, from)).toBe(0)+ await $.command.run({ command: 'iui', args: 'on' } as any)+ from = w.ran.length+ await w.clock.advance(2000)+ expect(checksSince(w, from)).toBe(2)+})++pollTest('herdr: the mod off when it starts (the stored setting): no check each second', async ($, on) => {+ const w = world(on, { isMac: true, env: HERDR_SERVER_ENV, families: ['Iosevka'], stored: { enabled: false }, run: argv => clientIs(() => ghosttyClient)(argv) ?? tickIs(() => null)(argv) ?? shows('font-family = Iosevka\n')(argv) })+ await details($)+ const from = w.ran.length+ await w.clock.advance(5000)+ expect(checksSince(w, from)).toBe(0)+})++pollTest("herdr with pictures off (no CLAUDE_CODE_FORCE_TERMINAL_IMAGES): no check each second", async ($, on) => {+ const { CLAUDE_CODE_FORCE_TERMINAL_IMAGES: _, ...env } = HERDR_SERVER_ENV+ const w = world(on, { isMac: true, env, families: ['Iosevka'], run: argv => clientIs(() => ghosttyClient)(argv) ?? tickIs(() => null)(argv) ?? shows('font-family = Iosevka\n')(argv) })+ on('tool.call', { tool: 'AskUserQuestion' }, async () => ({ deny: 'dismissed' }))+ await details($)+ const from = w.ran.length+ await w.clock.advance(5000)+ expect(checksSince(w, from)).toBe(0)+})++pollTest('herdr: the end of the session stops the check each second', async ($, on) => {+ const w = world(on, { isMac: true, env: HERDR_SERVER_ENV, families: ['Iosevka'], run: argv => clientIs(() => ghosttyClient)(argv) ?? tickIs(() => null)(argv) ?? shows('font-family = Iosevka\n')(argv) })+ on('session.end', async ($: any, e: any) => ({ sessionId: e.sessionId }))+ await details($)+ await ($ as any).session.end({ reason: 'prompt_input_exit', sessionId: 's1' })+ const from = w.ran.length+ await w.clock.advance(5000)+ expect(checksSince(w, from)).toBe(0)+})+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'