authorgravatar for spexguy070@gmail.comMartin Wickham <spexguy070@gmail.com> 2021-06-21 13:47:38-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-08 14:30:45-04:00
loge2b954c2738c683a85b864eb33530f0e3dbbc480
tree47d90ba0a28ee7ce3e4e2f12b325567c31f307e2
parent7bdeda82aea82b9a71378462f51a708b8ad88161

Add support for NO_COLOR


7 files changed, 42 insertions(+), 20 deletions(-)

lib/std/debug.zig+4-4
...@@ -90,11 +90,11 @@ pub fn getSelfDebugInfo() !*DebugInfo {...@@ -90,11 +90,11 @@ pub fn getSelfDebugInfo() !*DebugInfo {
90}90}
9191
92pub fn detectTTYConfig() TTY.Config {92pub fn detectTTYConfig() TTY.Config {
93 var bytes: [128]u8 = undefined;93 if (process.hasEnvVarConstant("ZIG_DEBUG_COLOR")) {
94 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;
95 if (process.getEnvVarOwned(allocator, "ZIG_DEBUG_COLOR")) |_| {
96 return .escape_codes;94 return .escape_codes;
97 } else |_| {95 } else if (process.hasEnvVarConstant("NO_COLOR")) {
96 return .no_color;
97 } else {
98 const stderr_file = io.getStdErr();98 const stderr_file = io.getStdErr();
99 if (stderr_file.supportsAnsiEscapeCodes()) {99 if (stderr_file.supportsAnsiEscapeCodes()) {
100 return .escape_codes;100 return .escape_codes;
lib/std/process.zig+20
...@@ -179,6 +179,26 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned...@@ -179,6 +179,26 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned
179 }179 }
180}180}
181181
182pub fn hasEnvVarConstant(comptime key: []const u8) bool {
183 if (builtin.os.tag == .windows) {
184 const key_w = comptime std.unicode.utf8ToUtf16LeStringLiteral(key);
185 return std.os.getenvW(key_w) != null;
186 } else {
187 return os.getenv(key) != null;
188 }
189}
190
191pub fn hasEnvVar(allocator: *Allocator, key: []const u8) error{OutOfMemory}!bool {
192 if (builtin.os.tag == .windows) {
193 var stack_alloc = std.heap.stackFallback(256 * @sizeOf(u16), allocator);
194 const key_w = try std.unicode.utf8ToUtf16LeWithNull(&stack_alloc.allocator, key);
195 defer stack_alloc.allocator.free(key_w);
196 return std.os.getenvW(key_w) != null;
197 } else {
198 return os.getenv(key) != null;
199 }
200}
201
182test "os.getEnvVarOwned" {202test "os.getEnvVarOwned" {
183 var ga = std.testing.allocator;203 var ga = std.testing.allocator;
184 try testing.expectError(error.EnvironmentVariableNotFound, getEnvVarOwned(ga, "BADENV"));204 try testing.expectError(error.EnvironmentVariableNotFound, getEnvVarOwned(ga, "BADENV"));
lib/std/special/build_runner.zig+5
...@@ -63,6 +63,11 @@ pub fn main() !void {...@@ -63,6 +63,11 @@ pub fn main() !void {
63 var install_prefix: ?[]const u8 = null;63 var install_prefix: ?[]const u8 = null;
64 var dir_list = Builder.DirList{};64 var dir_list = Builder.DirList{};
6565
66 // before arg parsing, check for the NO_COLOR environment variable
67 // if it exists, default the color setting to .off
68 // explicit --color arguments will still override this setting.
69 builder.color = if (std.process.hasEnvVarConstant("NO_COLOR")) .off else .auto;
70
66 while (nextArg(args, &arg_idx)) |arg| {71 while (nextArg(args, &arg_idx)) |arg| {
67 if (mem.startsWith(u8, arg, "-D")) {72 if (mem.startsWith(u8, arg, "-D")) {
68 const option_contents = arg[2..];73 const option_contents = arg[2..];
src/main.zig+7-11
...@@ -503,15 +503,6 @@ const Emit = union(enum) {...@@ -503,15 +503,6 @@ const Emit = union(enum) {
503 }503 }
504};504};
505505
506fn optionalBoolEnvVar(arena: *Allocator, name: []const u8) !bool {
507 if (std.process.getEnvVarOwned(arena, name)) |_| {
508 return true;
509 } else |err| switch (err) {
510 error.EnvironmentVariableNotFound => return false,
511 else => |e| return e,
512 }
513}
514
515fn optionalStringEnvVar(arena: *Allocator, name: []const u8) !?[]const u8 {506fn optionalStringEnvVar(arena: *Allocator, name: []const u8) !?[]const u8 {
516 if (std.process.getEnvVarOwned(arena, name)) |value| {507 if (std.process.getEnvVarOwned(arena, name)) |value| {
517 return value;508 return value;
...@@ -548,8 +539,8 @@ fn buildOutputType(...@@ -548,8 +539,8 @@ fn buildOutputType(
548 var single_threaded = false;539 var single_threaded = false;
549 var function_sections = false;540 var function_sections = false;
550 var watch = false;541 var watch = false;
551 var verbose_link = try optionalBoolEnvVar(arena, "ZIG_VERBOSE_LINK");542 var verbose_link = std.process.hasEnvVarConstant("ZIG_VERBOSE_LINK");
552 var verbose_cc = try optionalBoolEnvVar(arena, "ZIG_VERBOSE_CC");543 var verbose_cc = std.process.hasEnvVarConstant("ZIG_VERBOSE_CC");
553 var verbose_air = false;544 var verbose_air = false;
554 var verbose_llvm_ir = false;545 var verbose_llvm_ir = false;
555 var verbose_cimport = false;546 var verbose_cimport = false;
...@@ -670,6 +661,11 @@ fn buildOutputType(...@@ -670,6 +661,11 @@ fn buildOutputType(
670 defer freePkgTree(gpa, &pkg_tree_root, false);661 defer freePkgTree(gpa, &pkg_tree_root, false);
671 var cur_pkg: *Package = &pkg_tree_root;662 var cur_pkg: *Package = &pkg_tree_root;
672663
664 // before arg parsing, check for the NO_COLOR environment variable
665 // if it exists, default the color setting to .off
666 // explicit --color arguments will still override this setting.
667 color = if (std.process.hasEnvVarConstant("NO_COLOR")) .off else .auto;
668
673 switch (arg_mode) {669 switch (arg_mode) {
674 .build, .translate_c, .zig_test, .run => {670 .build, .translate_c, .zig_test, .run => {
675 var optimize_mode_string: ?[]const u8 = null;671 var optimize_mode_string: ?[]const u8 = null;
src/stage1/errmsg.cpp+2-2
...@@ -16,8 +16,8 @@ enum ErrType {...@@ -16,8 +16,8 @@ enum ErrType {
16};16};
1717
18static void print_err_msg_type(ErrorMsg *err, ErrColor color, ErrType err_type) {18static void print_err_msg_type(ErrorMsg *err, ErrColor color, ErrType err_type) {
19 bool is_tty = os_stderr_tty();19 bool supports_color = os_stderr_supports_color();
20 bool use_colors = color == ErrColorOn || (color == ErrColorAuto && is_tty);20 bool use_colors = color == ErrColorOn || (color == ErrColorAuto && supports_color);
2121
22 // Show the error location, if available22 // Show the error location, if available
23 if (err->path != nullptr) {23 if (err->path != nullptr) {
src/stage1/os.cpp+3-2
...@@ -834,13 +834,14 @@ static bool is_stderr_cyg_pty(void) {...@@ -834,13 +834,14 @@ static bool is_stderr_cyg_pty(void) {
834}834}
835#endif835#endif
836836
837bool os_stderr_tty(void) {837bool os_stderr_supports_color(void) {
838 if (getenv("NO_COLOR") != NULL) return false;
838#if defined(ZIG_OS_WINDOWS)839#if defined(ZIG_OS_WINDOWS)
839 return _isatty(_fileno(stderr)) != 0 || is_stderr_cyg_pty();840 return _isatty(_fileno(stderr)) != 0 || is_stderr_cyg_pty();
840#elif defined(ZIG_OS_POSIX)841#elif defined(ZIG_OS_POSIX)
841 return isatty(STDERR_FILENO) != 0;842 return isatty(STDERR_FILENO) != 0;
842#else843#else
843#error "missing os_stderr_tty implementation"844#error "missing os_stderr_supports_color implementation"
844#endif845#endif
845}846}
846847
src/stage1/os.hpp+1-1
...@@ -99,7 +99,7 @@ Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents);...@@ -99,7 +99,7 @@ Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents);
9999
100Error ATTRIBUTE_MUST_USE os_get_cwd(Buf *out_cwd);100Error ATTRIBUTE_MUST_USE os_get_cwd(Buf *out_cwd);
101101
102bool os_stderr_tty(void);102bool os_stderr_supports_color(void);
103void os_stderr_set_color(TermColor color);103void os_stderr_set_color(TermColor color);
104104
105Error os_rename(Buf *src_path, Buf *dest_path);105Error os_rename(Buf *src_path, Buf *dest_path);