Skip to content

Commit 600ea4e

Browse files
committed
process: extract is_a_tty value
1 parent a17be9b commit 600ea4e

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

src/process/terminal_source.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,41 +33,36 @@ pub struct ColorableTerminal {
3333

3434
impl ColorableTerminal {
3535
pub(super) fn stdout(process: &Process) -> Self {
36-
Self::new(StreamSelector::Stdout, process)
36+
let is_a_tty = match process {
37+
Process::OsProcess(p) => p.stdout_is_a_tty,
38+
#[cfg(feature = "test")]
39+
Process::TestProcess(_) => unreachable!(),
40+
};
41+
42+
Self::new(StreamSelector::Stdout, is_a_tty, process)
3743
}
3844

3945
pub(super) fn stderr(process: &Process) -> Self {
40-
Self::new(StreamSelector::Stderr, process)
46+
let is_a_tty = match process {
47+
Process::OsProcess(p) => p.stderr_is_a_tty,
48+
#[cfg(feature = "test")]
49+
Process::TestProcess(_) => unreachable!(),
50+
};
51+
52+
Self::new(StreamSelector::Stderr, is_a_tty, process)
4153
}
4254

4355
#[cfg(feature = "test")]
4456
pub(super) fn test(writer: TestWriter, process: &Process) -> Self {
45-
Self::new(StreamSelector::TestWriter(writer), process)
57+
Self::new(StreamSelector::TestWriter(writer), false, process)
4658
}
4759

4860
/// A terminal that supports colorisation of a stream.
4961
/// If `RUSTUP_TERM_COLOR` is set to `always`, or if the stream is a tty and
5062
/// `RUSTUP_TERM_COLOR` either unset or set to `auto`,
5163
/// then color commands will be sent to the stream.
5264
/// Otherwise color commands are discarded.
53-
fn new(stream: StreamSelector, process: &Process) -> Self {
54-
let is_a_tty = match stream {
55-
StreamSelector::Stdout => match process {
56-
Process::OsProcess(p) => p.stdout_is_a_tty,
57-
#[cfg(feature = "test")]
58-
Process::TestProcess(_) => unreachable!(),
59-
},
60-
StreamSelector::Stderr => match process {
61-
Process::OsProcess(p) => p.stderr_is_a_tty,
62-
#[cfg(feature = "test")]
63-
Process::TestProcess(_) => unreachable!(),
64-
},
65-
#[cfg(feature = "test")]
66-
StreamSelector::TestWriter(_) => false,
67-
#[cfg(all(test, feature = "test"))]
68-
StreamSelector::TestTtyWriter(_) => true,
69-
};
70-
65+
fn new(stream: StreamSelector, is_a_tty: bool, process: &Process) -> Self {
7166
let choice = match process.var("RUSTUP_TERM_COLOR") {
7267
Ok(s) if s.eq_ignore_ascii_case("always") => ColorChoice::Always,
7368
Ok(s) if s.eq_ignore_ascii_case("never") => ColorChoice::Never,
@@ -330,12 +325,17 @@ mod tests {
330325

331326
#[test]
332327
fn term_color_choice() {
333-
fn assert_color_choice(env_val: &str, stream: StreamSelector, color_choice: ColorChoice) {
328+
fn assert_color_choice(
329+
env_val: &str,
330+
stream: StreamSelector,
331+
is_a_tty: bool,
332+
color_choice: ColorChoice,
333+
) {
334334
let mut vars = HashMap::new();
335335
vars.env("RUSTUP_TERM_COLOR", env_val);
336336
let tp = TestProcess::with_vars(vars);
337337

338-
let term = ColorableTerminal::new(stream, &tp.process);
338+
let term = ColorableTerminal::new(stream, is_a_tty, &tp.process);
339339
let inner = term.inner.lock().unwrap();
340340
assert!(matches!(
341341
&*inner,
@@ -346,23 +346,27 @@ mod tests {
346346
assert_color_choice(
347347
"aLWayS",
348348
StreamSelector::TestWriter(Default::default()),
349+
false,
349350
ColorChoice::Always,
350351
);
351352
assert_color_choice(
352353
"neVer",
353354
StreamSelector::TestWriter(Default::default()),
355+
false,
354356
ColorChoice::Never,
355357
);
356358
// tty + `auto` enables the colors.
357359
assert_color_choice(
358360
"AutO",
359361
StreamSelector::TestTtyWriter(Default::default()),
362+
true,
360363
ColorChoice::Auto,
361364
);
362365
// non-tty + `auto` does not enable the colors.
363366
assert_color_choice(
364367
"aUTo",
365368
StreamSelector::TestWriter(Default::default()),
369+
false,
366370
ColorChoice::Never,
367371
);
368372
}

0 commit comments

Comments
 (0)