Skip to content

Commit e26ba9e

Browse files
committed
Support RUSTUP_TERM_COLOR as an override environment variable
1 parent c8e9284 commit e26ba9e

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

doc/src/environment-variables.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
determines the directory that traces will be written too. Traces are of the
3030
form PID.trace. Traces can be read by the Catapult project [tracing viewer].
3131

32+
- `RUSTUP_TERM_COLOR` (default: `auto` in TTY, `never` otherwise) Controls whether colored output is used in the terminal. Possible values:
33+
34+
- `auto`: Automatically detect if color support is available on the terminal.
35+
- `always`: Always display colors.
36+
- `never`: Never display colors.
37+
3238
- `RUSTUP_UNPACK_RAM` *unstable* (default free memory or 500MiB if unable to tell, min 210MiB) Caps the amount of
3339
RAM `rustup` will use for IO tasks while unpacking.
3440

src/currentprocess/terminalsource.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ use std::{
99
pub(crate) use termcolor::Color;
1010
use termcolor::{ColorChoice, ColorSpec, StandardStream, StandardStreamLock, WriteColor};
1111

12+
use super::{process, varsource::VarSource};
13+
1214
#[cfg(feature = "test")]
1315
use super::filesource::{TestWriter, TestWriterLock};
14-
use super::process;
1516

1617
/// Select what stream to make a terminal on
1718
pub(super) enum StreamSelector {
@@ -73,14 +74,17 @@ enum TerminalInnerLocked {
7374
}
7475

7576
impl ColorableTerminal {
76-
/// Construct a terminal for the selected stream. Colors written to the
77-
/// terminal will be discarded if the stream is not a tty.
77+
/// Construct a terminal for the selected stream.
78+
/// Colors written to the terminal will be discarded if the stream is not a tty,
79+
/// unless the `RUSTUP_TERM_COLOR` override environment variable is set.
7880
pub(super) fn new(stream: StreamSelector) -> Self {
79-
let is_a_tty = stream.is_a_tty();
80-
let choice = if is_a_tty {
81-
ColorChoice::Auto
82-
} else {
83-
ColorChoice::Never
81+
let env_override = process().var("RUSTUP_TERM_COLOR").ok();
82+
let choice = match env_override.as_deref() {
83+
Some("always") => ColorChoice::Always,
84+
Some("auto") => ColorChoice::Auto,
85+
Some("never") => ColorChoice::Never,
86+
_ if stream.is_a_tty() => ColorChoice::Auto,
87+
_ => ColorChoice::Never,
8488
};
8589
let inner = match stream {
8690
StreamSelector::Stdout => {

0 commit comments

Comments
 (0)