Shares the wezterm color scheme with other mods as $.palette

Shares the terminal's color scheme with every Claude Code mod. Mods cannot import code from one another's folders, so the scheme lives in one shared service on $, and each pane or band paints with the same colors as wezterm.
The colors come from wezterm's nordic scheme, so the Claude Code UI matches the terminal. The mod keeps no state and draws nothing of its own.
Another mod lists palette under dependencies in its plugin.json, then reads the theme with $.palette.get():
const theme = await $.palette.get();
const accent = theme.ansi.blue;
Used by session (the side pane).
hooks/register.ts 102 lines1import type { EngineInterface, Register } from 'claude-code'
2
3import type { Palette, PaletteColors, PaletteTheme } from '../types'
4
5// relative to $HOME
6const SCHEME = '.bashrc.d/wezterm/colors/nordic.toml'
7const SLOTS = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'] as const
8const SCALARS = ['foreground', 'background', 'cursor_bg', 'cursor_border', 'cursor_fg', 'selection_fg', 'selection_bg'] as const
9
10// full copy of wezterm/colors/nordic.toml (nordic.nvim's generated scheme, fetched by `dots link`), used field by field where
11// the file is missing or lacks one; refresh it when that file changes. `comment` is nordic.nvim's gray5, which the toml has no
12// slot for (nvim's own comment color is the darker gray4, #4C566A)
13export const FALLBACK: PaletteTheme = {
14 foreground: '#C0C8D8',
15 background: '#242933',
16 cursor_bg: '#D8DEE9',
17 cursor_border: '#D8DEE9',
18 cursor_fg: '#242933',
19 selection_fg: '#D8DEE9',
20 selection_bg: '#2E3440',
21 ansi: {
22 black: '#191D24',
23 red: '#BF616A',
24 green: '#A3BE8C',
25 yellow: '#EBCB8B',
26 blue: '#5E81AC',
27 magenta: '#B48EAD',
28 cyan: '#8FBCBB',
29 white: '#C0C8D8',
30 },
31 brights: {
32 black: '#3B4252',
33 red: '#C5727A',
34 green: '#B1C89D',
35 yellow: '#EFD49F',
36 blue: '#88C0D0',
37 magenta: '#BE9DB8',
38 cyan: '#9FC6C5',
39 white: '#D8DEE9',
40 },
41 comment: '#60728A',
42 dimmed: '#727986',
43}
44
45const HEX = /"(#[0-9A-Fa-f]{6})"/g
46
47// the color halfway between two "#rrggbb"
48export const mixColors = (firstHex: string, secondHex: string) => {
49 const channels = (hex: string) => [1, 3, 5].map(start => parseInt(hex.slice(start, start + 2), 16))
50 const secondChannels = channels(secondHex)
51 return `#${channels(firstHex)
52 .map((value, index) => Math.round((value + secondChannels[index]!) / 2).toString(16).padStart(2, '0'))
53 .join('')}`
54}
55
56// ponytail: reads only the shapes a wezterm scheme uses (key = "#hex", key = [ "#hex", ... ]), not general TOML
57export const parseScheme = (text: string): PaletteTheme => {
58 const scalar = (key: string) => text.match(new RegExp(`^\\s*${key}\\s*=\\s*"(#[0-9A-Fa-f]{6})"`, 'm'))?.[1]
59 const array = (key: 'ansi' | 'brights'): PaletteColors => {
60 const body = text.match(new RegExp(`^\\s*${key}\\s*=\\s*\\[([^\\]]*)\\]`, 'm'))?.[1] ?? ''
61 const hex = [...body.matchAll(HEX)].map(match => match[1])
62 return Object.fromEntries(
63 SLOTS.map((slot, slotIndex) => [slot, hex[slotIndex] ?? FALLBACK[key][slot]]),
64 ) as PaletteColors
65 }
66 const theme = { ...FALLBACK, ansi: array('ansi'), brights: array('brights') }
67 for (const key of SCALARS) theme[key] = scalar(key) ?? FALLBACK[key]
68 theme.dimmed = mixColors(theme.foreground, theme.background)
69 return theme
70}
71
72// rejects when HOME is unset or the file cannot be read (missing, or the calling turn was stopped)
73async function load($: EngineInterface): Promise<PaletteTheme> {
74 const home = await $.env.get('HOME')
75 if (!home) throw new Error('HOME is unset')
76 return parseScheme(await $.fs.read(`${home}/${SCHEME}`))
77}
78
79export const register: Register = on => {
80 let cached: Promise<PaletteTheme> | undefined
81
82 // the noun's own body is the bottom of its chain: the built-in copy
83 on('engine.create', async (_, event, next) => {
84 const palette: Palette = { get: async () => FALLBACK }
85
86 return { ...(await next(event)), palette }
87 })
88
89 // a call on $.palette.get runs this first, with a full $ to read the scheme file
90 // only a successful read is kept: a failed one (a stopped turn rejects it) is retried by the next call
91 // ponytail: a missing scheme file is re-read on every call, cache the miss if that ever shows in a profile
92 on('palette.get', async $ => {
93 cached ??= load($)
94 try {
95 return { value: await cached }
96 } catch {
97 cached = undefined
98 return { value: FALLBACK }
99 }
100 })
101}
102types/index.d.ts 43 lines1// the 8 ANSI slots of a wezterm `ansi` or `brights` array, in order
2export type PaletteColors = {
3 black: string
4 red: string
5 green: string
6 yellow: string
7 blue: string
8 magenta: string
9 cyan: string
10 white: string
11}
12
13// wezterm's [colors] table, plus `comment`, the theme's gray the toml has no slot for,
14// and `dimmed`, halfway between foreground and background: a dim text's look as a plain color
15export type PaletteTheme = {
16 foreground: string
17 background: string
18 cursor_bg: string
19 cursor_border: string
20 cursor_fg: string
21 selection_fg: string
22 selection_bg: string
23 ansi: PaletteColors
24 brights: PaletteColors
25 comment: string
26 dimmed: string
27}
28
29// a color as the palette names it, what a consumer's tables hold: `ansi.blue`, `brights.yellow`, `comment`
30export type PaletteSingleColor = 'foreground' | 'background' | 'comment' | 'dimmed'
31export type PaletteColorPath = `ansi.${keyof PaletteColors}` | `brights.${keyof PaletteColors}` | PaletteSingleColor
32
33export type Palette = {
34 // read once per load from the wezterm scheme file, the built-in copy where it cannot be read
35 get: () => Promise<PaletteTheme>
36}
37
38declare module 'claude-code' {
39 interface EngineInterface {
40 palette: Palette
41 }
42}
43