Skip to content

fix: detect mintty/MSYS2 terminals for colored output#3736

Open
Metbcy wants to merge 2 commits into
sharkdp:masterfrom
Metbcy:fix/help-color-detection
Open

fix: detect mintty/MSYS2 terminals for colored output#3736
Metbcy wants to merge 2 commits into
sharkdp:masterfrom
Metbcy:fix/help-color-detection

Conversation

@Metbcy

@Metbcy Metbcy commented May 9, 2026

Copy link
Copy Markdown

Summary

Fixes #3034.

On MinGW/MSYS2 terminals (mintty), std::io::stdout().is_terminal() returns false because mintty uses POSIX PTYs that Windows's isatty() does not recognize. This caused bat --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 on interactive_output.

Changes

Adds a fallback terminal detection check for mintty/MSYS2 environments on Windows by looking for:

  • TERM_PROGRAM=mintty (set by mintty itself)
  • MSYSTEM env var (set by all MSYS2 shells: MINGW64, UCRT64, CLANG64, etc.)

When either is present and is_terminal() returns false, 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:

  1. Open mintty (e.g. Git Bash or MSYS2 shell)
  2. Run bat --help and confirm colored output is shown
  3. Run bat --color=never --help and confirm plain output
  4. Run bat somefile.rs and confirm syntax highlighting works

@Metbcy Metbcy force-pushed the fix/help-color-detection branch from 7a0f099 to a4a09d4 Compare May 9, 2026 21:05
@keith-hall

Copy link
Copy Markdown
Collaborator

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 cat and is piping to another program? By default bat detects that and adds no decorations or colours etc.

@Metbcy Metbcy force-pushed the fix/help-color-detection branch from a4a09d4 to af5ee70 Compare May 9, 2026 21:15
@Metbcy

Metbcy commented May 9, 2026

Copy link
Copy Markdown
Author

Good catch @keith-hall. You're right, the original version would have forced interactive mode globally, which would break piping (bat file | grep foo would get unwanted colors/decorations).

I did a quick update to the approach: the mintty detection now only affects the --color=auto decision, not the interactive_output flag itself. So piping still works correctly (no decorations, no colors) while direct terminal use in mintty gets colored output as expected.

The key change:

  • interactive_output stays purely based on is_terminal()
  • use_color in auto mode uses interactive_output || is_mintty

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 👍

@tats-u

tats-u commented May 12, 2026

Copy link
Copy Markdown

Did you report it also in https://github.com/rust-lang/rust/issues? I believe it should be handled in Rust's std.

@leno23

leno23 commented May 16, 2026

Copy link
Copy Markdown

Code review (macOS, non-Windows): the split between interactive_output and use_color for mintty looks correct after @keith-hall's feedback — piping should stay non-interactive while --color=auto can still enable colors on mintty/MSYS2.

CI is green. Would be great to get a Windows/mintty maintainer pass before merge. Thanks @Metbcy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bat --help] not showing color since version 0.23.0-1

4 participants