diff options
| -rw-r--r-- | tools/gen_themes.zig | 67 |
1 files changed, 16 insertions, 51 deletions
diff --git a/tools/gen_themes.zig b/tools/gen_themes.zig index 30808dfe..3293349d 100644 --- a/tools/gen_themes.zig +++ b/tools/gen_themes.zig @@ -82,54 +82,33 @@ pub fn main(init: std.process.Init) !void { const args = try init.minimal.args.toSlice(arena); if (args.len < 3) std.process.fatal("usage: gen-themes <out-dir> <theme-source>...", .{}); - const sources = args[2..]; - const texts = try arena.alloc([]const u8, sources.len); - var helix_count: usize = 0; - var theme_count: usize = 0; - for (sources, texts) |src, *text| { - text.* = try std.Io.Dir.cwd().readFileAlloc(io, src, arena, .limited(8 << 20)); - if (std.mem.endsWith(u8, src, ".toml")) { - helix_count += 1; - theme_count += 1; - } else if (std.mem.endsWith(u8, src, ".json")) { - theme_count += try zedThemeCount(gpa, src, text.*); - } else { - std.process.fatal("{s}: not a helix .toml or a zed .json", .{src}); - } - } - - const themes = try arena.alloc(Theme, theme_count); - const stems = try arena.alloc([]const u8, helix_count); + var themes: std.ArrayList(Theme) = .empty; // TWO passes over the helix half, because `inherits = "other"` names a // SIBLING SOURCE by stem: nothing can be mapped until every file is read, // and the parent may be later in argv (or inherit something itself). var tomls: std.StringHashMapUnmanaged(Toml) = .empty; - var theme_index: usize = 0; - var stem_index: usize = 0; - for (sources, texts) |src, text| { + var stems: std.ArrayList([]const u8) = .empty; + for (args[2..]) |src| { + const text = try std.Io.Dir.cwd().readFileAlloc(io, src, arena, .limited(8 << 20)); if (std.mem.endsWith(u8, src, ".toml")) { const stem = std.fs.path.stem(src); try tomls.put(arena, stem, parseToml(arena, src, text)); - stems[stem_index] = stem; - stem_index += 1; + try stems.append(arena, stem); + } else if (std.mem.endsWith(u8, src, ".json")) { + try readZed(arena, gpa, src, text, &themes); } else { - theme_index += try readZed(arena, gpa, src, text, themes[theme_index..]); + std.process.fatal("{s}: not a helix .toml or a zed .json", .{src}); } } - std.debug.assert(stem_index == stems.len); - for (stems) |stem| { - themes[theme_index] = try helixTheme(arena, stem, resolve(arena, &tomls, stem, 8)); - theme_index += 1; - } - std.debug.assert(theme_index == themes.len); + for (stems.items) |stem| try themes.append(arena, try helixTheme(arena, stem, resolve(arena, &tomls, stem, 8))); // sorted, so the ring's tail is the same on every machine whatever order // the directory happened to be read in - std.mem.sort(Theme, themes, {}, byName); + std.mem.sort(Theme, themes.items, {}, byName); var dir = try std.Io.Dir.cwd().openDir(io, args[1], .{}); defer dir.close(io); var buf: std.Io.Writer.Allocating = .init(arena); - for (themes) |t| { + for (themes.items) |t| { buf.clearRetainingCapacity(); try emit(&buf.writer, t); try write(io, dir, arena, t.name, buf.written()); @@ -139,7 +118,7 @@ pub fn main(init: std.process.Init) !void { // from whoever can. colors.zig folds its declarations into the ring. buf.clearRetainingCapacity(); try buf.writer.writeAll("// Generated by tools/gen_themes.zig. Do not edit.\n"); - for (themes) |t| try buf.writer.print("pub const {s} = @import(\"{s}.zig\");\n", .{ t.name, t.name }); + for (themes.items) |t| try buf.writer.print("pub const {s} = @import(\"{s}.zig\");\n", .{ t.name, t.name }); try write(io, dir, arena, "list", buf.written()); } @@ -544,19 +523,7 @@ fn table(s: []const u8) Pair { // take them: a light theme is only usable if the shell's own output is // repainted for it, and pardes's acme-light is the hand-written proof. -fn zedThemeCount(gpa: std.mem.Allocator, path: []const u8, text: []const u8) !usize { - const parsed = std.json.parseFromSlice(std.json.Value, gpa, text, .{}) catch - std.process.fatal("{s}: not valid JSON", .{path}); - defer parsed.deinit(); - const root = switch (parsed.value) { - .object => |o| o, - else => std.process.fatal("{s}: top level is not an object", .{path}), - }; - const list = root.get("themes") orelse std.process.fatal("{s}: no `themes` array", .{path}); - return list.array.items.len; -} - -fn readZed(arena: std.mem.Allocator, gpa: std.mem.Allocator, path: []const u8, text: []const u8, out: []Theme) !usize { +fn readZed(arena: std.mem.Allocator, gpa: std.mem.Allocator, path: []const u8, text: []const u8, out: *std.ArrayList(Theme)) !void { const parsed = std.json.parseFromSlice(std.json.Value, gpa, text, .{}) catch std.process.fatal("{s}: not valid JSON", .{path}); defer parsed.deinit(); @@ -565,8 +532,7 @@ fn readZed(arena: std.mem.Allocator, gpa: std.mem.Allocator, path: []const u8, t else => std.process.fatal("{s}: top level is not an object", .{path}), }; const list = root.get("themes") orelse std.process.fatal("{s}: no `themes` array", .{path}); - std.debug.assert(list.array.items.len <= out.len); - for (list.array.items, 0..) |variant, index| { + for (list.array.items) |variant| { const v = variant.object; const z: Zed = .{ .path = path, @@ -578,7 +544,7 @@ fn readZed(arena: std.mem.Allocator, gpa: std.mem.Allocator, path: []const u8, t pal[i] = z.need(try std.fmt.allocPrint(arena, "terminal.ansi.{s}", .{n})); pal[i + 8] = z.need(try std.fmt.allocPrint(arena, "terminal.ansi.bright_{s}", .{n})); } - out[index] = .{ + try out.append(arena, .{ // `_zed` on every one of them: see the naming note at the top — // zed re-ships gruvbox, ayu and one, so the source is part of the // name rather than a suffix that appears only on the eight that @@ -610,9 +576,8 @@ fn readZed(arena: std.mem.Allocator, gpa: std.mem.Allocator, path: []const u8, t .scroll_track = z.need("scrollbar.thumb.border"), .scroll_thumb = z.need("scrollbar.thumb.background"), .palette = pal, - }; + }); } - return list.array.items.len; } /// zed's order, which is also the ANSI one: 0-7 plain, 8-15 the `bright_` half |
