authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-06-20 16:34:38+10:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-06-22 10:29:45+10:00
loge05412669c8dde1230612de5af64fbc3fb0bc17e
tree9d6b7d7cc938ec109f6aa9069ca9a39b9f6bae2b
parent128fd7dd02ae112cba68d0ad6cc40dc1ce8c34b1

std.io.tty: add detectTtyConfigForce for forcing color


1 files changed, 19 insertions(+), 14 deletions(-)

lib/std/io/tty.zig+19-14
...@@ -7,29 +7,34 @@ const native_os = builtin.os.tag;...@@ -7,29 +7,34 @@ const native_os = builtin.os.tag;
77
8/// Detect suitable TTY configuration options for the given file (commonly stdout/stderr).8/// Detect suitable TTY configuration options for the given file (commonly stdout/stderr).
9/// This includes feature checks for ANSI escape codes and the Windows console API, as well as9/// 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.
11pub fn detectConfig(file: File) Config {11pub fn detectConfig(file: File) Config {
12 if (builtin.os.tag == .wasi) {12 const force_color: ?bool = if (builtin.os.tag == .wasi)
13 // Per https://github.com/WebAssembly/WASI/issues/162 ANSI codes13 null // wasi does not support environment variables
14 // aren't currently supported.14 else if (process.hasEnvVarConstant("ZIG_DEBUG_COLOR"))
15 return .no_color;15 false
16 } else if (process.hasEnvVarConstant("ZIG_DEBUG_COLOR")) {16 else if (process.hasEnvVarConstant("YES_COLOR"))
17 return .escape_codes;17 true
18 } else if (process.hasEnvVarConstant("NO_COLOR")) {18 else
19 return .no_color;19 null;
20 } else if (file.supportsAnsiEscapeCodes()) {20
21 return .escape_codes;21 if (force_color == false) return .no_color;
22 } else if (native_os == .windows and file.isTty()) {22
23 if (native_os == .windows and file.isTty()) {
23 var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;24 var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
24 if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE) {25 if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE) {
25 // TODO: Should this return an error instead?26 return if (force_color == true) .escape_codes else .no_color;
26 return .no_color;
27 }27 }
28 return .{ .windows_api = .{28 return .{ .windows_api = .{
29 .handle = file.handle,29 .handle = file.handle,
30 .reset_attributes = info.wAttributes,30 .reset_attributes = info.wAttributes,
31 } };31 } };
32 }32 }
33
34 if (force_color == true or file.supportsAnsiEscapeCodes()) {
35 return .escape_codes;
36 }
37
33 return .no_color;38 return .no_color;
34}39}
3540