From ed13d58b625b07cfa96660ce92aac54beb77b58f Mon Sep 17 00:00:00 2001 From: Kirill Radzikhovskyy Date: Sun, 22 Dec 2024 15:10:57 +0900 Subject: [PATCH 1/2] zig 0.13.0 support --- build.zig | 23 ++++++++++------------- src/cli.zig | 16 ++++++++-------- src/main.zig | 10 +++++----- src/templator.zig | 30 +++++++++++++----------------- src/utils.zig | 18 +++++++++--------- 5 files changed, 45 insertions(+), 52 deletions(-) diff --git a/build.zig b/build.zig index 9c18762..695153e 100644 --- a/build.zig +++ b/build.zig @@ -1,22 +1,19 @@ const std = @import("std"); -pub fn build(b: *std.build.Builder) void { - // Standard target options allows the person running `zig build` to choose - // what target to build for. Here we do not override the defaults, which - // means any target is allowed, and the default is native. Other options - // for restricting supported target set are available. +pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); - // Standard release options allow the person running `zig build` to select - // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. - const mode = b.standardReleaseOptions(); + const exe = b.addExecutable(.{ + .name = "colorstorm", + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + b.installArtifact(exe); - const exe = b.addExecutable("colorstorm", "src/main.zig"); - exe.setTarget(target); - exe.setBuildMode(mode); - exe.install(); + const run_cmd = b.addRunArtifact(exe); - const run_cmd = exe.run(); run_cmd.step.dependOn(b.getInstallStep()); if (b.args) |args| { run_cmd.addArgs(args); diff --git a/src/cli.zig b/src/cli.zig index 33c92ac..a17b266 100644 --- a/src/cli.zig +++ b/src/cli.zig @@ -28,7 +28,7 @@ pub fn parse_flag(argument: []const u8) Flag { if (std.mem.eql(u8, argument, flag_prefix_long ++ f.name) or std.mem.eql(u8, argument, flag_prefix_short ++ [_]u8{f.name[0]})) { - return @intToEnum(Flag, f.value); + return @enumFromInt(f.value); } } @@ -50,23 +50,23 @@ pub fn get_flag_val(comptime flag: Flag) ?[]const u8 { } test "using long and short versions of flag" { - var input_long: []const u8 = "--input"; - var input_short: []const u8 = "-i"; - var input_flag_long = parse_flag(input_long); - var input_flag_short = parse_flag(input_short); + const input_long: []const u8 = "--input"; + const input_short: []const u8 = "-i"; + const input_flag_long = parse_flag(input_long); + const input_flag_short = parse_flag(input_short); try std.testing.expect(input_flag_long == input_flag_short); } test "check fallback flag value for invalid arguments" { - var bad_input: []const u8 = "--invalid"; - var bad_flag = parse_flag(bad_input); + const bad_input: []const u8 = "--invalid"; + const bad_flag = parse_flag(bad_input); try std.testing.expect(bad_flag == Flag.na); } test "set and get flag value" { - var gen_flag: []const u8 = "vim"; + const gen_flag: []const u8 = "vim"; try set_flag_val(Flag.gen, gen_flag); try std.testing.expect(std.mem.eql(u8, get_flag_val(Flag.gen).?, gen_flag)); diff --git a/src/main.zig b/src/main.zig index a9dd2c2..73ed93c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -39,7 +39,7 @@ fn parse_args() !void { flag = cli.parse_flag(argument); if (flag == cli.Flag.help) { try stdout.print("\n{s}\n\n", .{help}); - std.os.exit(0); + std.process.exit(0); } } @@ -58,18 +58,18 @@ pub fn main() !void { try cli.init(); try parse_args(); - var input = cli.get_flag_val(cli.Flag.input).?; + const input = cli.get_flag_val(cli.Flag.input).?; if (input.len == 0) { try stdout.print("ERROR: Missing input file\n{s}\n\n", .{help}); - std.os.exit(1); + std.process.exit(1); } const f = std.fs.cwd().openFile(input, std.fs.File.OpenFlags{}) catch { try stdout.print("ERROR: Unable to open file '{s}'\n", .{input}); - std.os.exit(1); + std.process.exit(1); }; - var outdir = cli.get_flag_val(cli.Flag.outdir).?; + const outdir = cli.get_flag_val(cli.Flag.outdir).?; try std.fs.cwd().makePath(cli.get_flag_val(cli.Flag.outdir).?); try templator.create_themes(f, outdir, cli.get_flag_val(cli.Flag.gen).?); } diff --git a/src/templator.zig b/src/templator.zig index 420079c..5630a04 100644 --- a/src/templator.zig +++ b/src/templator.zig @@ -26,7 +26,7 @@ var tmp_gen = std.BufMap.init(a); // Each embedded template file allows colorstorm to loop through and replace template // variables with color values to produce a full template. -pub const templates = std.ComptimeStringMap([]const u8, .{ +pub const templates = std.StaticStringMap([]const u8).initComptime(.{ .{ @tagName(cli.Gen.vim), @embedFile("templates/template.vim") }, .{ @tagName(cli.Gen.atom), @embedFile("templates/colors.less") }, .{ @tagName(cli.Gen.vscode), @embedFile("templates/template.json") }, @@ -35,7 +35,7 @@ pub const templates = std.ComptimeStringMap([]const u8, .{ }); // The base output path to use for each theme -const out_path = std.ComptimeStringMap([]const u8, .{ +const out_path = std.StaticStringMap([]const u8).initComptime(.{ .{ @tagName(cli.Gen.vim), "vim/colors" }, .{ @tagName(cli.Gen.atom), "atom/themes" }, .{ @tagName(cli.Gen.vscode), "vscode/themes" }, @@ -45,7 +45,7 @@ const out_path = std.ComptimeStringMap([]const u8, .{ // The output "extension" to use. In the case of atom, the "extension" is just colors.less, // since that is what the editor expects when loading a new theme. -const out_ext = std.ComptimeStringMap([]const u8, .{ +const out_ext = std.StaticStringMap([]const u8).initComptime(.{ .{ @tagName(cli.Gen.vim), ".vim" }, .{ @tagName(cli.Gen.atom), "colors.less" }, .{ @tagName(cli.Gen.vscode), ".json" }, @@ -53,7 +53,7 @@ const out_ext = std.ComptimeStringMap([]const u8, .{ .{ @tagName(cli.Gen.sublime), ".tmTheme" }, }); -const template_end = std.ComptimeStringMap(u8, .{ +const template_end = std.StaticStringMap(u8).initComptime(.{ .{ @tagName(cli.Gen.vim), '"' }, .{ @tagName(cli.Gen.atom), ';' }, .{ @tagName(cli.Gen.vscode), '}' }, @@ -76,13 +76,9 @@ pub fn parse_themes(f: std.fs.File) ![]Theme { try writer.writeAll(line); } - var stream = std.json.TokenStream.init(list.items); - - const themes = try std.json.parse( - []Theme, - &stream, - .{ .allocator = a }, - ); + const parsed = try std.json.parseFromSlice([]Theme, a, list.items, .{}); + defer parsed.deinit(); + const themes = parsed.value; return themes; } @@ -95,11 +91,11 @@ fn replace(name: []const u8, val: []const u8, gen_type: []const u8) !void { // This is done first since RGB percentage variable names are the same // as regular variable names, just prefixed with "R|G|B". if (std.mem.startsWith(u8, val, "#")) { - var c_percents = utils.hex_to_percent(val); + const c_percents = utils.hex_to_percent(val); var iter = std.mem.split(u8, c_percents, " "); for ("RGB") |c| { - var c_val = iter.next().?; - var c_var = try std.fmt.allocPrint( + const c_val = iter.next().?; + const c_var = try std.fmt.allocPrint( a, "{u}{s}", .{ c, name }, @@ -172,7 +168,7 @@ fn generate(gen_type: []const u8, themes: []Theme, outdir: []const u8) !void { // Ensure full output path exists before writing finished theme try std.fs.cwd().makePath(fmt_out_path); - var theme_output = tmp_gen.get(gen_type).?; + const theme_output = tmp_gen.get(gen_type).?; var theme_len = theme_output.len; while (theme_output[theme_len - 1] != template_end.get(gen_type).?) { theme_len -= 1; @@ -180,14 +176,14 @@ fn generate(gen_type: []const u8, themes: []Theme, outdir: []const u8) !void { try stdout.print(" {s}\n", .{fmt_out_file}); - try std.fs.cwd().writeFile(fmt_out_file, tmp_gen.get(gen_type).?[0..theme_len]); + try std.fs.cwd().writeFile(.{ .sub_path = fmt_out_file, .data = tmp_gen.get(gen_type).?[0..theme_len] }); } } /// Creates themes based on the provided input file. Can be used to create themes for a /// specific editor, or all supported editors/platforms if none is specified. pub fn create_themes(input: std.fs.File, outdir: []const u8, gen_type: []const u8) !void { - var themes: []Theme = try parse_themes(input); + const themes: []Theme = try parse_themes(input); if (std.mem.eql(u8, @tagName(cli.Gen.all), gen_type)) { // Loop through all supported platforms and create themes for each diff --git a/src/utils.zig b/src/utils.zig index a037e0a..724db6e 100644 --- a/src/utils.zig +++ b/src/utils.zig @@ -5,11 +5,11 @@ const allocator = std.heap.page_allocator; /// Returns a [3]u8 corresponding to RGB percentages /// derived from a hex color string (i.e. "#ffffff") pub fn hex_to_percent(hex_color: []const u8) []const u8 { - var result_i = hex_to_dec(hex_color); + const result_i = hex_to_dec(hex_color); var result_f = [3]f32{ 0.0, 0.0, 0.0 }; - for (result_i) |val, i| { - result_f[i] = @intToFloat(f32, val) / 255; + for (result_i, 0..) |val, i| { + result_f[i] = @as(f32, @floatFromInt(val)) / 255; } const result_str = std.fmt.allocPrint( @@ -26,7 +26,7 @@ pub fn hex_to_percent(hex_color: []const u8) []const u8 { pub fn hex_to_dec(hex_color: []const u8) [3]i32 { var result = [3]i32{ 0, 0, 0 }; - for (result) |_, index| { + for (result, 0..) |_, index| { // Split hex color into separate R/G/B components //std.debug.print("{d}\n", .{index}); const string = std.fmt.allocPrint( @@ -44,8 +44,8 @@ pub fn hex_to_dec(hex_color: []const u8) [3]i32 { } test "hex color to decimal" { - var hex: []const u8 = "#ff00aa"; - var hex_dec = hex_to_dec(hex); + const hex: []const u8 = "#ff00aa"; + const hex_dec = hex_to_dec(hex); try std.testing.expect(hex_dec[0] == 255); try std.testing.expect(hex_dec[1] == 0); @@ -53,9 +53,9 @@ test "hex color to decimal" { } test "hex color to percentage" { - var hex: []const u8 = "#ffffff"; - var hex_percent = hex_to_percent(hex); - var expected: []const u8 = "1.0000000"; + const hex: []const u8 = "#ffffff"; + const hex_percent = hex_to_percent(hex); + const expected: []const u8 = "1.0000000"; var iter = std.mem.split(u8, hex_percent, " "); while (iter.next()) |c_val| { From b1ed330305293e4e17dd9928aed9aa3eff4125f6 Mon Sep 17 00:00:00 2001 From: Kirill Radzikhovskyy Date: Mon, 23 Dec 2024 07:50:32 +0900 Subject: [PATCH 2/2] 0.13.0 in github --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 68a38b5..167eb60 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -17,7 +17,7 @@ jobs: - uses: actions/checkout@v2 - uses: goto-bus-stop/setup-zig@v1 with: - version: 0.10.1 + version: 0.13.0 - run: make test lint: runs-on: ubuntu-latest @@ -25,5 +25,5 @@ jobs: - uses: actions/checkout@v2 - uses: goto-bus-stop/setup-zig@v1 with: - version: 0.10.1 + version: 0.13.0 - run: zig fmt --check src/*.zig