Skip to content

Commit dc4921e

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

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

doc/src/environment-variables.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
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`) Controls whether colored output is used in the terminal.
33+
Set to `auto` to use colors only in tty streams, to `always` to always enable colors,
34+
or to `never` to disable colors.
35+
3236
- `RUSTUP_UNPACK_RAM` *unstable* (default free memory or 500MiB if unable to tell, min 210MiB) Caps the amount of
3337
RAM `rustup` will use for IO tasks while unpacking.
3438

src/currentprocess/terminalsource.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use termcolor::{ColorChoice, ColorSpec, StandardStream, StandardStreamLock, Writ
1111

1212
#[cfg(feature = "test")]
1313
use super::filesource::{TestWriter, TestWriterLock};
14-
use super::process;
14+
use super::{process, varsource::VarSource};
1515

1616
/// Select what stream to make a terminal on
1717
pub(super) enum StreamSelector {
@@ -73,14 +73,18 @@ enum TerminalInnerLocked {
7373
}
7474

7575
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.
76+
/// A terminal that supports colorisation of a stream.
77+
/// If `RUSTUP_TERM_COLOR` is set to `always`, or if the stream is a tty and
78+
/// `RUSTUP_TERM_COLOR` either unset or set to `auto`,
79+
/// then color commands will be sent to the stream.
80+
/// Otherwise color commands are discarded.
7881
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
82+
let env_override = process().var("RUSTUP_TERM_COLOR");
83+
let choice = match env_override.as_deref() {
84+
Ok("always") => ColorChoice::Always,
85+
Ok("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)