Skip to content

Commit

Permalink
build: fix tty conf retrieval
Browse files Browse the repository at this point in the history
As part of ziglang/zig#14647 and more
specifically this zig commit
ziglang/zig@bf73620,
the color is now communicated via env vars.

Signed-off-by: Nicolas Sterchele <[email protected]>
  • Loading branch information
sterchelen committed Mar 17, 2023
1 parent 8e64d13 commit f4838e7
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -532,31 +532,30 @@ pub fn build(b: *Builder) void {
std.os.exit(0);
}

use_color_escapes = false;
switch (b.color) {
.on => use_color_escapes = true,
.off => use_color_escapes = false,
.auto => {
if (std.io.getStdErr().supportsAnsiEscapeCodes()) {
use_color_escapes = true;
} else if (builtin.os.tag == .windows) {
const w32 = struct {
const WINAPI = std.os.windows.WINAPI;
const DWORD = std.os.windows.DWORD;
const ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
const STD_ERROR_HANDLE = @bitCast(DWORD, @as(i32, -12));
extern "kernel32" fn GetStdHandle(id: DWORD) callconv(WINAPI) ?*anyopaque;
extern "kernel32" fn GetConsoleMode(console: ?*anyopaque, out_mode: *DWORD) callconv(WINAPI) u32;
extern "kernel32" fn SetConsoleMode(console: ?*anyopaque, mode: DWORD) callconv(WINAPI) u32;
};
const handle = w32.GetStdHandle(w32.STD_ERROR_HANDLE);
var mode: w32.DWORD = 0;
if (w32.GetConsoleMode(handle, &mode) != 0) {
mode |= w32.ENABLE_VIRTUAL_TERMINAL_PROCESSING;
use_color_escapes = w32.SetConsoleMode(handle, mode) != 0;
}
if (b.env_map.get("NO_COLOR") != null) {
use_color_escapes = false;
} else if (b.env_map.get("ZIG_DEBUG_COLOR") != null) {
use_color_escapes = true;
} else {
if (std.io.getStdErr().supportsAnsiEscapeCodes()) {
use_color_escapes = true;
} else if (builtin.os.tag == .windows) {
const w32 = struct {
const WINAPI = std.os.windows.WINAPI;
const DWORD = std.os.windows.DWORD;
const ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
const STD_ERROR_HANDLE = @bitCast(DWORD, @as(i32, -12));
extern "kernel32" fn GetStdHandle(id: DWORD) callconv(WINAPI) ?*anyopaque;
extern "kernel32" fn GetConsoleMode(console: ?*anyopaque, out_mode: *DWORD) callconv(WINAPI) u32;
extern "kernel32" fn SetConsoleMode(console: ?*anyopaque, mode: DWORD) callconv(WINAPI) u32;
};
const handle = w32.GetStdHandle(w32.STD_ERROR_HANDLE);
var mode: w32.DWORD = 0;
if (w32.GetConsoleMode(handle, &mode) != 0) {
mode |= w32.ENABLE_VIRTUAL_TERMINAL_PROCESSING;
use_color_escapes = w32.SetConsoleMode(handle, mode) != 0;
}
},
}
}

if (use_color_escapes) {
Expand Down

0 comments on commit f4838e7

Please sign in to comment.