79113
Ma
mini-coding-agent / files
mini-coding-agent / src / tui / styles.zig
4.5 KB
1const std = @import("std"); 2const config = @import("../config.zig"); 3 4pub const Palette = enum(u8) { 5 black = 0, 6 red, 7 green, 8 yellow, 9 blue, 10 magenta, 11 cyan, 12 white, 13 bright_black, 14 bright_red, 15 bright_green, 16 bright_yellow, 17 bright_blue, 18 bright_magenta, 19 bright_cyan, 20 bright_white, 21}; 22 23pub const Color = union(enum) { 24 palette: Palette, 25 rgb: u24, 26}; 27 28pub const Style = struct { 29 fg: ?Color = null, 30 bold: bool = false, 31 italic: bool = false, 32 underline: bool = false, 33 dim: bool = false, 34}; 35 36pub const Roles = struct { 37 comment: Style = .{ .fg = .{ .palette = .bright_black } }, 38 prompt: Style = .{ .fg = .{ .palette = .blue } }, 39 accent: Style = .{ .fg = .{ .palette = .cyan } }, 40 err: Style = .{ .fg = .{ .palette = .red } }, 41 warn: Style = .{ .fg = .{ .palette = .yellow } }, 42 code: Style = .{ .fg = .{ .palette = .bright_black } }, 43 output: Style = .{}, 44 diff_add: Style = .{ .fg = .{ .palette = .green } }, 45 diff_delete: Style = .{ .fg = .{ .palette = .red } }, 46 inline_code: Style = .{ .fg = .{ .palette = .magenta } }, 47 link: Style = .{ .fg = .{ .palette = .blue }, .underline = true }, 48 uri: Style = .{ .underline = true }, 49 heading1: Style = .{ .fg = .{ .palette = .bright_blue }, .bold = true }, 50 heading2: Style = .{ .fg = .{ .palette = .bright_cyan }, .bold = true }, 51 heading3: Style = .{ .fg = .{ .palette = .bright_magenta }, .bold = true }, 52 heading4: Style = .{ .fg = .{ .palette = .bright_green }, .bold = true }, 53 heading5: Style = .{ .fg = .{ .palette = .bright_yellow }, .bold = true }, 54 heading6: Style = .{ .fg = .{ .palette = .bright_red }, .bold = true }, 55 56 pub fn heading(self: Roles, level: usize) Style { 57 return switch (level) { 58 1 => self.heading1, 59 2 => self.heading2, 60 3 => self.heading3, 61 4 => self.heading4, 62 5 => self.heading5, 63 else => self.heading6, 64 }; 65 } 66}; 67 68pub var roles: Roles = .{}; 69 70pub fn applyScheme(scheme: config.Colorscheme) void { 71 inline for (@typeInfo(config.Colorscheme).@"struct".field_names) |name| { 72 if (@field(scheme, name)) |hex| @field(roles, name).fg = .{ .rgb = config.parseHex(hex).? }; 73 } 74} 75 76pub const SGR_PLAIN = "\x1b[0m"; 77 78fn appendFg(a: std.mem.Allocator, out: *std.ArrayList(u8), color: Color) void { 79 const text = switch (color) { 80 .palette => |p| blk: { 81 const n: u8 = @backingInt(p); 82 break :blk std.fmt.allocPrint(a, ";{d}", .{if (n < 8) 30 + n else 90 + n - 8}); 83 }, 84 .rgb => |v| std.fmt.allocPrint(a, ";38;2;{d};{d};{d}", .{ v >> 16 & 0xff, v >> 8 & 0xff, v & 0xff }), 85 } catch return; 86 out.appendSlice(a, text) catch {}; 87} 88 89pub fn over(style: Style, base: Style) Style { 90 return .{ 91 .fg = style.fg orelse base.fg, 92 .bold = style.bold or base.bold, 93 .italic = style.italic or base.italic, 94 .underline = style.underline or base.underline, 95 .dim = style.dim or base.dim, 96 }; 97} 98 99fn sgr(a: std.mem.Allocator, style: Style) []const u8 { 100 var out: std.ArrayList(u8) = .empty; 101 out.appendSlice(a, "\x1b[0") catch {}; 102 if (style.bold) out.appendSlice(a, ";1") catch {}; 103 if (style.italic) out.appendSlice(a, ";3") catch {}; 104 if (style.underline) out.appendSlice(a, ";4") catch {}; 105 if (style.dim) out.appendSlice(a, ";2") catch {}; 106 if (style.fg) |c| appendFg(a, &out, c); 107 out.append(a, 'm') catch {}; 108 return out.items; 109} 110 111pub fn sgrOn(a: std.mem.Allocator, style: Style, base: Style) []const u8 { 112 return sgr(a, over(style, base)); 113} 114 115pub fn styledWith(a: std.mem.Allocator, style: Style, text: []const u8) []const u8 { 116 return std.fmt.allocPrint(a, "{s}{s}{s}", .{ sgr(a, style), text, SGR_PLAIN }) catch text; 117} 118 119pub fn anySet(style: Style) bool { 120 return style.fg != null or style.bold or style.italic or style.underline or style.dim; 121} 122 123pub fn sep(a: std.mem.Allocator) []const u8 { 124 return styledWith(a, roles.comment, " · "); 125} 126 127pub fn join(a: std.mem.Allocator, parts: []const []const u8) []const u8 { 128 return std.mem.join(a, sep(a), parts) catch ""; 129} 130 131pub fn comment(a: std.mem.Allocator, text: []const u8) []const u8 { 132 return styledWith(a, roles.comment, text); 133} 134 135pub fn err(a: std.mem.Allocator, text: []const u8) []const u8 { 136 return styledWith(a, roles.err, text); 137} 138 139pub fn warn(a: std.mem.Allocator, text: []const u8) []const u8 { 140 return styledWith(a, roles.warn, text); 141} 142 143pub fn accent(a: std.mem.Allocator, text: []const u8) []const u8 { 144 return styledWith(a, roles.accent, text); 145}