Skip to content

Commit

Permalink
lib/std/fs/File: enable VT seq support for Windows Console
Browse files Browse the repository at this point in the history
* Newer versions of Windows added VT seq support not only in Windows Terminal, but also in the old-fashioned Windows Console (standalone conhost.exe), though not enabled by default.
* Try setting the newer console mode flags provides better experience for Windows Console users.

Co-authored-by: Kexy Biscuit <kexybiscuit@biscuitt.in>
  • Loading branch information
Garfield550 and KexyBiscuit committed Jan 28, 2024
1 parent 0d2a328 commit 7f875f1
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/std/fs/File.zig
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,24 @@ pub fn isTty(self: File) bool {
/// Test whether ANSI escape codes will be treated as such.
pub fn supportsAnsiEscapeCodes(self: File) bool {
if (builtin.os.tag == .windows) {
var console_mode: windows.DWORD = 0;
if (windows.kernel32.GetConsoleMode(self.handle, &console_mode) != 0) {
if (console_mode & windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0) return true;
var original_console_mode: windows.DWORD = 0;

// For Windows Terminal, VT Sequences processing is enabled by default.
if (windows.kernel32.GetConsoleMode(self.handle, &original_console_mode) != 0) {
if (original_console_mode & windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0) return true;

// For Windows Console, VT Sequences processing support was added in Windows 10 build 14361, but disabled by default.
// https://devblogs.microsoft.com/commandline/tmux-support-arrives-for-bash-on-ubuntu-on-windows/
// Use Microsoft's recommended way to enable virtual terminal processing.
// https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#example-of-enabling-virtual-terminal-processing
var requested_console_modes: windows.DWORD = windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING | windows.DISABLE_NEWLINE_AUTO_RETURN;
var console_mode = original_console_mode | requested_console_modes;
if (windows.kernel32.SetConsoleMode(self.handle, console_mode) != 0) return true;

// An application receiving ERROR_INVALID_PARAMETER with one of the newer console mode flags in the bit field should gracefully degrade behavior and try again.
requested_console_modes = windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING;
console_mode = original_console_mode | requested_console_modes;
if (windows.kernel32.SetConsoleMode(self.handle, console_mode) != 0) return true;
}

return posix.isCygwinPty(self.handle);
Expand Down

0 comments on commit 7f875f1

Please sign in to comment.