Skip to content

Commit d65af24

Browse files
committed
process: extract color_choice() method
1 parent 600ea4e commit d65af24

File tree

2 files changed

+41
-72
lines changed

2 files changed

+41
-72
lines changed

src/process.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::{
1414
};
1515
use std::{env, thread};
1616

17+
use anstream::ColorChoice;
1718
use anyhow::{Context, Result, bail};
1819
use indicatif::ProgressDrawTarget;
1920
#[cfg(feature = "test")]
@@ -192,6 +193,15 @@ impl Process {
192193
}
193194
}
194195

196+
fn color_choice(&self, is_a_tty: bool) -> ColorChoice {
197+
match self.var("RUSTUP_TERM_COLOR") {
198+
Ok(s) if s.eq_ignore_ascii_case("always") => ColorChoice::Always,
199+
Ok(s) if s.eq_ignore_ascii_case("never") => ColorChoice::Never,
200+
_ if is_a_tty => ColorChoice::Auto,
201+
_ => ColorChoice::Never,
202+
}
203+
}
204+
195205
pub fn concurrent_downloads(&self) -> Option<usize> {
196206
let s = self.var("RUSTUP_CONCURRENT_DOWNLOADS").ok()?;
197207
Some(NonZero::from_str(&s).ok()?.get())
@@ -333,3 +343,29 @@ pub struct TestContext {
333343
stdout: file_source::TestWriterInner,
334344
stderr: file_source::TestWriterInner,
335345
}
346+
347+
#[cfg(test)]
348+
mod tests {
349+
use std::collections::HashMap;
350+
351+
use super::*;
352+
use crate::process::TestProcess;
353+
use crate::test::Env;
354+
355+
#[test]
356+
fn term_color_choice() {
357+
fn assert_color_choice(env_val: &str, is_a_tty: bool, color_choice: ColorChoice) {
358+
let mut vars = HashMap::new();
359+
vars.env("RUSTUP_TERM_COLOR", env_val);
360+
let tp = TestProcess::with_vars(vars);
361+
assert_eq!(tp.process.color_choice(is_a_tty), color_choice);
362+
}
363+
364+
assert_color_choice("aLWayS", false, ColorChoice::Always);
365+
assert_color_choice("neVer", false, ColorChoice::Never);
366+
// tty + `auto` enables the colors.
367+
assert_color_choice("AutO", true, ColorChoice::Auto);
368+
// non-tty + `auto` does not enable the colors.
369+
assert_color_choice("aUTo", false, ColorChoice::Never);
370+
}
371+
}

src/process/terminal_source.rs

Lines changed: 5 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,12 @@ impl ColorableTerminal {
6363
/// then color commands will be sent to the stream.
6464
/// Otherwise color commands are discarded.
6565
fn new(stream: StreamSelector, is_a_tty: bool, process: &Process) -> Self {
66-
let choice = match process.var("RUSTUP_TERM_COLOR") {
67-
Ok(s) if s.eq_ignore_ascii_case("always") => ColorChoice::Always,
68-
Ok(s) if s.eq_ignore_ascii_case("never") => ColorChoice::Never,
69-
_ if is_a_tty => ColorChoice::Auto,
70-
_ => ColorChoice::Never,
71-
};
66+
let choice = process.color_choice(is_a_tty);
7267
let inner = match stream {
7368
StreamSelector::Stdout => TerminalInner::Stdout(AutoStream::new(io::stdout(), choice)),
7469
StreamSelector::Stderr => TerminalInner::Stderr(AutoStream::new(io::stderr(), choice)),
7570
#[cfg(feature = "test")]
76-
StreamSelector::TestWriter(w) => TerminalInner::TestWriter(w, choice),
77-
#[cfg(all(test, feature = "test"))]
78-
StreamSelector::TestTtyWriter(w) => TerminalInner::TestWriter(w, choice),
71+
StreamSelector::TestWriter(w) => TerminalInner::TestWriter(w),
7972
};
8073
let width = process
8174
.var("RUSTUP_TERM_WIDTH")
@@ -114,7 +107,7 @@ impl ColorableTerminal {
114107
TerminalInnerLocked::Stderr(AutoStream::new(locked, self.color_choice))
115108
}
116109
#[cfg(feature = "test")]
117-
TerminalInner::TestWriter(w, _) => {
110+
TerminalInner::TestWriter(w) => {
118111
TerminalInnerLocked::TestWriter(StripStream::new(Box::new(w.clone())))
119112
}
120113
});
@@ -290,8 +283,7 @@ enum TerminalInner {
290283
Stdout(AutoStream<io::Stdout>),
291284
Stderr(AutoStream<io::Stderr>),
292285
#[cfg(feature = "test")]
293-
#[allow(dead_code)] // ColorChoice only read in test code
294-
TestWriter(TestWriter, ColorChoice),
286+
TestWriter(TestWriter),
295287
}
296288

297289
impl TerminalInner {
@@ -300,7 +292,7 @@ impl TerminalInner {
300292
TerminalInner::Stdout(s) => s,
301293
TerminalInner::Stderr(s) => s,
302294
#[cfg(feature = "test")]
303-
TerminalInner::TestWriter(w, _) => w,
295+
TerminalInner::TestWriter(w) => w,
304296
}
305297
}
306298
}
@@ -311,63 +303,4 @@ pub(super) enum StreamSelector {
311303
Stderr,
312304
#[cfg(feature = "test")]
313305
TestWriter(TestWriter),
314-
#[cfg(all(test, feature = "test"))]
315-
TestTtyWriter(TestWriter),
316-
}
317-
318-
#[cfg(test)]
319-
mod tests {
320-
use std::collections::HashMap;
321-
322-
use super::*;
323-
use crate::process::TestProcess;
324-
use crate::test::Env;
325-
326-
#[test]
327-
fn term_color_choice() {
328-
fn assert_color_choice(
329-
env_val: &str,
330-
stream: StreamSelector,
331-
is_a_tty: bool,
332-
color_choice: ColorChoice,
333-
) {
334-
let mut vars = HashMap::new();
335-
vars.env("RUSTUP_TERM_COLOR", env_val);
336-
let tp = TestProcess::with_vars(vars);
337-
338-
let term = ColorableTerminal::new(stream, is_a_tty, &tp.process);
339-
let inner = term.inner.lock().unwrap();
340-
assert!(matches!(
341-
&*inner,
342-
&TerminalInner::TestWriter(_, choice) if choice == color_choice
343-
));
344-
}
345-
346-
assert_color_choice(
347-
"aLWayS",
348-
StreamSelector::TestWriter(Default::default()),
349-
false,
350-
ColorChoice::Always,
351-
);
352-
assert_color_choice(
353-
"neVer",
354-
StreamSelector::TestWriter(Default::default()),
355-
false,
356-
ColorChoice::Never,
357-
);
358-
// tty + `auto` enables the colors.
359-
assert_color_choice(
360-
"AutO",
361-
StreamSelector::TestTtyWriter(Default::default()),
362-
true,
363-
ColorChoice::Auto,
364-
);
365-
// non-tty + `auto` does not enable the colors.
366-
assert_color_choice(
367-
"aUTo",
368-
StreamSelector::TestWriter(Default::default()),
369-
false,
370-
ColorChoice::Never,
371-
);
372-
}
373306
}

0 commit comments

Comments
 (0)