Replace YES_COLOR with CLICOLOR_FORCE

Instead of introducing YES_COLOR, a completely new standard, into the mix
it might make more sense to instead tag along with the CLICOLOR_FORCE env var,
which dates back to at least 2000 with FreeBSD 4.1.1 and which is
supported by tools like CMake.

<https://bixense.com/clicolors/>
This commit is contained in:
Carl Åstholm 2024-06-02 18:11:16 +02:00 committed by Andrew Kelley
parent 85eb5a3069
commit d74180c373
6 changed files with 14 additions and 8 deletions

View File

@ -285,7 +285,7 @@ pub fn main() !void {
const ttyconf = get_tty_conf(color, stderr);
switch (ttyconf) {
.no_color => try graph.env_map.put("NO_COLOR", "1"),
.escape_codes => try graph.env_map.put("YES_COLOR", "1"),
.escape_codes => try graph.env_map.put("CLICOLOR_FORCE", "1"),
.windows_api => {},
}

View File

@ -7,13 +7,13 @@ 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` and `YES_COLOR` environment variables to override the default.
/// respecting the `NO_COLOR` and `CLICOLOR_FORCE` environment variables to override the default.
pub fn detectConfig(file: File) Config {
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"))
else if (process.hasEnvVarConstant("CLICOLOR_FORCE"))
true
else
null;

View File

@ -1060,6 +1060,7 @@ pub const EnvVar = enum {
ZIG_DEBUG_CMD,
CC,
NO_COLOR,
CLICOLOR_FORCE,
XDG_CACHE_HOME,
HOME,

View File

@ -994,11 +994,16 @@ fn buildOutputType(
.native_system_include_paths = &.{},
};
// before arg parsing, check for the NO_COLOR environment variable
// if it exists, default the color setting to .off
// before arg parsing, check for the NO_COLOR and CLICOLOR_FORCE environment variables
// if set, default the color setting to .off or .on, respectively
// explicit --color arguments will still override this setting.
// Disable color on WASI per https://github.com/WebAssembly/WASI/issues/162
var color: Color = if (native_os == .wasi or EnvVar.NO_COLOR.isSet()) .off else .auto;
var color: Color = if (native_os == .wasi or EnvVar.NO_COLOR.isSet())
.off
else if (EnvVar.CLICOLOR_FORCE.isSet())
.on
else
.auto;
switch (arg_mode) {
.build, .translate_c, .zig_test, .run => {

View File

@ -61,7 +61,7 @@ fn addExpect(
});
const run = b.addRunArtifact(exe);
run.removeEnvironmentVariable("YES_COLOR");
run.removeEnvironmentVariable("CLICOLOR_FORCE");
run.setEnvironmentVariable("NO_COLOR", "1");
run.expectExitCode(1);
run.expectStdOutEqual("");

View File

@ -104,7 +104,7 @@ fn printOutput(
tmp_dir_path: []const u8,
) !void {
var env_map = try process.getEnvMap(arena);
try env_map.put("YES_COLOR", "1");
try env_map.put("CLICOLOR_FORCE", "1");
const host = try std.zig.system.resolveTargetQuery(.{});
const obj_ext = builtin.object_format.fileExt(builtin.cpu.arch);