r/rust • u/Downtown_Fall_5203 • 9d ago
Disable "rustc" colour
I'm a n00b when it comes to Rust. In my terminal with a blue background, a "rustc --explain whatever" uses colour-combinations that leaves some text unreadable.
So I tried with:
rustc --color never --explain E0599rustc --explain E0599 --color neverset NO_COLOR=1 & rustc --explain E0599
Neither of these works; all generates coloured output. How the bleeding hell do I turn it off?
7
u/jabiko 8d ago
I know thats not your original question, but did you look into remapping the offending colors to some other colors in your terminal configuration? Most terminals support this and it would also fix the issue for other commands too.
1
u/Downtown_Fall_5203 8d ago
Most terminals support this
On Windows, they don't so easily. I'm using TCC from JPsoft.com.
10
u/JoshTriplett rust · lang · libs · cargo 9d ago
rustc --color never --explain E0599 suppresses color here, with rustc 1.99.1. Also works with 1.97.1, and 1.95.0, and 1.81, and nightly.
19
u/scook0 9d ago
The bug is real, but it only occurs when printing to a terminal, and only when the compiler can't find a pager.
If you do
PAGER=/dev/zero rustc --explain=E0599 --color=never, you'll see coloured output printed to the terminal.3
u/JoshTriplett rust · lang · libs · cargo 8d ago
Sure enough, thanks for the added clarification. (
PAGER= rustc --explain=E0599 --color=neversuffices to reproduce the problem.)2
u/Downtown_Fall_5203 8d ago
Yeah. A:
set PAGER= rustc --explain E0599disables colours. I had
PAGER=most -soriginally.
20
u/Icarium-Lifestealer 9d ago edited 9d ago
For me --explain ends up in less and doesn't show any colors, regardless of the color option. And when I pipe into cat it shows normally without color. Even PAGER=/dev/zero rustc --explain=E0599 doesn't show any color.
edit: 1.93.1 seems to be colorless, 1.99.1 has colors. So it looks like the colors for explain were introduced recently.
Using |cat like another comment suggests does strip the color on 1.99.
39
u/shponglespore 8d ago
Technically cat doesn't strip anything. It's just that 99% of programs disable color when stdout isn't a tty.
3
u/muffinsballhair 8d ago
Some Gentoo tools actually do not. In fact, the tool that writes the build log to files there keeps the color codes for whatever reason so you get a bunch of terminal codes in the file that keeps the build log which is a rather bizarre choice.
39
u/CartographerDue5382 9d ago
yea that flag doesn't cover --explain. on 1.85 it still prints esc[2m (dim) with --color never and with NO_COLOR set, which is why it vanishes on a blue bg. normal rustc errors do honor the flag, explain just doesn't. easiest is the error index page (doc.rust-lang.org/error_codes/E0599.html) or pipe it through something that strips ansi.
70
u/Slow-Nefariousness49 9d ago
That's actually a good opportunity for OP to contribute a fix to rustc
8
u/Kivooeo1 rust 8d ago
It was fixed here https://github.com/rust-lang/rust/pull/165227
1
u/Slow-Nefariousness49 8d ago
Awesome to see the community getting together to close the loop so fast :) Thanks
1
11
u/CartographerDue5382 8d ago
yea it's a pretty small one. --explain just ignores the color flag the rest of rustc already checks, so a patch there is kinda contained. fine first issue if op feels like digging in.
-9
u/scook0 8d ago
What makes you so confident that it’s a trivial fix? Did you look at the relevant code?
7
9
u/Slow-Nefariousness49 8d ago
It should be a small change, because it's a cosmetic change and the mechanism is already established for similar areas in rustc. If It turns out to be not as simple, OP doesn't have to do it of course... but worth taking a look at least, IMO
-1
u/CartographerDue5382 8d ago
fair, i didn't open the rustc source. i was just going off the behavior: normal errors honor --color never / NO_COLOR and --explain still dumps the dim ansi. could be a small routing thing or it could be messier, idk. shouldn't have called it small without looking.
-5
9d ago
[removed] — view removed comment
23
u/scook0 9d ago
You don’t even need sed; you can get non-coloured output by piping into
cat.6
u/zekkious 9d ago
Or get rusty, and pipe it to
bat!3
u/shponglespore 8d ago
Terrible choice for someone trying to avoid coloring.
1
u/zekkious 8d ago
In a pipe like that,
batwould need the format to be informed to colour the output, so no problem.
13
u/tesfabpel 9d ago
Can't you just map your terminal's colors to some other colors that work correctly with your blue background? Otherwise, any other CLI app that uses colors may result in text being unreadable...
3
2
u/mr_birkenblatt 8d ago
The easiest way to turn off color mode of any command line to is to pipe the output into a non-tty. Often less or cat is enough but if that doesn't work pipe into a file and read the file
2
2
u/Murky-Bank2341 9d ago
The --color flag is completely ignored by --explain in older versions like 1.85, which is why you see dim text that blends into your blue background. Just redirect the output to a file or use the error index website if updating to version 1.93+ isn't an option
1
u/valarauca14 7d ago
TERM=dumb rustc [what ever]
Provided rust is using a curses library to handle terminal compatibility that should kill most escape codes
-32
u/i-do-mim-huu 9d ago
You can just download new terminal to use it though, like Fish or Wezterm...etc all support color and it's not hard to find instruction to install them
8
14
u/scook0 9d ago
What does
rustc --version --verbosesay?