Skip to content

Commit e2b954c

Browse files
SpexGuyandrewrk
authored andcommitted
Add support for NO_COLOR
1 parent 7bdeda8 commit e2b954c

File tree

7 files changed

+42
-20
lines changed

7 files changed

+42
-20
lines changed

lib/std/debug.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ pub fn getSelfDebugInfo() !*DebugInfo {
9090
}
9191

9292
pub 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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,26 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned
179179
}
180180
}
181181

182+
pub 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+
191+
pub 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+
182202
test "os.getEnvVarOwned" {
183203
var ga = std.testing.allocator;
184204
try testing.expectError(error.EnvironmentVariableNotFound, getEnvVarOwned(ga, "BADENV"));

lib/std/special/build_runner.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -503,15 +503,6 @@ const Emit = union(enum) {
503503
}
504504
};
505505

506-
fn 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-
515506
fn 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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ enum ErrType {
1616
};
1717

1818
static 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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,13 +834,14 @@ static bool is_stderr_cyg_pty(void) {
834834
}
835835
#endif
836836

837-
bool os_stderr_tty(void) {
837+
bool 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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents);
9999

100100
Error ATTRIBUTE_MUST_USE os_get_cwd(Buf *out_cwd);
101101

102-
bool os_stderr_tty(void);
102+
bool os_stderr_supports_color(void);
103103
void os_stderr_set_color(TermColor color);
104104

105105
Error os_rename(Buf *src_path, Buf *dest_path);

0 commit comments

Comments
 (0)