pub const Mode = union(enum) no_color: no_colorescape_codes: escape_codeswindows_api: WindowsApipub fn detect(io: Io, file: File, NO_COLOR: bool, CLICOLOR_FORCE: bool) Io.Cancelable!ModeDetect suitable TTY configuration options for the given file (commonly stdout/stderr).
Will attempt to enable ANSI escape code support if necessary/possible.
NO_COLOR indicates whether "NO_COLOR" environment variable is present and non-empty.CLICOLOR_FORCE indicates whether "CLICOLOR_FORCE" environment variable is present and non-empty.37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81pub const Mode = union(enum) {
no_color,
escape_codes,
windows_api: WindowsApi,
pub const WindowsApi = if (!is_windows) noreturn else struct {
io: Io,
file: File,
reset_attributes: u16,
};
/// Detect suitable TTY configuration options for the given file (commonly
/// stdout/stderr).
///
/// Will attempt to enable ANSI escape code support if necessary/possible.
///
/// * `NO_COLOR` indicates whether "NO_COLOR" environment variable is
/// present and non-empty.
/// * `CLICOLOR_FORCE` indicates whether "CLICOLOR_FORCE" environment
/// variable is present and non-empty.
pub fn detect(io: Io, file: File, NO_COLOR: bool, CLICOLOR_FORCE: bool) Io.Cancelable!Mode {
const force_color: ?bool = if (NO_COLOR) false else if (CLICOLOR_FORCE) true else null;
if (force_color == false) return .no_color;
if (file.enableAnsiEscapeCodes(io)) |_| {
return .escape_codes;
} else |err| switch (err) {
error.Canceled => |e| return e,
error.NotTerminalDevice, error.Unexpected => {},
}
if (is_windows and try file.isTty(io)) {
var get_console_info = std.os.windows.CONSOLE.USER_IO.GET_SCREEN_BUFFER_INFO;
switch (try get_console_info.operate(io, file)) {
.SUCCESS => return .{ .windows_api = .{
.io = io,
.file = file,
.reset_attributes = get_console_info.Data.wAttributes,
} },
else => {},
}
}
return if (force_color == true) .escape_codes else .no_color;
}
}