Skip to content
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

Add File.getOrEnableAnsiEscapeSupport and use it #20172

Merged
merged 2 commits into from
Jun 3, 2024

Conversation

squeek502
Copy link
Collaborator

@squeek502 squeek502 commented Jun 2, 2024

On Windows, the console mode flag ENABLE_VIRTUAL_TERMINAL_PROCESSING determines whether or not ANSI escape codes are parsed/acted on. On the newer Windows Terminal, this flag is set by default, but on the older Windows Console, it is not set by default, but can be enabled (since Windows 10 RS1 from June 2016).

The new File.getOrEnableAnsiEscapeSupport function will get the current status of ANSI escape code support, but will also attempt to enable ENABLE_VIRTUAL_TERMINAL_PROCESSING on Windows if necessary which will provide better/more consistent results for things like std.Progress and std.io.tty.

This type of change was not done previously due to a mistaken assumption (on my part) that the console mode would persist after the run of a program. However, it turns out that the console mode is always reset to the default for each program run in a console session.


EDIT: A caveat here is that child processes can affect the console mode of parent processes, see #16526 (comment) for an example of how this can cause problems. So this may not actually be fully safe to do, as spawning Zig as a child process can end up messing with the console mode of other processes unexpectedly.


This is a revivial of #18692 / #15206 (the first commit is cherry-picked from #18692).

Notes:

  • See lib/std/fs/File: enable VT seq support for Windows Console #18692 for a nice writeup about ENABLE_VIRTUAL_TERMINAL_PROCESSING
  • Console modes are per-screen-buffer, meaning if stderr and stdout share the same screen buffer (usually the case AFAIK), then setting ENABLE_VIRTUAL_TERMINAL_PROCESSING for one will set it for both
  • This could allow for dropping support for the Windows console API in std.Progress/std.io.tty, but that is not done in this PR (see rework std.Progress #20059 (comment)). Doing so would mean that color support/progress support would require Windows 10 >= RS1 (from June 2016)

Garfield550 and others added 2 commits June 2, 2024 15:37
* 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>
On Windows, the console mode flag `ENABLE_VIRTUAL_TERMINAL_PROCESSING` determines whether or not ANSI escape codes are parsed/acted on. On the newer Windows Terminal, this flag is set by default, but on the older Windows Console, it is not set by default, but *can* be enabled (since Windows 10 RS1 from June 2016).

The new `File.getOrEnableAnsiEscapeSupport` function will get the current status of ANSI escape code support, but will also attempt to enable `ENABLE_VIRTUAL_TERMINAL_PROCESSING` on Windows if necessary which will provide better/more consistent results for things like `std.Progress` and `std.io.tty`.

This type of change was not done previously due to a mistaken assumption (on my part) that the console mode would persist after the run of a program. However, it turns out that the console mode is always reset to the default for each program run in a console session.
@andrewrk andrewrk merged commit 7fc3fb9 into ziglang:master Jun 3, 2024
10 checks passed
@andrewrk
Copy link
Member

andrewrk commented Jun 3, 2024

Thanks!

squeek502 added a commit to squeek502/zig that referenced this pull request Jun 5, 2024
andrewrk pushed a commit that referenced this pull request Jun 5, 2024
ryoppippi pushed a commit to ryoppippi/zig that referenced this pull request Jul 5, 2024
SammyJames pushed a commit to SammyJames/zig that referenced this pull request Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants