Skip to content

Commit

Permalink
std.io.tty: cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
wooster0 committed Dec 31, 2024
1 parent cce4a6a commit 2f9a873
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions lib/std/io/tty.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const native_os = builtin.os.tag;
/// respecting the `NO_COLOR` and `CLICOLOR_FORCE` environment variables to override the default.
/// Will attempt to enable ANSI escape code support if necessary/possible.
pub fn detectConfig(file: File) Config {
const force_color: ?bool = if (builtin.os.tag == .wasi)
const force_color: ?bool = if (native_os == .wasi)
null // wasi does not support environment variables
else if (process.hasEnvVarConstant("NO_COLOR"))
false
Expand Down Expand Up @@ -60,8 +60,6 @@ pub const Color = enum {
reset,
};

/// Provides simple functionality for manipulating the terminal in some way,
/// such as coloring text, etc.
pub const Config = union(enum) {
no_color,
escape_codes,
Expand All @@ -73,12 +71,12 @@ pub const Config = union(enum) {
};

pub fn setColor(
conf: Config,
config: Config,
writer: anytype,
color: Color,
) (@typeInfo(@TypeOf(writer.writeAll(""))).error_union.error_set ||
windows.SetConsoleTextAttributeError)!void {
nosuspend switch (conf) {
nosuspend switch (config) {
.no_color => return,
.escape_codes => {
const color_string = switch (color) {
Expand All @@ -104,7 +102,8 @@ pub const Config = union(enum) {
};
try writer.writeAll(color_string);
},
.windows_api => |ctx| if (native_os == .windows) {
.windows_api => |ctx| {
std.debug.assert(native_os == .windows);
const attributes = switch (color) {
.black => 0,
.red => windows.FOREGROUND_RED,
Expand All @@ -128,8 +127,6 @@ pub const Config = union(enum) {
.reset => ctx.reset_attributes,
};
try windows.SetConsoleTextAttribute(ctx.handle, attributes);
} else {
unreachable;
},
};
}
Expand Down

0 comments on commit 2f9a873

Please sign in to comment.