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 {
9090}
9191
9292pub fn detectTTYConfig() TTY.Config {
93 var bytes: [128]u8 = undefined;
94 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;
95 if (process.getEnvVarOwned(allocator, "ZIG_DEBUG_COLOR")) |_| {
93 if (process.hasEnvVarConstant("ZIG_DEBUG_COLOR")) {
9694 return .escape_codes;
97 } else |_| {
95 } else if (process.hasEnvVarConstant("NO_COLOR")) {
96 return .no_color;
97 } else {
9898 const stderr_file = io.getStdErr();
9999 if (stderr_file.supportsAnsiEscapeCodes()) {
100100 return .escape_codes;
lib/std/process.zig+20
......@@ -179,6 +179,26 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned
179179 }
180180}
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
182202test "os.getEnvVarOwned" {
183203 var ga = std.testing.allocator;
184204 try testing.expectError(error.EnvironmentVariableNotFound, getEnvVarOwned(ga, "BADENV"));
lib/std/special/build_runner.zig+5
......@@ -63,6 +63,11 @@ pub fn main() !void {
6363 var install_prefix: ?[]const u8 = null;
6464 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
6671 while (nextArg(args, &arg_idx)) |arg| {
6772 if (mem.startsWith(u8, arg, "-D")) {
6873 const option_contents = arg[2..];
src/main.zig+7-11
......@@ -503,15 +503,6 @@ const Emit = union(enum) {
503503 }
504504};
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
515506fn optionalStringEnvVar(arena: *Allocator, name: []const u8) !?[]const u8 {
516507 if (std.process.getEnvVarOwned(arena, name)) |value| {
517508 return value;
......@@ -548,8 +539,8 @@ fn buildOutputType(
548539 var single_threaded = false;
549540 var function_sections = false;
550541 var watch = false;
551 var verbose_link = try optionalBoolEnvVar(arena, "ZIG_VERBOSE_LINK");
552 var verbose_cc = try optionalBoolEnvVar(arena, "ZIG_VERBOSE_CC");
542 var verbose_link = std.process.hasEnvVarConstant("ZIG_VERBOSE_LINK");
543 var verbose_cc = std.process.hasEnvVarConstant("ZIG_VERBOSE_CC");
553544 var verbose_air = false;
554545 var verbose_llvm_ir = false;
555546 var verbose_cimport = false;
......@@ -670,6 +661,11 @@ fn buildOutputType(
670661 defer freePkgTree(gpa, &pkg_tree_root, false);
671662 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
673669 switch (arg_mode) {
674670 .build, .translate_c, .zig_test, .run => {
675671 var optimize_mode_string: ?[]const u8 = null;
src/stage1/errmsg.cpp+2-2
......@@ -16,8 +16,8 @@ enum ErrType {
1616};
1717
1818static void print_err_msg_type(ErrorMsg *err, ErrColor color, ErrType err_type) {
19 bool is_tty = os_stderr_tty();
20 bool use_colors = color == ErrColorOn || (color == ErrColorAuto && is_tty);
19 bool supports_color = os_stderr_supports_color();
20 bool use_colors = color == ErrColorOn || (color == ErrColorAuto && supports_color);
2121
2222 // Show the error location, if available
2323 if (err->path != nullptr) {
src/stage1/os.cpp+3-2
......@@ -834,13 +834,14 @@ static bool is_stderr_cyg_pty(void) {
834834}
835835#endif
836836
837bool os_stderr_tty(void) {
837bool os_stderr_supports_color(void) {
838 if (getenv("NO_COLOR") != NULL) return false;
838839#if defined(ZIG_OS_WINDOWS)
839840 return _isatty(_fileno(stderr)) != 0 || is_stderr_cyg_pty();
840841#elif defined(ZIG_OS_POSIX)
841842 return isatty(STDERR_FILENO) != 0;
842843#else
843#error "missing os_stderr_tty implementation"
844#error "missing os_stderr_supports_color implementation"
844845#endif
845846}
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);
9999
100100Error ATTRIBUTE_MUST_USE os_get_cwd(Buf *out_cwd);
101101
102bool os_stderr_tty(void);
102bool os_stderr_supports_color(void);
103103void os_stderr_set_color(TermColor color);
104104
105105Error os_rename(Buf *src_path, Buf *dest_path);