zig icon indicating copy to clipboard operation
zig copied to clipboard

std.io.tty: add detectTtyConfigForce for forcing color

Open dweiller opened this issue 2 years ago • 2 comments

This PR adds fn std.io.tty.detectConfigForce(File, bool) Config which accepts an extra parameter to force coloring if the platform supports it. There are two code paths where .no_color will be returned: if the platform is wasi, or on windows if the windows.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_codes directly.

dweiller avatar Jun 20 '23 06:06 dweiller

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 hasEnvVarConstants, but that doesn't make sense to me as the changes should not have changed the behaviour of std.io.tty.detectConfig() at all.

dweiller avatar Jun 20 '23 08:06 dweiller

Or is it something specifically to do with bootstrapping? It looks like build-debug/zig2 is the executable that's being used during the failure.

dweiller avatar Jun 20 '23 08:06 dweiller

I pushed a commit with my opinion of how it should be. What do you think?

andrewrk avatar Jun 21 '23 21:06 andrewrk

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?

It was moved from std.debug in #15806.

andrewrk avatar Jun 21 '23 23:06 andrewrk

Looks like ZIG_DEBUG_COLOR is referenced in 3 others files:

~/S/z/src (master) > rg ZIG_DEBUG_COLOR
lib/build_runner.zig
285:        .escape_codes => try builder.env_map.put("ZIG_DEBUG_COLOR", "1"),

lib/std/io/tty.zig
16:    } else if (process.hasEnvVarConstant("ZIG_DEBUG_COLOR")) {


test/src/StackTrace.zig
84:    run.removeEnvironmentVariable("ZIG_DEBUG_COLOR");

doc/docgen.zig
1308:    try env_map.put("ZIG_DEBUG_COLOR", "1");

dweiller avatar Jun 22 '23 00:06 dweiller

ZIG_DEBUG_COLOR was renamed to YES_COLOR

andrewrk avatar Jun 22 '23 00:06 andrewrk

whoops

dweiller avatar Jun 22 '23 00:06 dweiller

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.

dweiller avatar Jun 22 '23 00:06 dweiller