Merge pull request #16110 from dweiller/force-tty-color
std.io.tty: cleanup detectConfig
This commit is contained in:
@@ -1305,7 +1305,7 @@ fn genHtml(
|
||||
defer root_node.end();
|
||||
|
||||
var env_map = try process.getEnvMap(allocator);
|
||||
try env_map.put("ZIG_DEBUG_COLOR", "1");
|
||||
try env_map.put("YES_COLOR", "1");
|
||||
|
||||
const host = try std.zig.system.NativeTargetInfo.detect(.{});
|
||||
const builtin_code = try getBuiltinCode(allocator, &env_map, zig_exe, opt_zig_lib_dir);
|
||||
|
||||
@@ -282,7 +282,7 @@ pub fn main() !void {
|
||||
const ttyconf = get_tty_conf(color, stderr);
|
||||
switch (ttyconf) {
|
||||
.no_color => try builder.env_map.put("NO_COLOR", "1"),
|
||||
.escape_codes => try builder.env_map.put("ZIG_DEBUG_COLOR", "1"),
|
||||
.escape_codes => try builder.env_map.put("YES_COLOR", "1"),
|
||||
.windows_api => {},
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ fn addExpect(
|
||||
});
|
||||
|
||||
const run = b.addRunArtifact(exe);
|
||||
run.removeEnvironmentVariable("ZIG_DEBUG_COLOR");
|
||||
run.removeEnvironmentVariable("YES_COLOR");
|
||||
run.setEnvironmentVariable("NO_COLOR", "1");
|
||||
run.expectExitCode(1);
|
||||
run.expectStdOutEqual("");
|
||||
|
||||
Reference in New Issue
Block a user