Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix color preference parsing from env #22497

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/std/zig.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
//! source lives here. These APIs are provided as-is and have absolutely no API
//! guarantees whatsoever.

const builtin = @import("builtin");
const native_os = builtin.os.tag;

pub const ErrorBundle = @import("zig/ErrorBundle.zig");
pub const Server = @import("zig/Server.zig");
pub const Client = @import("zig/Client.zig");
Expand Down Expand Up @@ -45,6 +48,20 @@ pub const Color = enum {
/// Assume stderr is a terminal.
on,

pub fn getEnv(arena: std.mem.Allocator) !Color {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like something like getFromEnv would be more appropriate naming-wise

// Disable color on WASI per https://github.com/WebAssembly/WASI/issues/162
if (native_os == .wasi) return .off;
// https://no-color.org/
// Command-line software which adds ANSI color to its output by default should check for
// a NO_COLOR environment variable that, when present and not an empty string (regardless
// of its value), prevents the addition of ANSI color.
if (try EnvVar.NO_COLOR.get(arena)) |no_color| {
if (no_color.len != 0) return .off;
}
// There is no specification for CLICOLOR_FORCE, so we will just check if it is set.
return if (EnvVar.CLICOLOR_FORCE.isSet()) .on else .auto;
}

pub fn get_tty_conf(color: Color) std.io.tty.Config {
return switch (color) {
.auto => std.io.tty.detectConfig(std.io.getStdErr()),
Expand Down
13 changes: 3 additions & 10 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -992,16 +992,9 @@ fn buildOutputType(
};
defer create_module.link_inputs.deinit(gpa);

// 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 if (EnvVar.CLICOLOR_FORCE.isSet())
.on
else
.auto;
// Detect color preferences using environment variables.
// Explicit --color arguments will still override this setting.
var color: Color = try .getEnv(arena);
var n_jobs: ?u32 = null;

switch (arg_mode) {
Expand Down