From 59a12cd293a6d84090071b0ebb4828cd6626e079 Mon Sep 17 00:00:00 2001 From: Christofer Nolander Date: Fri, 14 Apr 2023 16:07:51 +0200 Subject: [PATCH 1/2] windows: detect ANSI support in more terminals --- lib/std/fs/file.zig | 7 +++++++ lib/std/os/windows.zig | 2 ++ 2 files changed, 9 insertions(+) diff --git a/lib/std/fs/file.zig b/lib/std/fs/file.zig index 3ed4b07a3d..0a2fbf74ad 100644 --- a/lib/std/fs/file.zig +++ b/lib/std/fs/file.zig @@ -230,6 +230,13 @@ pub const File = struct { /// Test whether ANSI escape codes will be treated as such. pub fn supportsAnsiEscapeCodes(self: File) bool { if (builtin.os.tag == .windows) { + if (!os.isatty(self.handle)) return false; + + var console_mode: os.windows.DWORD = 0; + if (os.windows.kernel32.GetConsoleMode(self.handle, &console_mode) != 0) { + if (console_mode & os.windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0) return true; + } + return os.isCygwinPty(self.handle); } if (builtin.os.tag == .wasi) { diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig index 2c3b1ded8e..c591f8cf7f 100644 --- a/lib/std/os/windows.zig +++ b/lib/std/os/windows.zig @@ -3387,6 +3387,8 @@ pub const CONSOLE_SCREEN_BUFFER_INFO = extern struct { dwMaximumWindowSize: COORD, }; +pub const ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4; + pub const FOREGROUND_BLUE = 1; pub const FOREGROUND_GREEN = 2; pub const FOREGROUND_RED = 4; From 5439f36c976ac31f7faa27cc1536a0ecd3c4469f Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Sat, 17 Jun 2023 23:36:10 -0700 Subject: [PATCH 2/2] Remove unnecessary isatty call from supportsAnsiEscapeCodes isatty on Windows is implemented as a isCygwinPty call and a GetConsoleMode call, so calling isatty just duplicates the function calls we already need to do in supportsAnsiEscapeCodes. --- lib/std/fs/file.zig | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/std/fs/file.zig b/lib/std/fs/file.zig index 0a2fbf74ad..7b954399d8 100644 --- a/lib/std/fs/file.zig +++ b/lib/std/fs/file.zig @@ -230,8 +230,6 @@ pub const File = struct { /// Test whether ANSI escape codes will be treated as such. pub fn supportsAnsiEscapeCodes(self: File) bool { if (builtin.os.tag == .windows) { - if (!os.isatty(self.handle)) return false; - var console_mode: os.windows.DWORD = 0; if (os.windows.kernel32.GetConsoleMode(self.handle, &console_mode) != 0) { if (console_mode & os.windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0) return true;