Skip to content

Commit b32aa99

Browse files
squeek502andrewrk
authored andcommitted
File.getOrEnableAnsiEscapeSupport: Do not attempt to set DISABLE_NEWLINE_AUTO_RETURN
Follow up to #20172. Fixes #20188
1 parent 9fe0c1d commit b32aa99

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

lib/std/fs/File.zig

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,17 @@ pub fn getOrEnableAnsiEscapeSupport(self: File) bool {
264264

265265
// For Windows Console, VT Sequences processing support was added in Windows 10 build 14361, but disabled by default.
266266
// https://devblogs.microsoft.com/commandline/tmux-support-arrives-for-bash-on-ubuntu-on-windows/
267-
// Use Microsoft's recommended way to enable virtual terminal processing.
267+
//
268+
// Note: In Microsoft's example for enabling virtual terminal processing, it
269+
// shows attempting to enable `DISABLE_NEWLINE_AUTO_RETURN` as well:
268270
// https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#example-of-enabling-virtual-terminal-processing
269-
var requested_console_modes: windows.DWORD = windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING | windows.DISABLE_NEWLINE_AUTO_RETURN;
270-
var console_mode = original_console_mode | requested_console_modes;
271-
if (windows.kernel32.SetConsoleMode(self.handle, console_mode) != 0) return true;
272-
273-
// An application receiving ERROR_INVALID_PARAMETER with one of the newer console mode
274-
// flags in the bit field should gracefully degrade behavior and try again.
275-
requested_console_modes = windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING;
276-
console_mode = original_console_mode | requested_console_modes;
271+
// This is avoided because in the old Windows Console, that flag causes \n (as opposed to \r\n)
272+
// to behave unexpectedly (the cursor moves down 1 row but remains on the same column).
273+
// Additionally, the default console mode in Windows Terminal does not have
274+
// `DISABLE_NEWLINE_AUTO_RETURN` set, so by only enabling `ENABLE_VIRTUAL_TERMINAL_PROCESSING`
275+
// we end up matching the mode of Windows Terminal.
276+
const requested_console_modes = windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING;
277+
const console_mode = original_console_mode | requested_console_modes;
277278
if (windows.kernel32.SetConsoleMode(self.handle, console_mode) != 0) return true;
278279
}
279280

0 commit comments

Comments
 (0)