diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-06-22 06:54:15 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-22 06:54:15 -0700 |
| commit | 93e54f2354aa1b3de797a7ca5572bfa8c0f733f1 (patch) | |
| tree | 93fbbe87a2876a424e3fa267014cc109e3a8de26 /lib/std | |
| parent | 64f0059cd33b571d6cf91df45f4cb2e0af9c0742 (diff) | |
| parent | e45d24c0de29eb6668e56ea927e15505674833a6 (diff) | |
| download | zig-93e54f2354aa1b3de797a7ca5572bfa8c0f733f1.tar.gz zig-93e54f2354aa1b3de797a7ca5572bfa8c0f733f1.zip | |
Merge pull request #16110 from dweiller/force-tty-color
std.io.tty: cleanup detectConfig
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/io/tty.zig | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/lib/std/io/tty.zig b/lib/std/io/tty.zig index 04a84dc375..bbae90198e 100644 --- a/lib/std/io/tty.zig +++ b/lib/std/io/tty.zig @@ -7,29 +7,34 @@ const native_os = builtin.os.tag; /// Detect suitable TTY configuration options for the given file (commonly stdout/stderr). /// This includes feature checks for ANSI escape codes and the Windows console API, as well as -/// respecting the `NO_COLOR` environment variable. +/// respecting the `NO_COLOR` and `YES_COLOR` environment variables to override the default. pub fn detectConfig(file: File) Config { - if (builtin.os.tag == .wasi) { - // Per https://github.com/WebAssembly/WASI/issues/162 ANSI codes - // aren't currently supported. - return .no_color; - } else if (process.hasEnvVarConstant("ZIG_DEBUG_COLOR")) { - return .escape_codes; - } else if (process.hasEnvVarConstant("NO_COLOR")) { - return .no_color; - } else if (file.supportsAnsiEscapeCodes()) { - return .escape_codes; - } else if (native_os == .windows and file.isTty()) { + const force_color: ?bool = if (builtin.os.tag == .wasi) + null // wasi does not support environment variables + else if (process.hasEnvVarConstant("NO_COLOR")) + false + else if (process.hasEnvVarConstant("YES_COLOR")) + true + else + null; + + if (force_color == false) return .no_color; + + if (native_os == .windows and file.isTty()) { var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined; if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE) { - // TODO: Should this return an error instead? - return .no_color; + return if (force_color == true) .escape_codes else .no_color; } return .{ .windows_api = .{ .handle = file.handle, .reset_attributes = info.wAttributes, } }; } + + if (force_color == true or file.supportsAnsiEscapeCodes()) { + return .escape_codes; + } + return .no_color; } |
