-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Conversation
|
I'm not sure I understand the CI failure - it looks like it's something to do with docgen. The error says it's a problem with wasi not supporting |
|
Or is it something specifically to do with bootstrapping? It looks like |
a351e69 to
9cb66f0
Compare
9cb66f0 to
23716d9
Compare
|
I pushed a commit with my opinion of how it should be. What do you think? |
c3d82d8 to
c651f5d
Compare
dweiller
left a comment
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.
This seems like a nicer way to go. I'm unsure what the ZIG_DEBUG_COLOR environment variable was for - is there somewhere else in the codebase that was using it?
| if (builtin.os.tag == .wasi) { | ||
| // Per https://github.com/WebAssembly/WASI/issues/162 ANSI codes | ||
| // aren't currently supported. | ||
| 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_codes but I think the least surprising behaviour is for detectConfigForce to 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 call detectConfig*.
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.
| 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; |
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 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 .no_color unconditionally.
It was moved from std.debug in #15806. |
|
Looks like |
c651f5d to
9b3909f
Compare
|
ZIG_DEBUG_COLOR was renamed to YES_COLOR |
|
whoops |
9b3909f to
b391ac0
Compare
andrewrk
left a comment
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.
Alright, I have it figured out. Ripgrep's --color <when> option is bad.
"always" should not be an option, because it is ambiguous.
The proper CLI options to accept are:
- always off (
no_color) - ansi (
escape_codes) - auto/default
As for std.io.tty.detectConfig, there should not be a "force" variant of the function. It should be how it was before, with only 1 function but with the logic cleaned up as I have done in the commit here.
Then your CLI application logic will look almost exactly how Zig's CLI logic already looks:
Lines 6023 to 6027 in 128fd7d
| return switch (color) { | |
| .auto => std.io.tty.detectConfig(std.io.getStdErr()), | |
| .on => .escape_codes, | |
| .off => .no_color, | |
| }; |
|
I'll be happy with that. You won't be able to force the windows specific way of colouring, but I don't know if that's something you actually ever want to do. |
b391ac0 to
e45d24c
Compare
This PR adds
fn std.io.tty.detectConfigForce(File, bool) Configwhich accepts an extra parameter to force coloring if the platform supports it. There are two code paths where.no_colorwill be returned: if the platform is wasi, or on windows if thewindows.kernel32.GetConsoleScreenBufferInfo()test fails - other options in this case could be to return an error, or.escape_codes(though this might be unexpected). If you want to force escape codes on wasi for some reason, you don't need to do TTY detection, just use.escape_codesdirectly.