Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.2k
std.io.tty: cleanup detectConfig#16110
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,29 +7,34 @@ const native_os = builtin.os.tag; | ||
| /// Detect suitable TTY configuration options for the given file (commonly stdout/stderr). | ||
| /// This includes feature checks for ANSI escape codes and the Windows console API, as well as | ||
| /// respecting the `NO_COLOR` environment variable. | ||
| /// respecting the `NO_COLOR` and `YES_COLOR` environment variables to override the default. | ||
| pub fn detectConfig(file: File) Config { | ||
| if (builtin.os.tag == .wasi) { | ||
| // Per https://github.com/WebAssembly/WASI/issues/162 ANSI codes | ||
| // aren't currently supported. | ||
| return .no_color; | ||
| } else if (process.hasEnvVarConstant("ZIG_DEBUG_COLOR")) { | ||
| return .escape_codes; | ||
| } else if (process.hasEnvVarConstant("NO_COLOR")) { | ||
| return .no_color; | ||
| } else if (file.supportsAnsiEscapeCodes()) { | ||
| return .escape_codes; | ||
| } else if (native_os == .windows and file.isTty()) { | ||
| const force_color: ?bool = if (builtin.os.tag == .wasi) | ||
| null // wasi does not support environment variables | ||
| else if (process.hasEnvVarConstant("NO_COLOR")) | ||
| false | ||
| else if (process.hasEnvVarConstant("YES_COLOR")) | ||
| true | ||
| else | ||
| null; | ||
| if (force_color == false) return .no_color; | ||
| if (native_os == .windows and file.isTty()) { | ||
| var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined; | ||
| if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE) { | ||
| // TODO: Should this return an error instead? | ||
| return .no_color; | ||
| return if (force_color == true) .escape_codes else .no_color; | ||
Comment on lines
24
to
+26
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't actually sure how windows works with colour so I left the previous behaviour. If windows would support escape codes here this seems fine, if not similar to WASI I'd suggest returning | ||
| } | ||
| return .{ .windows_api = .{ | ||
| .handle = file.handle, | ||
| .reset_attributes = info.wAttributes, | ||
| } }; | ||
| } | ||
| if (force_color == true or file.supportsAnsiEscapeCodes()) { | ||
| return .escape_codes; | ||
| } | ||
| return .no_color; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally had this behaviour (forcing colour on WASI returning
.escape_codesbut I think the least surprising behaviour is fordetectConfigForceto not output escapes codes on platforms that don't support them. If someone actually wants them in an environment that doesn't support them they should just not calldetectConfig*.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When color is forced on, it is irrelevant whether a platform supports it or not. The intention of forcing it on is that the output stream will be interpreted to have escape codes in them, and handled appropriately. This can be done on WASI, or Windows, or any platform.