1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! Theme system for TUI customization.
//!
//! Provides color schemes and styling options.
use std::sync::Mutex;
/// Theme colors.
///
/// Mirrors the pi `ThemeJson.colors` tokens (see
/// `.reference/pi/.../theme/dark.json`). The semantic core (`muted`, `accent`,
/// `error`, `success`, `warning`, `border`, `surface`) drives most components;
/// the `md_*` / `tool_*` / `thinking_*` fields correspond 1:1 to the pi md* /
/// toolDiff* / thinking-level tokens and let the markdown renderer and tool
/// blocks match pi's palette instead of hardcoded `Ansi256`/`fg_256` literals.
#[derive(Debug, Clone)]
pub struct ThemeColors {
/// Primary text color.
pub text: Color,
/// Muted/dimmed text color (pi `muted`).
pub muted: Color,
/// Dimmer-than-muted color (pi `dim`).
pub dim: Color,
/// Accent/highlight color (pi `accent`, e.g. teal-ish).
pub accent: Color,
/// Error color.
pub error: Color,
/// Success color.
pub success: Color,
/// Warning color.
pub warning: Color,
/// Info color.
pub info: Color,
/// Background color.
pub background: Color,
/// Primary surface color (pi `userMessageBg`).
pub surface: Color,
/// Border color (pi `border`).
pub border: Color,
/// Accent border color (pi `borderAccent`).
pub border_accent: Color,
/// Muted border color (pi `borderMuted`).
pub border_muted: Color,
/// Selection background color.
pub selection: Color,
/// Cursor color.
pub cursor: Color,
/// Text color for thinking/reasoning blocks (pi `thinkingText`).
pub thinking_text: Color,
// -- Markdown syntax tokens (pi md*) --
/// Heading color (pi `mdHeading`, gold on dark).
pub md_heading: Color,
/// Inline link color (pi `mdLink`).
pub md_link: Color,
/// Link URL color (pi `mdLinkUrl`).
pub md_link_url: Color,
/// Inline `code` color (pi `mdCode`).
pub md_code: Color,
/// Subtle inline-code background. This Rust extension keeps code readable
/// without applying a card background to the whole assistant message.
pub md_code_bg: Color,
/// Code-block body color (pi `mdCodeBlock`).
pub md_code_block: Color,
/// Low-contrast code-block surface (One Dark inspired).
pub md_code_block_bg: Color,
/// Code-block fence/border color (pi `mdCodeBlockBorder`).
pub md_code_block_border: Color,
/// Blockquote body color (pi `mdQuote`).
pub md_quote: Color,
/// Blockquote border color (pi `mdQuoteBorder`).
pub md_quote_border: Color,
/// Horizontal rule color (pi `mdHr`).
pub md_hr: Color,
/// List bullet color (pi `mdListBullet`).
pub md_list_bullet: Color,
// -- Tool execution backgrounds (pi tool*Bg / toolTitle / toolOutput) --
/// Pending-tool background.
pub tool_pending_bg: Color,
/// Successful-tool background.
pub tool_success_bg: Color,
/// Failed-tool background.
pub tool_error_bg: Color,
/// Tool title text color.
pub tool_title: Color,
/// Tool output text color.
pub tool_output: Color,
/// Bash mode accent color (pi `bashMode`).
pub bash_mode: Color,
// -- Custom messages / skill invocation (pi customMessage*) --
/// Skill-invocation / custom-message background (pi `customMessageBg`).
pub custom_message_bg: Color,
/// Skill-invocation `[skill]` label color (pi `customMessageLabel`).
pub custom_message_label: Color,
/// Skill-invocation body text color (pi `customMessageText`).
pub custom_message_text: Color,
// -- Tool diffs (pi toolDiff*) --
/// Added-line color.
pub tool_diff_added: Color,
/// Removed-line color.
pub tool_diff_removed: Color,
/// Context-line color.
pub tool_diff_context: Color,
}
impl Default for ThemeColors {
fn default() -> Self {
// Defaults mirror the pi `dark.json` palette (`accent` teal, the
// `#f0c674` gold heading, `#b5bd68` green code blocks, and the
// `#282832`/`#283228`/`#3c2828` tool backgrounds). Exact pi hexes are
// approximated by their nearest ANSI-256 index so 8/256-color
// terminals reproduce the same relationships; truecolor rendering is
// handled per-`Color` variant at emit time.
Self {
text: Color::Default,
muted: Color::Ansi256(244), // gray (#808080-ish)
dim: Color::Ansi256(242), // dimGray (#666666-ish)
accent: Color::Ansi256(108), // accent teal (#8abeb7) ≈ 108
error: Color::Ansi256(131), // red (#cc6666) ≈ 131
success: Color::Ansi256(107), // green (#b5bd68) ≈ 107
warning: Color::Ansi256(226), // yellow
info: Color::Ansi256(81), // cyan
background: Color::Default,
surface: Color::Ansi256(236), // userMessageBg (#343541) ≈ 236
border: Color::Ansi256(67), // border blue (#5f87ff) ≈ 67
border_accent: Color::Ansi256(45), // borderAccent cyan (#00d7ff) ≈ 45
border_muted: Color::Ansi256(239), // borderMuted darkGray (#505050) ≈ 239
selection: Color::Ansi256(60), // selectedBg (#3a3a4a) ≈ 60
cursor: Color::Ansi256(81),
thinking_text: Color::Ansi256(244), // thinkingText gray
// Markdown
md_heading: Color::Ansi256(179), // gold (#f0c674) ≈ 179
md_link: Color::Ansi256(110), // blue (#81a2be) ≈ 110
md_link_url: Color::Ansi256(242), // dimGray
md_code: Color::Rgb(138, 190, 183), // upstream accent #8abeb7
md_code_bg: Color::Rgb(48, 52, 59), // subtle inline chip
md_code_block: Color::Rgb(212, 212, 212), // neutral code text
md_code_block_bg: Color::Rgb(40, 44, 52), // One Dark #282c34
md_code_block_border: Color::Ansi256(244), // gray
md_quote: Color::Ansi256(244), // gray
md_quote_border: Color::Ansi256(244),
md_hr: Color::Ansi256(244),
md_list_bullet: Color::Ansi256(108), // accent
// Tool blocks
// Keep status backgrounds close to the terminal surface. Status is
// communicated by the icon/foreground; a full saturated green
// panel makes successful tools dominate the transcript.
tool_pending_bg: Color::Rgb(40, 44, 52),
tool_success_bg: Color::Rgb(38, 48, 52), // subtle blue/teal tint
tool_error_bg: Color::Rgb(55, 40, 45), // subtle red tint
tool_title: Color::Default,
tool_output: Color::Ansi256(244),
bash_mode: Color::Ansi256(107), // green
// Custom messages / skill invocation (pi dark.json):
// customMsgBg #2d2838, customMessageLabel #9575cd, text #d4d4d4.
custom_message_bg: Color::Rgb(45, 40, 56),
custom_message_label: Color::Rgb(149, 117, 205),
custom_message_text: Color::Default,
// Tool diffs
tool_diff_added: Color::Ansi256(107), // green
tool_diff_removed: Color::Ansi256(131), // red
tool_diff_context: Color::Ansi256(244), // gray
}
}
}
/// Color representation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Color {
/// Default terminal color.
Default,
/// ANSI 16-color palette.
Ansi16(u8),
/// ANSI 256-color palette.
Ansi256(u8),
/// RGB true color.
Rgb(u8, u8, u8),
}
impl Color {
/// Convert to ANSI foreground escape sequence.
pub fn to_fg(&self) -> String {
match self {
Color::Default => "\x1b[39m".to_string(),
Color::Ansi16(c) => format!("\x1b[{}m", c),
Color::Ansi256(c) => format!("\x1b[38;5;{}m", c),
Color::Rgb(r, g, b) => format!("\x1b[38;2;{};{};{}m", r, g, b),
}
}
/// Convert to ANSI background escape sequence.
pub fn to_bg(&self) -> String {
match self {
Color::Default => "\x1b[49m".to_string(),
Color::Ansi16(c) => format!("\x1b[{}m", c + 10),
Color::Ansi256(c) => format!("\x1b[48;5;{}m", c),
Color::Rgb(r, g, b) => format!("\x1b[48;2;{};{};{}m", r, g, b),
}
}
/// Apply color to text (foreground).
pub fn fg(&self, text: &str) -> String {
format!("{}{}\x1b[0m", self.to_fg(), text)
}
/// Apply color to text (background).
pub fn bg(&self, text: &str) -> String {
let bg = self.to_bg();
// Foreground helpers deliberately terminate with SGR 0. When such a
// styled span is nested in a panel background, that reset also clears
// the background and used to leave the rest of tool/user rows patchy.
// Re-apply this background after every nested reset.
let nested = text.replace("\x1b[0m", &format!("\x1b[0m{bg}"));
format!("{bg}{nested}\x1b[0m")
}
}
/// Theme settings.
#[derive(Debug, Clone)]
pub struct Theme {
/// Color scheme.
pub colors: ThemeColors,
/// Border style.
pub border_style: BorderStyle,
/// Corner style.
pub corner_style: CornerStyle,
}
impl Default for Theme {
fn default() -> Self {
Self {
colors: ThemeColors::default(),
border_style: BorderStyle::Rounded,
corner_style: CornerStyle::Rounded,
}
}
}
/// Border style for boxes.
#[derive(Debug, Clone, Copy, Default)]
pub enum BorderStyle {
#[default]
Rounded,
Sharp,
Double,
Thick,
None,
}
impl BorderStyle {
/// Get the border characters.
pub fn chars(&self) -> BorderChars {
match self {
BorderStyle::Rounded => BorderChars {
top_left: "╭",
top_right: "╮",
bottom_left: "╰",
bottom_right: "╯",
horizontal: "─",
vertical: "│",
},
BorderStyle::Sharp => BorderChars {
top_left: "┌",
top_right: "┐",
bottom_left: "└",
bottom_right: "┘",
horizontal: "─",
vertical: "│",
},
BorderStyle::Double => BorderChars {
top_left: "╔",
top_right: "╗",
bottom_left: "╚",
bottom_right: "╝",
horizontal: "═",
vertical: "║",
},
BorderStyle::Thick => BorderChars {
top_left: "┏",
top_right: "┓",
bottom_left: "┗",
bottom_right: "┛",
horizontal: "━",
vertical: "┃",
},
BorderStyle::None => BorderChars {
top_left: " ",
top_right: " ",
bottom_left: " ",
bottom_right: " ",
horizontal: " ",
vertical: " ",
},
}
}
}
/// Border characters.
pub struct BorderChars {
pub top_left: &'static str,
pub top_right: &'static str,
pub bottom_left: &'static str,
pub bottom_right: &'static str,
pub horizontal: &'static str,
pub vertical: &'static str,
}
/// Corner style.
#[derive(Debug, Clone, Copy, Default)]
pub enum CornerStyle {
#[default]
Rounded,
Sharp,
}
/// Global theme manager.
pub struct ThemeManager {
current: Mutex<Theme>,
}
/// Monotonic revision bumped on every theme mutation.
///
/// Components that memoize rendered output (which bakes in theme colors) key
/// their cache on this value, so a live theme switch invalidates them without
/// every host having to walk the tree and call `invalidate` (upstream's
/// `theme-controller.ts` does both: swap the palette and `ui.invalidate()`).
static THEME_REVISION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
/// Current theme revision. Cache the value at build time and compare on render.
pub fn theme_revision() -> u64 {
THEME_REVISION.load(std::sync::atomic::Ordering::Acquire)
}
impl ThemeManager {
/// Create a new theme manager.
pub fn new() -> Self {
Self {
current: Mutex::new(Theme::default()),
}
}
/// Get the current theme.
pub fn get(&self) -> Theme {
self.current.lock().map(|t| t.clone()).unwrap_or_default()
}
/// Set the theme.
pub fn set(&self, theme: Theme) {
if let Ok(mut current) = self.current.lock() {
*current = theme;
}
THEME_REVISION.fetch_add(1, std::sync::atomic::Ordering::AcqRel);
}
/// Apply a preset theme to this manager.
pub fn apply_preset(&self, preset: ThemePreset) {
self.set(theme_for_preset(preset));
}
}
/// Build a complete theme for a preset.
///
/// Kept separate from [`ThemeManager::apply_preset`] so the process-wide theme
/// used by components can be updated without maintaining a second palette.
fn theme_for_preset(preset: ThemePreset) -> Theme {
match preset {
ThemePreset::Dark => Theme::default(),
ThemePreset::Light => {
let mut colors = ThemeColors::default();
colors.text = Color::Default;
colors.muted = Color::Ansi256(241); // mediumGray (#6c6c6c)
colors.dim = Color::Ansi256(243); // dimGray (#767676)
colors.accent = Color::Ansi256(66); // teal (#5a8080)
colors.error = Color::Ansi256(131); // red (#aa5555)
colors.success = Color::Ansi256(65); // green (#588458)
colors.warning = Color::Ansi256(136); // yellow (#9a7326)
colors.info = Color::Ansi256(67); // blue
colors.background = Color::Default;
colors.surface = Color::Ansi256(254); // userMsgBg (#e8e8e8)
colors.border = Color::Ansi256(67); // blue (#547da7)
colors.border_accent = Color::Ansi256(66); // teal
colors.border_muted = Color::Ansi256(249); // lightGray (#b0b0b0)
colors.selection = Color::Ansi256(189); // selectedBg (#d0d0e0)
colors.cursor = Color::Ansi256(67);
colors.thinking_text = Color::Ansi256(241);
colors.md_heading = Color::Ansi256(136); // yellow
colors.md_link = Color::Ansi256(67); // blue
colors.md_link_url = Color::Ansi256(243);
colors.md_code = Color::Rgb(90, 128, 128); // native light teal
colors.md_code_bg = Color::Rgb(232, 234, 237);
colors.md_code_block = Color::Rgb(31, 35, 40);
colors.md_code_block_bg = Color::Rgb(246, 248, 250); // GitHub light code
colors.md_code_block_border = Color::Ansi256(241);
colors.md_quote = Color::Ansi256(241);
colors.md_quote_border = Color::Ansi256(241);
colors.md_hr = Color::Ansi256(241);
colors.md_list_bullet = Color::Ansi256(65); // green
colors.tool_pending_bg = Color::Ansi256(189);
colors.tool_success_bg = Color::Ansi256(151);
colors.tool_error_bg = Color::Ansi256(181);
colors.tool_title = Color::Default;
colors.tool_output = Color::Ansi256(241);
colors.bash_mode = Color::Ansi256(65);
// Custom messages / skill invocation (pi light.json):
// customMsgBg #ede7f6, customMessageLabel #7e57c2, text #1f2328.
colors.custom_message_bg = Color::Rgb(237, 231, 246);
colors.custom_message_label = Color::Rgb(126, 87, 194);
colors.custom_message_text = Color::Ansi256(237);
colors.tool_diff_added = Color::Ansi256(65);
colors.tool_diff_removed = Color::Ansi256(131);
colors.tool_diff_context = Color::Ansi256(241);
Theme {
colors,
..Default::default()
}
}
ThemePreset::Monochrome => {
let mut colors = ThemeColors::default();
for f in [
&mut colors.accent,
&mut colors.border_accent,
&mut colors.md_code,
&mut colors.md_list_bullet,
&mut colors.bash_mode,
] {
*f = Color::Ansi256(15);
}
colors.muted = Color::Ansi256(244);
colors.dim = Color::Ansi256(242);
colors.error = Color::Ansi256(9);
colors.success = Color::Ansi256(15);
colors.warning = Color::Ansi256(15);
colors.info = Color::Ansi256(15);
colors.surface = Color::Ansi256(236);
colors.border = Color::Ansi256(244);
colors.border_muted = Color::Ansi256(240);
colors.selection = Color::Ansi256(244);
colors.cursor = Color::Ansi256(15);
colors.thinking_text = Color::Ansi256(244);
colors.md_heading = Color::Ansi256(15);
colors.md_link = Color::Ansi256(15);
colors.md_link_url = Color::Ansi256(242);
colors.md_code_bg = Color::Ansi256(237);
colors.md_code_block = Color::Ansi256(15);
colors.md_code_block_bg = Color::Ansi256(235);
colors.md_code_block_border = Color::Ansi256(244);
colors.md_quote = Color::Ansi256(244);
colors.md_quote_border = Color::Ansi256(244);
colors.md_hr = Color::Ansi256(244);
colors.custom_message_bg = Color::Ansi256(236);
colors.custom_message_label = Color::Ansi256(15);
colors.custom_message_text = Color::Ansi256(244);
colors.tool_title = Color::Default;
colors.tool_output = Color::Ansi256(244);
colors.tool_diff_added = Color::Ansi256(15);
colors.tool_diff_removed = Color::Ansi256(9);
colors.tool_diff_context = Color::Ansi256(244);
Theme {
colors,
..Default::default()
}
}
}
}
impl Default for ThemeManager {
fn default() -> Self {
Self::new()
}
}
/// Theme presets.
#[derive(Debug, Clone, Copy)]
pub enum ThemePreset {
Dark,
Light,
Monochrome,
}
/// Global theme helper functions.
pub fn theme() -> Theme {
global_theme_manager().get()
}
/// Apply a preset to the process-wide theme read by every component.
///
/// Creating a standalone [`ThemeManager`] does not affect components that call
/// [`theme`]. Hosts should use this function for live theme switching.
pub fn apply_theme_preset(preset: ThemePreset) {
global_theme_manager().apply_preset(preset);
}
/// Return the process-wide theme manager.
pub fn global_theme_manager() -> &'static ThemeManager {
THEME_MANAGER.get_or_init(ThemeManager::new)
}
/// Global theme manager.
static THEME_MANAGER: std::sync::OnceLock<ThemeManager> = std::sync::OnceLock::new();
/// Apply color from theme.
pub fn themed_text(text: &str, color_fn: fn(&ThemeColors) -> Color) -> String {
let theme = theme();
let color = color_fn(&theme.colors);
color.fg(text)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_color_ansi() {
let color = Color::Ansi256(196);
let text = color.fg("Hello");
assert!(text.contains("Hello"));
}
#[test]
fn test_theme_manager() {
let manager = ThemeManager::new();
manager.apply_preset(ThemePreset::Dark);
let theme = manager.get();
assert!(matches!(theme.colors.accent, Color::Ansi256(108)));
}
}