Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zig 0.13.0 support #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ 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
steps:
- 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
23 changes: 10 additions & 13 deletions build.zig
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
16 changes: 8 additions & 8 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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));
Expand Down
10 changes: 5 additions & 5 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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).?);
}
30 changes: 13 additions & 17 deletions src/templator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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") },
Expand All @@ -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" },
Expand All @@ -45,15 +45,15 @@ 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" },
.{ @tagName(cli.Gen.iterm2), ".itermcolors" },
.{ @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), '}' },
Expand All @@ -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;
}
Expand All @@ -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 },
Expand Down Expand Up @@ -172,22 +168,22 @@ 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;
}

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
Expand Down
18 changes: 9 additions & 9 deletions src/utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -44,18 +44,18 @@ 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);
try std.testing.expect(hex_dec[2] == 170);
}

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| {
Expand Down