authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2020-10-13 20:17:30+02:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2020-10-13 20:17:30+02:00
log6ba1fdf7e0d8dfc348a163219b3c3215c3644dd6
treea8e5d328bc8f1dc3f47f8d84fc16ce1582ed673c
parentf01c3150c19fa3d6c605cc1940f70cce303c5997
signaturelock-open Commit is signed but in an unrecognized format.

stage2: use meta.stringToEnum for Color parsing

This requires renaming the variants to be snake_case, which is the new recommended style anyways.

2 files changed, 15 insertions(+), 27 deletions(-)

src/Compilation.zig+3-3
......@@ -103,7 +103,7 @@ owned_link_dir: ?std.fs.Dir,
103103
104104/// This is for stage1 and should be deleted upon completion of self-hosting.
105105/// Don't use this for anything other than stage1 compatibility.
106color: @import("main.zig").Color = .Auto,
106color: @import("main.zig").Color = .auto,
107107
108108test_filter: ?[]const u8,
109109test_name_prefix: ?[]const u8,
......@@ -385,7 +385,7 @@ pub const InitOptions = struct {
385385 machine_code_model: std.builtin.CodeModel = .default,
386386 clang_preprocessor_mode: ClangPreprocessorMode = .no,
387387 /// This is for stage1 and should be deleted upon completion of self-hosting.
388 color: @import("main.zig").Color = .Auto,
388 color: @import("main.zig").Color = .auto,
389389 test_filter: ?[]const u8 = null,
390390 test_name_prefix: ?[]const u8 = null,
391391 subsystem: ?std.Target.SubSystem = null,
......@@ -1179,7 +1179,7 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
11791179 var progress: std.Progress = .{};
11801180 var main_progress_node = try progress.start("", null);
11811181 defer main_progress_node.end();
1182 if (self.color == .Off) progress.terminal = null;
1182 if (self.color == .off) progress.terminal = null;
11831183
11841184 var c_comp_progress_node = main_progress_node.start("Compile C Objects", self.c_source_files.len);
11851185 defer c_comp_progress_node.end();
src/main.zig+12-24
......@@ -28,9 +28,9 @@ pub fn fatal(comptime format: []const u8, args: anytype) noreturn {
2828pub const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
2929
3030pub const Color = enum {
31 Auto,
32 Off,
33 On,
31 auto,
32 off,
33 on,
3434};
3535
3636const usage =
......@@ -380,7 +380,7 @@ fn buildOutputType(
380380 run,
381381 },
382382) !void {
383 var color: Color = .Auto;
383 var color: Color = .auto;
384384 var optimize_mode: std.builtin.Mode = .Debug;
385385 var provided_name: ?[]const u8 = null;
386386 var link_mode: ?std.builtin.LinkMode = null;
......@@ -585,15 +585,9 @@ fn buildOutputType(
585585 }
586586 i += 1;
587587 const next_arg = args[i];
588 if (mem.eql(u8, next_arg, "auto")) {
589 color = .Auto;
590 } else if (mem.eql(u8, next_arg, "on")) {
591 color = .On;
592 } else if (mem.eql(u8, next_arg, "off")) {
593 color = .Off;
594 } else {
588 color = std.meta.stringToEnum(Color, next_arg) orelse {
595589 fatal("expected [auto|on|off] after --color, found '{}'", .{next_arg});
596 }
590 };
597591 } else if (mem.eql(u8, arg, "--subsystem")) {
598592 if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg});
599593 i += 1;
......@@ -2374,7 +2368,7 @@ const Fmt = struct {
23742368
23752369pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
23762370 const stderr_file = io.getStdErr();
2377 var color: Color = .Auto;
2371 var color: Color = .auto;
23782372 var stdin_flag: bool = false;
23792373 var check_flag: bool = false;
23802374 var input_files = ArrayList([]const u8).init(gpa);
......@@ -2394,15 +2388,9 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
23942388 }
23952389 i += 1;
23962390 const next_arg = args[i];
2397 if (mem.eql(u8, next_arg, "auto")) {
2398 color = .Auto;
2399 } else if (mem.eql(u8, next_arg, "on")) {
2400 color = .On;
2401 } else if (mem.eql(u8, next_arg, "off")) {
2402 color = .Off;
2403 } else {
2391 color = std.meta.stringToEnum(Color, next_arg) orelse {
24042392 fatal("expected [auto|on|off] after --color, found '{}'", .{next_arg});
2405 }
2393 };
24062394 } else if (mem.eql(u8, arg, "--stdin")) {
24072395 stdin_flag = true;
24082396 } else if (mem.eql(u8, arg, "--check")) {
......@@ -2626,9 +2614,9 @@ fn printErrMsgToFile(
26262614 color: Color,
26272615) !void {
26282616 const color_on = switch (color) {
2629 .Auto => file.isTty(),
2630 .On => true,
2631 .Off => false,
2617 .auto => file.isTty(),
2618 .on => true,
2619 .off => false,
26322620 };
26332621 const lok_token = parse_error.loc();
26342622 const span_first = lok_token;