Skip to content

Commit c3d82d8

Browse files
committed
std.io.tty.detectConfig: rework
* move environment variable checks into detectConfig * rename ZIG_DEBUG_COLOR to YES_COLOR to mirror NO_COLOR * color forcing should still apply to wasi * support color forcing off as well as on * when forcing on, if windows APIs are not available, use escape codes
1 parent 09b2216 commit c3d82d8

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

lib/std/io/tty.zig

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,44 @@ 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 `NO_COLOR` and `YES_COLOR` environment variables to override the default.
1111
pub fn detectConfig(file: File) Config {
12-
return detectConfigForce(file, false);
12+
return detectConfigForce(file, if (process.hasEnvVarConstant("NO_COLOR"))
13+
false
14+
else if (process.hasEnvVarConstant("YES_COLOR"))
15+
true
16+
else
17+
null);
1318
}
1419

15-
/// Detect suitable TTY configuration options, but force the use of color if the platform
16-
/// supports it. On platforms such as wasi that do not support ANSI codes, if they are
17-
/// desired for some reason just use `Config.escape_codes` directly.
18-
pub fn detectConfigForce(file: File, force_color: bool) Config {
19-
if (builtin.os.tag == .wasi) {
20-
// Per https://github.com/WebAssembly/WASI/issues/162 ANSI codes
21-
// aren't currently supported.
22-
return .no_color;
23-
} else if (process.hasEnvVarConstant("ZIG_DEBUG_COLOR")) {
24-
return .escape_codes;
25-
} else if (process.hasEnvVarConstant("NO_COLOR") and !force_color) {
26-
return .no_color;
27-
} else if (file.supportsAnsiEscapeCodes()) {
28-
return .escape_codes;
29-
} else if (native_os == .windows and file.isTty()) {
20+
/// Detect suitable TTY configuration options, along with an override parameter.
21+
/// Even if `force_color` is `true`, this will prefer Windows console API calls
22+
/// if `file` is a Windows terminal.
23+
pub fn detectConfigForce(file: File, force_color: ?bool) Config {
24+
if (force_color == false) return .no_color;
25+
26+
if (native_os == .windows and file.isTty()) {
3027
var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
3128
if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE) {
32-
// TODO: Should this return an error instead?
33-
return .no_color;
29+
return if (force_color == true) .escape_codes else .no_color;
3430
}
3531
return .{ .windows_api = .{
3632
.handle = file.handle,
3733
.reset_attributes = info.wAttributes,
3834
} };
3935
}
40-
return if (force_color) .escape_codes else .no_color;
36+
37+
if (builtin.os.tag == .wasi and force_color != true) {
38+
// Per https://github.com/WebAssembly/WASI/issues/162 ANSI codes
39+
// aren't currently supported.
40+
return .no_color;
41+
}
42+
43+
if (force_color == true or file.supportsAnsiEscapeCodes()) {
44+
return .escape_codes;
45+
}
46+
47+
return .no_color;
4148
}
4249

4350
pub const Color = enum {

0 commit comments

Comments
 (0)