Skip to content

Commit 85f0df9

Browse files
epagerami3l
authored andcommitted
refactor: Simplify working with ColorableTerminal
1 parent 061cf78 commit 85f0df9

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/process/filesource.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::io::{self, BufRead, Read, Write};
22

3-
use super::terminalsource::{ColorableTerminal, StreamSelector};
3+
use super::terminalsource::ColorableTerminal;
44
use crate::process::Process;
55

66
/// Stand-in for std::io::Stdin
@@ -59,7 +59,7 @@ impl Writer for io::Stdout {
5959
}
6060

6161
fn terminal(&self, process: &Process) -> ColorableTerminal {
62-
ColorableTerminal::new(StreamSelector::Stdout, process)
62+
ColorableTerminal::stdout(process)
6363
}
6464
}
6565

@@ -79,7 +79,7 @@ impl Writer for io::Stderr {
7979
}
8080

8181
fn terminal(&self, process: &Process) -> ColorableTerminal {
82-
ColorableTerminal::new(StreamSelector::Stderr, process)
82+
ColorableTerminal::stderr(process)
8383
}
8484
}
8585

@@ -174,7 +174,7 @@ mod test_support {
174174
}
175175

176176
fn terminal(&self, process: &Process) -> ColorableTerminal {
177-
ColorableTerminal::new(StreamSelector::TestWriter(self.clone()), process)
177+
ColorableTerminal::test(self.clone(), process)
178178
}
179179
}
180180

src/process/terminalsource.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,25 @@ impl TerminalInnerLocked {
104104
}
105105

106106
impl ColorableTerminal {
107+
pub(super) fn stdout(process: &Process) -> Self {
108+
Self::new(StreamSelector::Stdout, process)
109+
}
110+
111+
pub(super) fn stderr(process: &Process) -> Self {
112+
Self::new(StreamSelector::Stderr, process)
113+
}
114+
115+
#[cfg(feature = "test")]
116+
pub(super) fn test(writer: TestWriter, process: &Process) -> Self {
117+
Self::new(StreamSelector::TestWriter(writer), process)
118+
}
119+
107120
/// A terminal that supports colorisation of a stream.
108121
/// If `RUSTUP_TERM_COLOR` is set to `always`, or if the stream is a tty and
109122
/// `RUSTUP_TERM_COLOR` either unset or set to `auto`,
110123
/// then color commands will be sent to the stream.
111124
/// Otherwise color commands are discarded.
112-
pub(super) fn new(stream: StreamSelector, process: &Process) -> Self {
125+
fn new(stream: StreamSelector, process: &Process) -> Self {
113126
let is_a_tty = stream.is_a_tty(process);
114127
let choice = match process.var("RUSTUP_TERM_COLOR") {
115128
Ok(s) if s.eq_ignore_ascii_case("always") => ColorChoice::Always,

0 commit comments

Comments
 (0)