Uh oh!
There was an error while loading. Please reload this page.
std.io.tty: cleanup detectConfig - #16110
Conversation
dweiller
commented
Jun 20, 2023
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 |
dweiller
commented
Jun 20, 2023
Or is it something specifically to do with bootstrapping? It looks like |
a351e69 to
9cb66f0Compareandrewrk
commented
Jun 21, 2023
I pushed a commit with my opinion of how it should be. What do you think? |
c3d82d8 to
c651f5dCompare
dweiller
left a comment
There was a problem hiding this comment.
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.
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.
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.
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.
andrewrk
commented
Jun 21, 2023
It was moved from std.debug in #15806. |
Looks like |
andrewrk
commented
Jun 22, 2023
ZIG_DEBUG_COLOR was renamed to YES_COLOR |
dweiller
commented
Jun 22, 2023
whoops |
andrewrk
left a comment
There was a problem hiding this comment.
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
dweiller
commented
Jun 22, 2023
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. |
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.