@@ -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 }