-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Color IR output in cout and cerr. #8635
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
…ainbow) to make depth of Exprs clear.
|
Love this idea. Will take a while to review. |
|
I was looking into whether this is going to to work on Windows. I saw that you can enable Virtual Terminal processing, by doing: // Set output mode to handle virtual terminal sequences
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hOut, &dwMode)
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOut, dwMode)I have not added the code above right now, and rely on detecting color support through the |
On Posix-y systems at least, I think you need to check bool supports_ansi(std::ostream &os) {
if (&os == &std::cout) {
return isatty(fileno(stdout));
} else if (&os == &std::cerr) {
return isatty(fileno(stderr));
}
return false;
}This should be more robust than checking bool supports_ansi(std::ostream &os) {
HANDLE h;
if (&os == &std::cout) {
h = GetStdHandle(STD_OUTPUT_HANDLE);
} else if (&os == &std::cerr) {
h = GetStdHandle(STD_ERROR_HANDLE);
} else {
return false;
}
DWORD mode;
return GetConsoleMode(h, &mode) &&
SetConsoleMode(h, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
}Should be something like that. |
|
I also wonder whether the other ad-hoc |
I thought about alternatives, but decided for this, as this will be 100% correctly branch predicted. |
Sure, but performance doesn't really matter for the IRPrinter (it's all IO bound anyway) and I'm thinking about saving future headaches if we need to add another case. You could also template-ize the whole thing and have no branches at all. 🤷 |
|
Implemented your suggestion for the tabular constants, and added the Windows-specific code. Thanks for those! |
|
A few things to keep in mind: That said, this is catastrophizing to the next level! (but I have seen super weird stream rebindings in production over the years...) |
That looks great! |
In this case, the failure mode would be to not emit ANSI codes, which is always safe. I suppose
Of course, the real failure is of the C++ standards committee to make the |
… is used as undefined (auto-detect), 0 or 1.
|
Thanks @alexreinking and @slomp for the feedback. I implemented this:
I also validated that piping to a file does not produce colors. Overriding with |
There seems to be no way to get the file descriptor from an |
Yup, no standard way of doing so, and as you pointed, a stream object does not need to be backed by a file stream or (file-descriptor). It's one of the reasons I tend to steer away from C++ io abstractions. |
alexreinking
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 looks great. Excited to try it out.
|
Dang I switched to another branch to continue work there, and I'm missing this already badly 😝 |
|
Does anybody want to add something to this, or can we merge this? @steven-johnson @abadams ? |
|
I think it's safe to merge this. We can always revert if something goes wrong and both @slomp and I have taken a look. |
|
Btw, if you liked this, you might also wanna be aware of the neovim/vim syntax highlighting file I contributed to the Wiki: https://github.com/halide/Halide/wiki/Syntax-highlighting-for-Stmt-files |
📖 Overview
IRPrintersupports ANSI escape codes for coloring when printing tostd::coutorstd::cerr.isatty()passes for the output stream.HL_COLORS=0, and forcefully enabled withHL_COLORS=1.LetStmt,Store, andProvide, to have have reliable depth to color mapping on the Stmt level.👀 Preview
Also useful to simply print

Exprs for debugging: