Skip to content

windows: detect ANSI support in more terminals #15282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/std/fs/file.zig
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,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;
Copy link
Collaborator

@squeek502 squeek502 May 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant and can (should?) be removed, the implementation of os.isatty on Windows is:

        if (isCygwinPty(handle))
            return true;

        var out: windows.DWORD = undefined;
        return windows.kernel32.GetConsoleMode(handle, &out) != 0;


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) {
Expand Down
2 changes: 2 additions & 0 deletions lib/std/os/windows.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3151,6 +3151,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;
Expand Down