summaryrefslogtreecommitdiff
path: root/src/colors.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/colors.zig')
-rw-r--r--src/colors.zig70
1 files changed, 39 insertions, 31 deletions
diff --git a/src/colors.zig b/src/colors.zig
index 379d1301..dd0d8938 100644
--- a/src/colors.zig
+++ b/src/colors.zig
@@ -205,7 +205,7 @@ const name_tint_chroma: f32 = 0.06;
/// ink where that hue holds, the name reads on its ground (nameTintFloor)
/// and it stands off the ink. Null when no colour of the theme will do.
fn derivedNameTint(th: *const Theme, ink: [3]u8, ground: [3]u8) ?[3]u8 {
- const pal = th.palette orelse [_][3]u8{th.kw} ** 16;
+ const pal = th.palette orelse @as([16][3]u8, @splat(th.kw));
const sources = [_][3]u8{ th.kw, pal[4], pal[5], pal[6], pal[3], pal[2], pal[1], th.num, th.str, th.box } ++
[_][3]u8{ th.column_box orelse th.num, th.box_dirty orelse th.num, th.sel_bg, th.search_bg orelse th.num } ++
[_][3]u8{ th.diagnostic_info orelse th.num, th.diagnostic_hint orelse th.num, th.diagnostic_warning orelse th.num } ++
@@ -468,28 +468,29 @@ const curated = struct {
pub const dark = @import("themes/dark.zig");
};
-fn fold(comptime C: type) [@typeInfo(C).@"struct".decls.len]Theme {
+fn fold(comptime C: type) [@typeInfo(C).@"struct".decl_names.len]Theme {
comptime {
@setEvalBranchQuota(400000);
- var out: [@typeInfo(C).@"struct".decls.len]Theme = undefined;
- for (@typeInfo(C).@"struct".decls, 0..) |d, i| {
+ var out: [@typeInfo(C).@"struct".decl_names.len]Theme = undefined;
+ const theme_info = @typeInfo(Theme).@"struct";
+ for (@typeInfo(C).@"struct".decl_names, 0..) |d, i| {
var t: Theme = undefined;
- const source = @field(C, d.name).theme;
- for (@typeInfo(Theme).@"struct".fields) |f| {
- @field(t, f.name) = if (f.type == ?Decor and @hasField(@TypeOf(source), f.name)) decor: {
+ const source = @field(C, d).theme;
+ for (theme_info.field_names, theme_info.field_types, theme_info.field_attrs) |name, T, attrs| {
+ @field(t, name) = if (T == ?Decor and @hasField(@TypeOf(source), name)) decor: {
// A nested literal is folded field by field too.
var decor: Decor = .{};
- const given = @field(source, f.name);
- for (@typeInfo(Decor).@"struct".fields) |g| if (@hasField(@TypeOf(given), g.name)) {
- @field(decor, g.name) = @field(given, g.name);
+ const given = @field(source, name);
+ for (@typeInfo(Decor).@"struct".field_names) |g| if (@hasField(@TypeOf(given), g)) {
+ @field(decor, g) = @field(given, g);
};
break :decor decor;
- } else if (@hasField(@TypeOf(source), f.name))
- @field(source, f.name)
- else if (f.default_value_ptr) |ptr|
- @as(*const f.type, @ptrCast(@alignCast(ptr))).*
+ } else if (@hasField(@TypeOf(source), name))
+ @field(source, name)
+ else if (attrs.defaultValue(T)) |value|
+ value
else
- @compileError("theme " ++ d.name ++ " is missing " ++ f.name);
+ @compileError("theme " ++ d ++ " is missing " ++ name);
}
out[i] = t;
}
@@ -501,7 +502,7 @@ fn fold(comptime C: type) [@typeInfo(C).@"struct".decls.len]Theme {
/// takes: a faithful port (`dracula`) keeps the plain name, and helix's
/// looser copy of it is still there as `dracula_helix` (the zed ones already
/// carry `_zed`).
-fn foldImports(comptime ours: anytype, comptime C: type) [@typeInfo(C).@"struct".decls.len]Theme {
+fn foldImports(comptime ours: anytype, comptime C: type) [@typeInfo(C).@"struct".decl_names.len]Theme {
comptime {
@setEvalBranchQuota(400000);
var out = fold(C);
@@ -514,7 +515,7 @@ fn foldImports(comptime ours: anytype, comptime C: type) [@typeInfo(C).@"struct"
pub const themes = fold(curated) ++ foldImports(fold(curated), @import("generated_themes"));
/// The first `native_count` of `themes` are pardes's own; the rest imported.
-pub const native_count = @typeInfo(curated).@"struct".decls.len;
+pub const native_count = @typeInfo(curated).@"struct".decl_names.len;
/// The picker's sections (`Themes`), in ring order, each running from
/// the theme it names to the next section's; `NextColor` and `Theme` still
@@ -677,8 +678,8 @@ pub const ChromeTheme = struct {
pub fn interpolate(from: ChromeTheme, to: ChromeTheme, step: u16, steps: u16) ChromeTheme {
var out: ChromeTheme = undefined;
const t = @as(f32, @floatFromInt(@min(step, steps))) / @as(f32, @floatFromInt(@max(1, steps)));
- inline for (@typeInfo(ChromeTheme).@"struct".fields) |field|
- @field(out, field.name) = if (step == 0) @field(from, field.name) else if (step >= steps) @field(to, field.name) else mixOklab(@field(from, field.name), @field(to, field.name), t);
+ inline for (@typeInfo(ChromeTheme).@"struct".field_names) |name|
+ @field(out, name) = if (step == 0) @field(from, name) else if (step >= steps) @field(to, name) else mixOklab(@field(from, name), @field(to, name), t);
return out;
}
};
@@ -801,14 +802,16 @@ pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8, animate: bo
themeFileError(p, request, error.ThemeFileTooLarge);
return false;
}
- const source = p.gpa.dupeZ(u8, bytes) catch |err| {
+ const source = p.gpa.dupeSentinel(u8, bytes, 0) catch |err| {
themeFileError(p, request, err);
return false;
};
defer p.gpa.free(source);
- var diagnostics: std.zon.parse.Diagnostics = .{};
- defer diagnostics.deinit(p.gpa);
- const parsed = std.zon.parse.fromSliceAlloc(Theme, p.gpa, source, &diagnostics, .{}) catch |err| {
+ // The parse's results live in `arena`; the theme kept owns only its name.
+ var arena: std.heap.ArenaAllocator = .init(p.gpa);
+ defer arena.deinit();
+ var diagnostics: std.zon.parse.Diagnostics = undefined;
+ var parsed = std.zon.parse.fromSlice(Theme, .{ .gpa = p.gpa, .arena = arena.allocator(), .source = source, .diagnostics = &diagnostics }) catch |err| {
if (err != error.ParseZon) themeFileError(p, request, err) else {
// Which line, and what the parser wanted there.
var why: [200]u8 = undefined;
@@ -817,10 +820,13 @@ pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8, animate: bo
return false;
};
if (parsed.name.len == 0 or parsed.name.len > 255) {
- std.zon.parse.free(p.gpa, parsed);
themeFileError(p, request, error.InvalidThemeName);
return false;
}
+ parsed.name = p.gpa.dupe(u8, parsed.name) catch |err| {
+ themeFileError(p, request, err);
+ return false;
+ };
const target_chrome = ChromeTheme.fromTheme(&parsed);
if (animate and p.animate_theme_changes)
@@ -829,7 +835,7 @@ pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8, animate: bo
p.chrome_animation.snap(target_chrome);
const old = p.custom_theme;
p.custom_theme = parsed;
- if (old) |theme_value| std.zon.parse.free(p.gpa, theme_value);
+ if (old) |theme_value| p.gpa.free(theme_value.name);
var settled: [limits.host_path_cap]u8 = undefined;
@memcpy(settled[0..request.path.len], request.path);
_ = p.theme_file_loaded.set(settled[0..request.path.len]);
@@ -847,7 +853,7 @@ pub fn carryTheme(p: *Pardes, old: *const Pardes, index: bool) !void {
const t = old.custom_theme orelse return;
var copy = t;
copy.name = try p.gpa.dupe(u8, t.name);
- if (p.custom_theme) |mine| std.zon.parse.free(p.gpa, mine);
+ if (p.custom_theme) |mine| p.gpa.free(mine.name);
p.custom_theme = copy;
_ = p.theme_file_loaded.set(old.theme_file_loaded.get());
}
@@ -931,7 +937,7 @@ fn likeness(theme_name: []const u8, name: []const u8) usize {
in_order += 1;
pos = at + 1;
}
- const inside: usize = if (name.len > 0 and std.ascii.indexOfIgnoreCase(theme_name, name) != null) name.len * 4 else 0;
+ const inside: usize = if (name.len > 0 and std.ascii.findIgnoreCase(theme_name, name) != null) name.len * 4 else 0;
return prefix * 3 + in_order + inside;
}
@@ -952,7 +958,7 @@ pub fn setThemeIndex(p: *Pardes, index: usize) void {
p.chrome_animation.snap(target_chrome);
if (p.custom_theme) |theme_value| {
p.custom_theme = null;
- std.zon.parse.free(p.gpa, theme_value);
+ p.gpa.free(theme_value.name);
}
p.theme_file_loaded.clear();
if (p.theme_file_path.get().len > 0) {
@@ -1264,10 +1270,12 @@ test "legacy ThemeFile documents inherit Pardes UI roles without new fields" {
rest = rest[at + std.mem.indexOfScalar(u8, rest[at..], '}').? + 1 ..];
rest = std.mem.trimStart(u8, rest, " ,");
}
- const source = try std.testing.allocator.dupeZ(u8, legacy.items);
+ const source = try std.testing.allocator.dupeSentinel(u8, legacy.items, 0);
defer std.testing.allocator.free(source);
- const parsed = try std.zon.parse.fromSliceAlloc(Theme, std.testing.allocator, source, null, .{});
- defer std.zon.parse.free(std.testing.allocator, parsed);
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ var diagnostics: std.zon.parse.Diagnostics = undefined;
+ const parsed = try std.zon.parse.fromSlice(Theme, .{ .gpa = std.testing.allocator, .arena = arena.allocator(), .source = source, .diagnostics = &diagnostics });
try std.testing.expectEqual(@as(?[3]u8, null), parsed.tag_active_bg);
try std.testing.expectEqual(@as(?[3]u8, null), parsed.tag_name_fg);
try std.testing.expectEqual(@as(?[3]u8, null), parsed.tag_active_name_fg);