xonecas.io
79113
Ma

mini-coding-agent / commit abed8fe

feat: colorscheme overrides

xonecas2026-10-26 abed8fe8ea8b821375960a44e576bfb1303663c0 parent 155b63d

7 files changed

README.md+2−0
@@ -118,6 +118,7 @@ A full example:
118118 "snapshotDiffsInContext": false,
119119 "historySize": 100,
120120 "maxDiffLines": 2000,
121+ "colorscheme": { "prompt": "#5f87ff", "err": "#ff5f5f" },
121122 "customProviders": [
122123 {
123124 "id": "local",
@@ -164,6 +165,7 @@ Keys:
164165 | `snapshotDiffsInContext` | `false` | send the `bash` tool's snapshot diffs to the model inside the tool result |
165166 | `historySize` | `100` | past inputs kept in `<state dir>/mini-coding-agent/history` for `Shift+Up`/`Shift+Down` and `Ctrl+R` |
166167 | `maxDiffLines` | `2000` | diffs longer than this render as a "too large" notice instead of highlighting every line |
168+| `colorscheme` | `{}` | per-role `#rrggbb` foreground overrides; roles are `comment`, `prompt`, `accent`, `err`, `warn`, `code`, `diff_add`, `diff_delete`, `inline_code`, `link`, `uri`, `heading1`-`heading6`; a role left out keeps its terminal palette color |
167169
168170 Unknown keys are rejected. `provider` and `model` must both be set to make a
169171 request; a custom provider must list the model in its `models`. Config errors
src/config.zig+41−0
@@ -32,6 +32,33 @@ pub const CustomProvider = struct {
3232 headers: ?std.json.ArrayHashMap([]const u8) = null,
3333 };
3434
35+pub const Colorscheme = struct {
36+ comment: ?[]const u8 = null,
37+ prompt: ?[]const u8 = null,
38+ accent: ?[]const u8 = null,
39+ err: ?[]const u8 = null,
40+ warn: ?[]const u8 = null,
41+ code: ?[]const u8 = null,
42+ diff_add: ?[]const u8 = null,
43+ diff_delete: ?[]const u8 = null,
44+ inline_code: ?[]const u8 = null,
45+ link: ?[]const u8 = null,
46+ uri: ?[]const u8 = null,
47+ heading1: ?[]const u8 = null,
48+ heading2: ?[]const u8 = null,
49+ heading3: ?[]const u8 = null,
50+ heading4: ?[]const u8 = null,
51+ heading5: ?[]const u8 = null,
52+ heading6: ?[]const u8 = null,
53+};
54+
55+/// A `#rrggbb` override. The TUI only ever sees the parsed value.
56+pub fn parseHex(text: []const u8) ?u24 {
57+ const body = if (text.len > 0 and text[0] == '#') text[1..] else text;
58+ if (body.len != 6) return null;
59+ return std.fmt.parseInt(u24, body, 16) catch null;
60+}
61+
3562 pub const Config = struct {
3663 path: []const u8,
3764 sessions_dir: []const u8,
@@ -51,6 +78,7 @@ pub const Config = struct {
5178 snapshot_diffs_in_context: bool,
5279 history_size: usize,
5380 max_diff_lines: usize,
81+ colorscheme: Colorscheme,
5482 };
5583
5684 const File = struct {
@@ -71,6 +99,7 @@ const File = struct {
7199 snapshotIgnoreDirs: ?[]const []const u8 = null,
72100 snapshotUsesGitignore: ?bool = null,
73101 snapshotDiffsInContext: ?bool = null,
102+ colorscheme: ?Colorscheme = null,
74103 };
75104
76105 /// An empty environment variable is treated as unset, per the XDG spec.
@@ -152,6 +181,17 @@ fn readInto(a: std.mem.Allocator, path: []const u8, required: bool, cfg: *Config
152181 if (file.snapshotDiffsInContext) |v| cfg.snapshot_diffs_in_context = v;
153182 if (file.historySize) |v| cfg.history_size = v;
154183 if (file.maxDiffLines) |v| cfg.max_diff_lines = v;
184+ if (file.colorscheme) |scheme| {
185+ inline for (@typeInfo(Colorscheme).@"struct".field_names) |name| {
186+ if (@field(scheme, name)) |hex| {
187+ if (parseHex(hex) == null) {
188+ platform.printErr("config {s}: colorscheme.{s}: expected \"#rrggbb\"\n", .{ path, name });
189+ return error.InvalidConfig;
190+ }
191+ @field(cfg.colorscheme, name) = hex;
192+ }
193+ }
194+ }
155195 }
156196
157197 pub const Update = struct {
@@ -206,5 +246,6 @@ fn defaults(a: std.mem.Allocator) !Config {
206246 .snapshot_diffs_in_context = false,
207247 .history_size = 100,
208248 .max_diff_lines = 2000,
249+ .colorscheme = .{},
209250 };
210251 }
src/tui/diff_view.zig+3−3
@@ -13,7 +13,7 @@ pub const Rendered = struct {
1313 };
1414
1515 fn diffHunk() styles.Style {
16- return .{ .fg = styles.roles.prompt };
16+ return styles.roles.prompt;
1717 }
1818
1919 fn diffNote() styles.Style {
@@ -46,8 +46,8 @@ pub fn diffLines(a: std.mem.Allocator, lines: []const []const u8, max_diff_lines
4646 .add, .del => {
4747 const is_add = kind == .add;
4848 if (is_add) added += 1 else removed += 1;
49- const fg = if (is_add) styles.roles.diff_add else styles.roles.diff_delete;
50- out.append(a, .{ .text = text, .style = .{ .fg = fg } }) catch {};
49+ const style = if (is_add) styles.roles.diff_add else styles.roles.diff_delete;
50+ out.append(a, .{ .text = text, .style = style }) catch {};
5151 },
5252 }
5353 }
src/tui/highlight.zig+2−3
@@ -258,8 +258,7 @@ fn markdownLine(a: std.mem.Allocator, line: []const u8, base: u32, spans: *std.A
258258 // Headings and rules keep their style when quoted or listed.
259259 const rest = line[start..];
260260 if (headingLevel(rest)) |level| {
261- const idx = @min(level, styles.roles.headings.len) - 1;
262- addSpan(a, spans, base, start, limit, styles.roles.headings[idx]);
261+ addSpan(a, spans, base, start, limit, styles.roles.heading(level));
263262 return;
264263 }
265264 if (isFenceMarker(rest) or isRule(rest)) {
@@ -285,7 +284,7 @@ pub fn highlightMarkdown(a: std.mem.Allocator, text: []const u8) []const u8 {
285284 pub fn highlightCode(a: std.mem.Allocator, text: []const u8) []const u8 {
286285 // Stop before the block's trailing newline so the style does not leak a dangling SGR.
287286 const end = if (text.len > 0 and text[text.len - 1] == '\n') text.len - 1 else text.len;
288- const spans = [_]Span{.{ .start = 0, .end = @intCast(end), .style = .{ .fg = styles.roles.code } }};
287+ const spans = [_]Span{.{ .start = 0, .end = @intCast(end), .style = styles.roles.code }};
289288 return paint(a, text, &spans, .{});
290289 }
291290
src/tui/screen.zig+1−1
@@ -127,7 +127,7 @@ pub const Scrollback = struct {
127127 self.separator = true;
128128 var it = std.mem.splitScalar(u8, text, '\n');
129129 var lines: std.ArrayList(render.BodyLine) = .empty;
130- while (it.next()) |l| lines.append(self.scratch, .{ .text = l, .style = .{ .fg = styles.roles.prompt } }) catch {};
130+ while (it.next()) |l| lines.append(self.scratch, .{ .text = l, .style = styles.roles.prompt }) catch {};
131131 self.commitLines(lines.items);
132132 self.separator = true;
133133 }
src/tui/styles.zig+70−44
@@ -1,8 +1,9 @@
11 const std = @import("std");
2+const config = @import("../config.zig");
23
34 /// The terminal's own palette. Indices 0-7 are its normal colors and 8-15 the bright
45 /// ones, so the interface inherits whatever scheme the host terminal is set up with.
5-pub const Color = enum(u8) {
6+pub const Palette = enum(u8) {
67 black = 0,
78 red,
89 green,
@@ -19,11 +20,12 @@ pub const Color = enum(u8) {
1920 bright_magenta,
2021 bright_cyan,
2122 bright_white,
23+};
2224
23- fn fgCode(self: Color) u8 {
24- const n: u8 = @backingInt(self);
25- return if (n < 8) 30 + n else 90 + n - 8;
26- }
25+/// A palette entry unless the config overrides the role with a `#rrggbb`.
26+pub const Color = union(enum) {
27+ palette: Palette,
28+ rgb: u24,
2729 };
2830
2931 pub const Style = struct {
@@ -35,33 +37,68 @@ pub const Style = struct {
3537 };
3638
3739 /// Every role resolves to the terminal's default foreground unless it asks otherwise.
38-pub const roles = struct {
39- pub const comment: Style = .{ .fg = .bright_black, .dim = true };
40- pub const prompt = Color.blue;
41- pub const accent = Color.cyan;
42- pub const err = Color.red;
43- pub const warn = Color.yellow;
44- pub const code = Color.bright_black;
45- pub const diff_add = Color.green;
46- pub const diff_delete = Color.red;
47- pub const inline_code: Style = .{ .fg = .cyan };
48- pub const link: Style = .{ .fg = .blue, .underline = true };
49- pub const uri: Style = .{ .underline = true };
50- pub const headings = [_]Style{
51- .{ .fg = .bright_blue, .bold = true },
52- .{ .fg = .bright_cyan, .bold = true },
53- .{ .fg = .bright_magenta, .bold = true },
54- .{ .fg = .bright_green, .bold = true },
55- .{ .fg = .bright_yellow, .bold = true },
56- .{ .fg = .bright_red, .bold = true },
57- };
40+pub const Roles = struct {
41+ comment: Style = .{ .fg = .{ .palette = .bright_black }, .dim = true },
42+ prompt: Style = .{ .fg = .{ .palette = .blue } },
43+ accent: Style = .{ .fg = .{ .palette = .cyan } },
44+ err: Style = .{ .fg = .{ .palette = .red } },
45+ warn: Style = .{ .fg = .{ .palette = .yellow } },
46+ code: Style = .{ .fg = .{ .palette = .bright_black } },
47+ diff_add: Style = .{ .fg = .{ .palette = .green } },
48+ diff_delete: Style = .{ .fg = .{ .palette = .red } },
49+ inline_code: Style = .{ .fg = .{ .palette = .cyan } },
50+ link: Style = .{ .fg = .{ .palette = .blue }, .underline = true },
51+ uri: Style = .{ .underline = true },
52+ heading1: Style = .{ .fg = .{ .palette = .bright_blue }, .bold = true },
53+ heading2: Style = .{ .fg = .{ .palette = .bright_cyan }, .bold = true },
54+ heading3: Style = .{ .fg = .{ .palette = .bright_magenta }, .bold = true },
55+ heading4: Style = .{ .fg = .{ .palette = .bright_green }, .bold = true },
56+ heading5: Style = .{ .fg = .{ .palette = .bright_yellow }, .bold = true },
57+ heading6: Style = .{ .fg = .{ .palette = .bright_red }, .bold = true },
58+
59+ pub fn heading(self: Roles, level: usize) Style {
60+ return switch (level) {
61+ 1 => self.heading1,
62+ 2 => self.heading2,
63+ 3 => self.heading3,
64+ 4 => self.heading4,
65+ 5 => self.heading5,
66+ else => self.heading6,
67+ };
68+ }
5869 };
5970
71+pub var roles: Roles = .{};
72+
73+/// Each role the config names takes its `#rrggbb` as foreground. Field names line up.
74+pub fn applyScheme(scheme: config.Colorscheme) void {
75+ inline for (@typeInfo(config.Colorscheme).@"struct".field_names) |name| {
76+ if (@field(scheme, name)) |hex| @field(roles, name).fg = .{ .rgb = config.parseHex(hex).? };
77+ }
78+}
79+
6080 pub const SGR_PLAIN = "\x1b[0m";
61-const SGR_NORMAL_FG = "\x1b[39m";
6281
63-fn sgrFg(a: std.mem.Allocator, color: Color) []const u8 {
64- return std.fmt.allocPrint(a, "\x1b[{d}m", .{color.fgCode()}) catch "";
82+fn appendFg(a: std.mem.Allocator, out: *std.ArrayList(u8), color: Color) void {
83+ const text = switch (color) {
84+ .palette => |p| blk: {
85+ const n: u8 = @backingInt(p);
86+ break :blk std.fmt.allocPrint(a, ";{d}", .{if (n < 8) 30 + n else 90 + n - 8});
87+ },
88+ .rgb => |v| std.fmt.allocPrint(a, ";38;2;{d};{d};{d}", .{ v >> 16 & 0xff, v >> 8 & 0xff, v & 0xff }),
89+ } catch return;
90+ out.appendSlice(a, text) catch {};
91+}
92+
93+/// Renders `style` on top of `base`, so a span keeps the attributes of the line it sits in.
94+pub fn over(style: Style, base: Style) Style {
95+ return .{
96+ .fg = style.fg orelse base.fg,
97+ .bold = style.bold or base.bold,
98+ .italic = style.italic or base.italic,
99+ .underline = style.underline or base.underline,
100+ .dim = style.dim or base.dim,
101+ };
65102 }
66103
67104 fn sgr(a: std.mem.Allocator, style: Style) []const u8 {
@@ -71,20 +108,13 @@ fn sgr(a: std.mem.Allocator, style: Style) []const u8 {
71108 if (style.italic) out.appendSlice(a, ";3") catch {};
72109 if (style.underline) out.appendSlice(a, ";4") catch {};
73110 if (style.dim) out.appendSlice(a, ";2") catch {};
74- if (style.fg) |c| out.appendSlice(a, std.fmt.allocPrint(a, ";{d}", .{c.fgCode()}) catch "") catch {};
111+ if (style.fg) |c| appendFg(a, &out, c);
75112 out.append(a, 'm') catch {};
76113 return out.items;
77114 }
78115
79-/// Renders `style` on top of `base`, so a span keeps the attributes of the line it sits in.
80116 pub fn sgrOn(a: std.mem.Allocator, style: Style, base: Style) []const u8 {
81- return sgr(a, .{
82- .fg = style.fg orelse base.fg,
83- .bold = style.bold or base.bold,
84- .italic = style.italic or base.italic,
85- .underline = style.underline or base.underline,
86- .dim = style.dim or base.dim,
87- });
117+ return sgr(a, over(style, base));
88118 }
89119
90120 pub fn styledWith(a: std.mem.Allocator, style: Style, text: []const u8) []const u8 {
@@ -95,22 +125,18 @@ pub fn anySet(style: Style) bool {
95125 return style.fg != null or style.bold or style.italic or style.underline or style.dim;
96126 }
97127
98-fn colored(a: std.mem.Allocator, color: Color, text: []const u8) []const u8 {
99- return std.fmt.allocPrint(a, "{s}{s}{s}", .{ sgrFg(a, color), text, SGR_NORMAL_FG }) catch text;
100-}
101-
102128 pub fn comment(a: std.mem.Allocator, text: []const u8) []const u8 {
103129 return styledWith(a, roles.comment, text);
104130 }
105131
106132 pub fn err(a: std.mem.Allocator, text: []const u8) []const u8 {
107- return colored(a, roles.err, text);
133+ return styledWith(a, roles.err, text);
108134 }
109135
110136 pub fn warn(a: std.mem.Allocator, text: []const u8) []const u8 {
111- return colored(a, roles.warn, text);
137+ return styledWith(a, roles.warn, text);
112138 }
113139
114140 pub fn accent(a: std.mem.Allocator, text: []const u8) []const u8 {
115- return colored(a, roles.accent, text);
141+ return styledWith(a, roles.accent, text);
116142 }
src/tui/tui.zig+10−9
@@ -262,33 +262,33 @@ const Tui = struct {
262262 fn stateLabel(self: *Tui) []const u8 {
263263 const a = self.s;
264264 var text: []const u8 = "idle";
265- var color: ?styles.Color = null;
265+ var role: ?styles.Style = null;
266266 var bytes: usize = 0;
267267 switch (self.phase) {
268268 .preparing => text = "preparing",
269269 .waiting_model => text = "waiting for provider",
270270 .snapshotting => {
271271 text = "snapshotting";
272- color = styles.roles.warn;
272+ role = styles.roles.warn;
273273 },
274274 .running_tool => {
275275 text = std.fmt.allocPrint(a, "running tool call {s}", .{self.detail orelse "tool"}) catch "running tool call";
276- color = styles.roles.prompt;
276+ role = styles.roles.prompt;
277277 },
278278 .streaming => switch (self.stream_kind) {
279279 .tool => {
280280 text = std.fmt.allocPrint(a, "writing tool call {s}", .{self.writing_tool orelse "tool"}) catch "writing tool call";
281- color = styles.roles.prompt;
281+ role = styles.roles.prompt;
282282 bytes = self.writing_bytes;
283283 },
284284 .reasoning => {
285285 text = "reasoning";
286- color = styles.roles.prompt;
286+ role = styles.roles.prompt;
287287 bytes = self.reasoning_bytes;
288288 },
289289 .text => {
290290 text = "writing response";
291- color = styles.roles.prompt;
291+ role = styles.roles.prompt;
292292 bytes = self.text_bytes;
293293 },
294294 .none => text = "streaming",
@@ -296,7 +296,7 @@ const Tui = struct {
296296 else => {},
297297 }
298298 var out: std.ArrayList(u8) = .empty;
299- out.appendSlice(a, if (color) |c| styles.styledWith(a, .{ .fg = c }, text) else styles.comment(a, text)) catch {};
299+ out.appendSlice(a, if (role) |r| styles.styledWith(a, r, text) else styles.comment(a, text)) catch {};
300300 if (bytes > 0) {
301301 const n = usage_mod.formatTokens(a, (bytes + 3) / 4);
302302 out.appendSlice(a, styles.comment(a, std.fmt.allocPrint(a, " · ~{s} tok", .{n}) catch "")) catch {};
@@ -341,7 +341,7 @@ const Tui = struct {
341341 if (self.picker) |*p| {
342342 const prefix = std.fmt.allocPrint(self.s, "{s} › ", .{p.title}) catch "";
343343 lines.append(self.s, std.fmt.allocPrint(self.s, "{s}{s}", .{
344- styles.styledWith(self.s, .{ .fg = styles.roles.prompt, .bold = true }, prefix),
344+ styles.styledWith(self.s, styles.over(.{ .bold = true }, styles.roles.prompt), prefix),
345345 render.sanitize(self.s, p.query.items),
346346 }) catch "> ") catch {};
347347 caret_line = 1;
@@ -358,7 +358,7 @@ const Tui = struct {
358358 render.sanitize(self.s, p.labels[idx]),
359359 }) catch p.labels[idx];
360360 lines.append(self.s, if (i == p.sel)
361- styles.styledWith(self.s, .{ .fg = styles.roles.prompt, .bold = true }, row)
361+ styles.styledWith(self.s, styles.over(.{ .bold = true }, styles.roles.prompt), row)
362362 else
363363 styles.comment(self.s, row)) catch {};
364364 }
@@ -1084,6 +1084,7 @@ pub fn run(opts: agent.Options, cfg: *const config.Config, tool_names: []const c
10841084 self.bindAllocators();
10851085 self.history = history.Store.init(gpa, config.historyPath(gpa), cfg.history_size);
10861086 status.init(cfg);
1087+ styles.applyScheme(cfg.colorscheme);
10871088
10881089 self.term.start();
10891090 defer self.term.stop();