fix: detect mintty/MSYS2 terminals for colored output#3736
Conversation
7a0f099 to
a4a09d4
Compare
|
Thanks for your contribution. Doesn't this mean that when using mintty style terminals, it will always be treated as interactive mode? That seems undesirable...What if the user has aliased |
a4a09d4 to
af5ee70
Compare
|
Good catch @keith-hall. You're right, the original version would have forced interactive mode globally, which would break piping ( I did a quick update to the approach: the mintty detection now only affects the The key change:
This means mintty users get colored output when viewing files directly, but piped output stays plain. I'm newer to Rust, so thanks for the call-out, it helps my learning 👍 |
|
Did you report it also in https://github.com/rust-lang/rust/issues? I believe it should be handled in Rust's std. |
|
Code review (macOS, non-Windows): the split between CI is green. Would be great to get a Windows/mintty maintainer pass before merge. Thanks @Metbcy! |
Summary
Fixes #3034.
On MinGW/MSYS2 terminals (mintty),
std::io::stdout().is_terminal()returnsfalsebecause mintty uses POSIX PTYs that Windows'sisatty()does not recognize. This causedbat --help(and regular file display) to lose colored output starting with v0.23.0, when help rendering moved from clap's built-in output to bat's own syntax-highlighting pipeline gated oninteractive_output.Changes
Adds a fallback terminal detection check for mintty/MSYS2 environments on Windows by looking for:
TERM_PROGRAM=mintty(set by mintty itself)MSYSTEMenv var (set by all MSYS2 shells: MINGW64, UCRT64, CLANG64, etc.)When either is present and
is_terminal()returnsfalse, the output is still treated as interactive.The check is
#[cfg(windows)]gated and compiles to a no-op on other platforms.Testing
Verified all existing tests pass (232 passed, 0 failed).
To manually test on a MinGW/MSYS2 terminal:
bat --helpand confirm colored output is shownbat --color=never --helpand confirm plain outputbat somefile.rsand confirm syntax highlighting works