From 8e69a18d8c01aaf2bf4f0c0f8495e47cb36c284f Mon Sep 17 00:00:00 2001 From: emekoi Date: Wed, 7 Nov 2018 23:36:36 -0600 Subject: [PATCH] made colored output more consistent (#1706) * made colored output more consistent * added os.supportsAnsiEscapeCodes --- src/os.cpp | 4 -- std/debug/index.zig | 87 ++++++++++++++++++++++++++--------------- std/os/index.zig | 12 ++++++ std/os/windows/util.zig | 2 +- 4 files changed, 69 insertions(+), 36 deletions(-) diff --git a/src/os.cpp b/src/os.cpp index 27937be164..ea88a8ef98 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -1128,9 +1128,6 @@ Error os_get_cwd(Buf *out_cwd) { #define is_wprefix(s, prefix) \ (wcsncmp((s), (prefix), sizeof(prefix) / sizeof(WCHAR) - 1) == 0) static bool is_stderr_cyg_pty(void) { -#if defined(__MINGW32__) - return false; -#else HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE); if (stderr_handle == INVALID_HANDLE_VALUE) return false; @@ -1182,7 +1179,6 @@ static bool is_stderr_cyg_pty(void) { } free(nameinfo); return (p != NULL); -#endif } #endif diff --git a/std/debug/index.zig b/std/debug/index.zig index 0508c17feb..a0d2f65339 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -171,7 +171,9 @@ pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, c os.abort(); } +const RED = "\x1b[31;1m"; const GREEN = "\x1b[32;1m"; +const CYAN = "\x1b[36;1m"; const WHITE = "\x1b[37;1m"; const DIM = "\x1b[2m"; const RESET = "\x1b[0m"; @@ -454,38 +456,61 @@ const TtyColor = enum.{ /// TODO this is a special case hack right now. clean it up and maybe make it part of std.fmt fn setTtyColor(tty_color: TtyColor) void { - const S = struct.{ - var attrs: windows.WORD = undefined; - var init_attrs = false; - }; - if (!S.init_attrs) { - S.init_attrs = true; - var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined; - // TODO handle error - _ = windows.GetConsoleScreenBufferInfo(stderr_file.handle, &info); - S.attrs = info.wAttributes; - } + if (os.supportsAnsiEscapeCodes(stderr_file.handle)) { + switch (tty_color) { + TtyColor.Red => { + stderr_file.write(RED) catch return; + }, + TtyColor.Green => { + stderr_file.write(GREEN) catch return; + }, + TtyColor.Cyan => { + stderr_file.write(CYAN) catch return; + }, + TtyColor.White, TtyColor.Bold => { + stderr_file.write(WHITE) catch return; + }, + TtyColor.Dim => { + stderr_file.write(DIM) catch return; + }, + TtyColor.Reset => { + stderr_file.write(RESET) catch return; + }, + } + } else { + const S = struct.{ + var attrs: windows.WORD = undefined; + var init_attrs = false; + }; + if (!S.init_attrs) { + S.init_attrs = true; + var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined; + // TODO handle error + _ = windows.GetConsoleScreenBufferInfo(stderr_file.handle, &info); + S.attrs = info.wAttributes; + } - // TODO handle errors - switch (tty_color) { - TtyColor.Red => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_INTENSITY); - }, - TtyColor.Green => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_INTENSITY); - }, - TtyColor.Cyan => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY); - }, - TtyColor.White, TtyColor.Bold => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY); - }, - TtyColor.Dim => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_INTENSITY); - }, - TtyColor.Reset => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, S.attrs); - }, + // TODO handle errors + switch (tty_color) { + TtyColor.Red => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_INTENSITY); + }, + TtyColor.Green => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_INTENSITY); + }, + TtyColor.Cyan => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY); + }, + TtyColor.White, TtyColor.Bold => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY); + }, + TtyColor.Dim => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_INTENSITY); + }, + TtyColor.Reset => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, S.attrs); + }, + } } } diff --git a/std/os/index.zig b/std/os/index.zig index d9e6c3c81d..c04baeeb91 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -2272,6 +2272,18 @@ pub fn isTty(handle: FileHandle) bool { } } +pub fn supportsAnsiEscapeCodes(handle: FileHandle) bool { + if (is_windows) { + return windows_util.windowsIsCygwinPty(handle); + } else { + if (builtin.link_libc) { + return c.isatty(handle) != 0; + } else { + return posix.isatty(handle); + } + } +} + pub const PosixSocketError = error.{ /// Permission to create a socket of the specified type and/or /// pro‐tocol is denied. diff --git a/std/os/windows/util.zig b/std/os/windows/util.zig index 07d68b0a46..cb245f1190 100644 --- a/std/os/windows/util.zig +++ b/std/os/windows/util.zig @@ -91,7 +91,7 @@ pub fn windowsIsCygwinPty(handle: windows.HANDLE) bool { @ptrCast(*c_void, &name_info_bytes[0]), @intCast(u32, name_info_bytes.len), ) == 0) { - return true; + return false; } const name_info = @ptrCast(*const windows.FILE_NAME_INFO, &name_info_bytes[0]);