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
Mode: read the cmux theme from cmux's own Ghostty config
razorback160bac381cf191
diff --git a/docs/how-it-works.md b/docs/how-it-works.mdindex f4920af..77a358c 100644--- a/docs/how-it-works.md+++ b/docs/how-it-works.md@@ -19,7 +19,7 @@ Claude Code finds the light or dark mode for `auto` by asking the terminal for i1. **The iTerm2 profile:** the background colour of the profile in use (`ITERM_PROFILE`, else the default profile). Intelligent UI finds iTerm2 by `TERM_PROGRAM`. If `TERM_PROGRAM` is not set, `ITERM_PROFILE`, `ITERM_SESSION_ID` or `LC_TERMINAL=iTerm2` also show iTerm2. If the profile has separate colours for light and dark mode, Intelligent UI uses the set for iTerm2's theme (Settings > Appearance). If that theme is Automatic, Minimal or Compact, it uses the set for the OS appearance.2. **The Ghostty config:** the `background` that `ghostty +show-config` gives. With a `theme = light:A,dark:B`, Intelligent UI reads the background from the theme file for the OS appearance. With no theme and no `background`, it uses Ghostty's default (`#282c34`, dark).- - **cmux** (Ghostty inside, found by its `CMUX_*` variables) reads the same Ghostty config. If that config sets no `theme`, `background`, `foreground` or `palette`, cmux uses its adaptive default theme. Intelligent UI then uses cmux's appearance: `app.appearance` in `~/.config/cmux/cmux.json`, else the value in cmux's Settings. If that appearance is System, Intelligent UI uses the OS appearance. If `terminal.adaptiveDefaultTheme` is `false` in `cmux.json`, Intelligent UI uses the Ghostty rules above.+ - **cmux** (Ghostty inside, found by its `CMUX_*` variables) reads the same Ghostty config, then its own config, `~/Library/Application Support/com.cmuxterm.app/config.ghostty`. The cmux themes picker (Settings > Themes, `cmux themes`) writes the theme there, and that theme wins over the Ghostty config's. A `light:A,dark:B` theme follows cmux's appearance (see below). If these configs set no `theme`, `background`, `foreground` or `palette`, cmux uses its adaptive default theme. Intelligent UI then uses cmux's appearance: `app.appearance` in `~/.config/cmux/cmux.json`, else the value in cmux's Settings. If that appearance is System, Intelligent UI uses the OS appearance. If `terminal.adaptiveDefaultTheme` is `false` in `cmux.json`, Intelligent UI uses the Ghostty rules above.3. **The kitty config:** the `background` in `kitty.conf` and in the files that it includes (`include` lines only). The themes kitten writes such an include. If the config folder has `dark-theme.auto.conf`, `light-theme.auto.conf` or `no-preference-theme.auto.conf` (kitty 0.38 and later), Intelligent UI uses the file for the OS appearance, as kitty does. With no `background`, it uses kitty's default (`#000000`, dark). The config folder is `KITTY_CONFIG_DIRECTORY`. If that is not set, it is the first of `$XDG_CONFIG_HOME/kitty`, `~/.config/kitty` and (on macOS) `~/Library/Preferences/kitty` that has a `kitty.conf`.4. **The Rio config:** `config.toml` in `RIO_CONFIG_HOME`, else in `~/.config/rio` (on Linux, `$XDG_CONFIG_HOME/rio` if that is set). With an `[adaptive-theme]`, Intelligent UI reads the theme file for `force-theme`, else for the OS appearance. Else it reads the file for `theme`, else the `[colors] background`. Theme files are in the `themes` folder. With no background, it uses Rio's default (`#0F0D0E`, dark).5. **`COLORFGBG`**, as it was when Claude Code started. Ghostty does not set it.diff --git a/hooks/lib/host.ts b/hooks/lib/host.tsindex a278056..6b9fe5a 100644--- a/hooks/lib/host.ts+++ b/hooks/lib/host.ts@@ -9,10 +9,10 @@ import { S } from './state'import { beginEpoch } from './epoch'import { luminanceOf, modeOfColorFgBg, modeOfGnome, modeOfLuminance, modeOfMacAppearance, parseOsc11, resolveMode, type HostMode } from './mode'import {- cmuxAppearanceOf, cmuxSettingsOf, ghosttyBackgroundOf, ghosttyFontOf, ghosttyHasColors, ghosttyThemePaths, ghosttyThemesOf,+ cmuxAppearanceOf, cmuxConfigOf, cmuxGhosttyConfigPath, cmuxSettingsOf, ghosttyBackgroundOf, ghosttyFontOf, ghosttyHasColors, ghosttyThemePaths, ghosttyThemesOf,ghosttyValueOf, hexColorOf, itermAppearanceOf, itermFontOf, itermModeOf, jsoncOf, kittyAutoFileOf, kittyBackgroundOf,kittyConfigDirsOf, kittyEntriesOf, kittyFontOf, kittySchemeOfGnome, kittyValueOf, parseFontSpec, rioBackgroundOf,- rioConfigDirOf, rioFontOf, rioForcedOf, rioThemeNamesOf, rioThemePath, tomlStringsOf, type KittyEntries,+ rioConfigDirOf, rioFontOf, rioForcedOf, rioThemeNamesOf, rioThemePath, tomlStringsOf, type CmuxAppearance, type KittyEntries,} from './terminals'import { firstLine } from './markdown'@@ -180,23 +180,27 @@ export async function probeOsc11($: EngineInterface): Promise<{ reply: string }return got && got.exitCode === 0 ? { reply: got.stdout } : null}-// The background of Ghostty's config. A `light:A,dark:B` theme follows the OS.+// The background of Ghostty's config. A `light:A,dark:B` theme follows the OS+// (in cmux, cmux's appearance). In cmux, cmux's own config is read as well.async function readGhosttyMode($: EngineInterface): Promise<HostMode | null> {if (!/ghostty/i.test(S.termProgram + S.termName)) return nullconst got = await $.process.run(['sh', '-c', GHOSTTY_SH], { timeoutMs: HOST_CHECK_MS }).catch(() => null)if (!got || got.exitCode !== 0) return null- if (S.isCmux && !ghosttyHasColors(got.stdout)) {+ const home = (await $.env.get('HOME')) ?? ''+ const own = S.isCmux ? ((await readText($, cmuxGhosttyConfigPath(home))) ?? '') : ''+ const config = cmuxConfigOf(got.stdout, own)+ if (S.isCmux && !ghosttyHasColors(config)) {const adaptive = await readCmuxMode($).catch(() => null)if (adaptive) return adaptive}- const pair = ghosttyThemesOf(ghosttyValueOf(got.stdout, 'theme'))+ const pair = ghosttyThemesOf(ghosttyValueOf(config, 'theme'))const themeBg: { light?: string; dark?: string } = {}let appearance: IuiTheme | null = nullif (pair) {- appearance = (await readOsMode($).catch(() => null))?.mode ?? null+ appearance = S.isCmux ? ((await readCmuxAppearance($).catch(() => null))?.mode ?? null) : ((await readOsMode($).catch(() => null))?.mode ?? null)const where = {xdgConfig: (await $.env.get('XDG_CONFIG_HOME')) || undefined,- home: (await $.env.get('HOME')) ?? '',+ home,resources: (await $.env.get('GHOSTTY_RESOURCES_DIR')) || undefined,}for (const mode of MODES) {@@ -208,11 +212,11 @@ async function readGhosttyMode($: EngineInterface): Promise<HostMode | null> {}}}- const bg = hexColorOf(ghosttyBackgroundOf(got.stdout, appearance, themeBg) ?? '')+ const bg = hexColorOf(ghosttyBackgroundOf(config, appearance, themeBg) ?? '')if (!bg) return null- const shown = ghosttyValueOf(got.stdout, 'background')- const detail = `${!shown ? 'default' : pair && appearance ? `theme ${pair[appearance]}` : 'config'} background`- return { mode: modeOfLuminance(luminanceOf(bg)), source: 'Ghostty', detail }+ const shown = ghosttyValueOf(config, 'background')+ const detail = `${pair && appearance ? `theme ${pair[appearance]}` : !shown ? 'default' : 'config'} background`+ return { mode: modeOfLuminance(luminanceOf(bg)), source: ghosttyValueOf(own, 'theme') ? 'cmux' : 'Ghostty', detail }}// cmux with no colours in the Ghostty config: its adaptive default theme@@ -222,14 +226,25 @@ async function readCmuxMode($: EngineInterface): Promise<HostMode | null> {const home = (await $.env.get('HOME')) ?? ''const settings = cmuxSettingsOf(jsoncOf((await readText($, `${home}/.config/cmux/cmux.json`)) ?? ''))if (settings.adaptive === false) return null+ const seen = await readCmuxAppearance($, settings.appearance)+ return seen ? { mode: seen.mode, source: 'cmux', detail: `adaptive default theme, appearance ${seen.appearance}` } : null+}++// cmux's appearance (cmux.json's, else the one saved in Settings) and the+// mode it gives: the OS's when it is system. Null when that is not known.+async function readCmuxAppearance($: EngineInterface, file?: CmuxAppearance): Promise<{ mode: IuiTheme; appearance: CmuxAppearance } | null> {+ if (file === undefined) {+ const home = (await $.env.get('HOME')) ?? ''+ file = cmuxSettingsOf(jsoncOf((await readText($, `${home}/.config/cmux/cmux.json`)) ?? '')).appearance+ }let saved = ''- if (!settings.appearance && S.hostOs === 'Darwin') {+ if (!file && S.hostOs === 'Darwin') {const got = await $.process.run(['defaults', 'read', 'com.cmuxterm.app', 'appearanceMode'], { timeoutMs: HOST_CHECK_MS }).catch(() => null)saved = got?.exitCode === 0 ? got.stdout : ''}- const appearance = cmuxAppearanceOf(settings.appearance, saved)+ const appearance = cmuxAppearanceOf(file, saved)const mode = appearance === 'system' ? ((await readOsMode($).catch(() => null))?.mode ?? null) : appearance- return mode ? { mode, source: 'cmux', detail: `adaptive default theme, appearance ${appearance}` } : null+ return mode ? { mode, appearance } : null}// Rio sets TERM_PROGRAM=rio, and TERM=rio when its terminfo is installed.diff --git a/hooks/lib/terminals.ts b/hooks/lib/terminals.tsindex e287977..30cdd86 100644--- a/hooks/lib/terminals.ts+++ b/hooks/lib/terminals.ts@@ -391,6 +391,22 @@ export function cmuxAppearanceOf(file: CmuxAppearance | undefined, saved: stringreturn file ?? (v === 'light' || v === 'dark' ? v : 'system')}+// cmux's own Ghostty config: its themes picker (Settings > Themes, `cmux+// themes`) writes `theme = …` there, and cmux loads it after the Ghostty+// config files. `ghostty +show-config` does not read it.+export const cmuxGhosttyConfigPath = (home: string) => `${home}/Library/Application Support/com.cmuxterm.app/config.ghostty`++// The config cmux draws with: `ghostty +show-config`, then cmux's own config+// (its values win, as the last value of a key does). One theme name there+// becomes `light:X,dark:X`: +show-config did not merge its colours in, so+// ghosttyBackgroundOf reads its background from the theme file.+export function cmuxConfigOf(shown: string, own: string): string {+ if (!own.trim()) return shown+ const theme = ghosttyValueOf(own, 'theme')+ if (!theme || ghosttyThemesOf(theme)) return `${shown}\n${own}`+ return `${shown}\n${own}\ntheme = light:${theme},dark:${theme}`+}+// Whether `ghostty +show-config` shows colours of the user's own (a theme,// a background, a foreground or a palette). cmux then uses them; else its// adaptive default theme follows cmux's appearance.diff --git a/tests/mode.test.ts b/tests/mode.test.tsindex 57b9e59..d1afba3 100644--- a/tests/mode.test.ts+++ b/tests/mode.test.ts@@ -219,10 +219,11 @@ for (const colorFgBg of [undefined, '0;15']) {// cmux (Ghostty inside) with no colours in the Ghostty config: its adaptive// default theme follows cmux's appearance; turned off, Ghostty's dark default.+// A theme in cmux's own config (its themes picker) wins.test('with the theme on auto, cmux draws as its appearance is', async ($, on) => {mock.store(on)const clock = mock.clock(on)- mock.env(on, { HOME: '/h', TERM_PROGRAM: 'ghostty', TERM: 'xterm-ghostty', CMUX_SURFACE_ID: 'S1' })+ mock.env(on, { HOME: '/h', TERM_PROGRAM: 'ghostty', TERM: 'xterm-ghostty', CMUX_SURFACE_ID: 'S1', GHOSTTY_RESOURCES_DIR: '/r' })on('config.list', async () => ({ value: [{ key: 'theme', value: 'auto' }] as any }))let saved = 'system\n'const ok = (stdout: string, exitCode = 0) => ({ value: { exitCode, stdout, stderr: '', isStdoutTruncated: false, isStderrTruncated: false } })@@ -236,7 +237,15 @@ test('with the theme on auto, cmux draws as its appearance is', async ($, on) =>return next(e)})let cmuxJson: string | null = null- on('fs.read', async ($: any, e: any, next: any) => (e.path === '/h/.config/cmux/cmux.json' ? (cmuxJson === null ? ({ deny: 'no such file' } as any) : { value: cmuxJson }) : next(e)))+ let cmuxConfig: string | null = null+ const files: Record<string, string> = { '/r/themes/Paper': 'background = #fafafa\n', '/r/themes/Ink': 'background = #101010\n' }+ on('fs.read', async ($: any, e: any, next: any) => {+ if (e.path === '/h/.config/cmux/cmux.json') return cmuxJson === null ? ({ deny: 'no such file' } as any) : { value: cmuxJson }+ if (e.path === '/h/Library/Application Support/com.cmuxterm.app/config.ghostty') return cmuxConfig === null ? ({ deny: 'no such file' } as any) : { value: cmuxConfig }+ if (e.path in files) return { value: files[e.path] }+ if (e.path.startsWith('/h/') || e.path.startsWith('/r/')) return { deny: 'no such file' } as any+ return next(e)+ })const ran = await $.command.run({ command: 'iui', args: 'details' } as any)expect(ran.text).toContain('mode light (auto, from cmux: adaptive default theme, appearance system)')saved = 'dark\n'@@ -246,6 +255,18 @@ test('with the theme on auto, cmux draws as its appearance is', async ($, on) =>cmuxJson = '{\n // off\n "terminal": { "adaptiveDefaultTheme": false, },\n}'await clock.advance(3000)expect((await $.command.run({ command: 'iui', args: 'details' } as any)).text).toContain('mode dark (auto, from Ghostty: default background)')+ // The cmux themes picker writes cmux's own config: one theme, then a+ // light:dark pair that follows cmux's appearance (system: the OS, light).+ cmuxJson = null+ cmuxConfig = '# cmux themes start\ntheme = Paper\n# cmux themes end\n'+ await clock.advance(3000)+ expect((await $.command.run({ command: 'iui', args: 'details' } as any)).text).toContain('mode light (auto, from cmux: theme Paper background)')+ cmuxConfig = '# cmux themes start\ntheme = light:Ink,dark:Paper\n# cmux themes end\n'+ await clock.advance(3000)+ expect((await $.command.run({ command: 'iui', args: 'details' } as any)).text).toContain('mode dark (auto, from cmux: theme Ink background)')+ saved = 'dark\n'+ await clock.advance(3000)+ expect((await $.command.run({ command: 'iui', args: 'details' } as any)).text).toContain('mode light (auto, from cmux: theme Paper background)')})// iTerm2 whose TERM_PROGRAM did not come through: ITERM_PROFILE says iTerm2.diff --git a/tests/terminals.test.ts b/tests/terminals.test.tsindex 38982ed..9244c69 100644--- a/tests/terminals.test.ts+++ b/tests/terminals.test.ts@@ -1,7 +1,7 @@import { expect, test } from 'claude-code/testing'import {- cmuxAppearanceOf, cmuxSettingsOf, ghosttyHasColors, isItermIn, jsoncOf,+ cmuxAppearanceOf, cmuxConfigOf, cmuxGhosttyConfigPath, cmuxSettingsOf, ghosttyBackgroundOf, ghosttyHasColors, ghosttyThemesOf, ghosttyValueOf, isItermIn, jsoncOf,KITTY_DEFAULT_BG, kittyAutoFileOf, kittyBackgroundOf, kittyConfigDirsOf, kittyEntriesOf, kittyFontOf, kittyIncludePath, kittyLinesOf,kittySchemeOfGnome, kittyValueOf,RIO_DEFAULT_BG, RIO_DEFAULT_FONT, rioBackgroundOf, rioConfigDirOf, rioFontOf, rioForcedOf, rioThemeNamesOf, rioThemePath, tomlStringsOf,@@ -201,3 +201,24 @@ test('Ghostty colours of your own: a theme, background, foreground or palette',expect(ghosttyHasColors('foreground = #000000\n')).toBe(true)expect(ghosttyHasColors('palette = 0=#000000\n')).toBe(true)})++test("cmux's own Ghostty config: its theme wins; one theme name becomes a light:dark pair", () => {+ expect(cmuxGhosttyConfigPath('/Users/me')).toBe('/Users/me/Library/Application Support/com.cmuxterm.app/config.ghostty')+ const shown = 'font-family = Menlo\n'+ expect(cmuxConfigOf(shown, '')).toBe(shown)+ expect(cmuxConfigOf(shown, ' \n')).toBe(shown)+ const block = (theme: string) => `# cmux themes start\ntheme = ${theme}\n# cmux themes end\n`+ // A pair as written.+ const pair = cmuxConfigOf(shown, block('light:Apple System Colors Light,dark:Dracula'))+ expect(ghosttyThemesOf(ghosttyValueOf(pair, 'theme'))).toEqual({ light: 'Apple System Colors Light', dark: 'Dracula' })+ expect(ghosttyHasColors(pair)).toBe(true)+ // One name: both modes, so the background comes from the theme file.+ const one = cmuxConfigOf(shown, block('Dracula'))+ expect(ghosttyThemesOf(ghosttyValueOf(one, 'theme'))).toEqual({ light: 'Dracula', dark: 'Dracula' })+ expect(ghosttyBackgroundOf(one, 'light', { light: '#282a36', dark: '#282a36' })).toBe('#282a36')+ // Over the Ghostty config's theme.+ const over = cmuxConfigOf('theme = Night\nbackground = #000000\n', block('light:A,dark:B'))+ expect(ghosttyValueOf(over, 'theme')).toBe('light:A,dark:B')+ // No theme there: the Ghostty config as shown, with its other values.+ expect(ghosttyHasColors(cmuxConfigOf(shown, 'font-size = 13\n'))).toBe(false)+})