authorgravatar for Garfield550@users.noreply.github.comGarfield Lee <Garfield550@users.noreply.github.com> 2024-01-26 00:16:26+08:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2024-06-02 15:37:38-07:00
log17dc93934689235ac16b3354c3eb932420a88e85
treea47bd778f7e3b3fa807eb989558f29b3a53e5907
parentd74180c373274bc545992c2a96a8e7618f8bd52c

lib/std/fs/File: enable VT seq support for Windows Console

* 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>

1 files changed, 18 insertions(+), 3 deletions(-)

lib/std/fs/File.zig+18-3
...@@ -248,9 +248,24 @@ pub fn isCygwinPty(file: File) bool {...@@ -248,9 +248,24 @@ pub fn isCygwinPty(file: File) bool {
248/// Test whether ANSI escape codes will be treated as such.248/// Test whether ANSI escape codes will be treated as such.
249pub fn supportsAnsiEscapeCodes(self: File) bool {249pub fn supportsAnsiEscapeCodes(self: File) bool {
250 if (builtin.os.tag == .windows) {250 if (builtin.os.tag == .windows) {
251 var console_mode: windows.DWORD = 0;251 var original_console_mode: windows.DWORD = 0;
252 if (windows.kernel32.GetConsoleMode(self.handle, &console_mode) != 0) {252
253 if (console_mode & windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0) return true;253 // For Windows Terminal, VT Sequences processing is enabled by default.
254 if (windows.kernel32.GetConsoleMode(self.handle, &original_console_mode) != 0) {
255 if (original_console_mode & windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0) return true;
256
257 // For Windows Console, VT Sequences processing support was added in Windows 10 build 14361, but disabled by default.
258 // https://devblogs.microsoft.com/commandline/tmux-support-arrives-for-bash-on-ubuntu-on-windows/
259 // Use Microsoft's recommended way to enable virtual terminal processing.
260 // https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#example-of-enabling-virtual-terminal-processing
261 var requested_console_modes: windows.DWORD = windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING | windows.DISABLE_NEWLINE_AUTO_RETURN;
262 var console_mode = original_console_mode | requested_console_modes;
263 if (windows.kernel32.SetConsoleMode(self.handle, console_mode) != 0) return true;
264
265 // 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.
266 requested_console_modes = windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING;
267 console_mode = original_console_mode | requested_console_modes;
268 if (windows.kernel32.SetConsoleMode(self.handle, console_mode) != 0) return true;
254 }269 }
255270
256 return self.isCygwinPty();271 return self.isCygwinPty();