Skip to content

Commit e054126

Browse files
author
dweiller
committed
std.io.tty: add detectTtyConfigForce for forcing color
1 parent 128fd7d commit e054126

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

lib/std/io/tty.zig

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,34 @@ const native_os = builtin.os.tag;
77

88
/// Detect suitable TTY configuration options for the given file (commonly stdout/stderr).
99
/// This includes feature checks for ANSI escape codes and the Windows console API, as well as
10-
/// respecting the `NO_COLOR` environment variable.
10+
/// respecting the `ZIG_DEBUG_COLOR` and `YES_COLOR` environment variables to override the default.
1111
pub fn detectConfig(file: File) Config {
12-
if (builtin.os.tag == .wasi) {
13-
// Per https://github.com/WebAssembly/WASI/issues/162 ANSI codes
14-
// aren't currently supported.
15-
return .no_color;
16-
} else if (process.hasEnvVarConstant("ZIG_DEBUG_COLOR")) {
17-
return .escape_codes;
18-
} else if (process.hasEnvVarConstant("NO_COLOR")) {
19-
return .no_color;
20-
} else if (file.supportsAnsiEscapeCodes()) {
21-
return .escape_codes;
22-
} else if (native_os == .windows and file.isTty()) {
12+
const force_color: ?bool = if (builtin.os.tag == .wasi)
13+
null // wasi does not support environment variables
14+
else if (process.hasEnvVarConstant("ZIG_DEBUG_COLOR"))
15+
false
16+
else if (process.hasEnvVarConstant("YES_COLOR"))
17+
true
18+
else
19+
null;
20+
21+
if (force_color == false) return .no_color;
22+
23+
if (native_os == .windows and file.isTty()) {
2324
var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
2425
if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE) {
25-
// TODO: Should this return an error instead?
26-
return .no_color;
26+
return if (force_color == true) .escape_codes else .no_color;
2727
}
2828
return .{ .windows_api = .{
2929
.handle = file.handle,
3030
.reset_attributes = info.wAttributes,
3131
} };
3232
}
33+
34+
if (force_color == true or file.supportsAnsiEscapeCodes()) {
35+
return .escape_codes;
36+
}
37+
3338
return .no_color;
3439
}
3540

0 commit comments

Comments
 (0)