Skip to content

Commit b1795f1

Browse files
epagerami3l
authored andcommitted
refactor: Directly use ColorableTerminal
In a lot of cases, `Process` will create a boxed `Writer` just then to create a `ColorableTerminal` from it. This bypasses some of those steps, simplifying the code.
1 parent 85f0df9 commit b1795f1

File tree

7 files changed

+27
-19
lines changed

7 files changed

+27
-19
lines changed

src/cli/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ fn show_channel_updates(
244244
Ok((pkg, banner, width, color, version, previous_version))
245245
});
246246

247-
let mut t = cfg.process.stdout().terminal(cfg.process);
247+
let mut t = cfg.process.stdout();
248248

249249
let data: Vec<_> = data.collect::<Result<_>>()?;
250250
let max_width = data
@@ -307,7 +307,7 @@ pub(super) fn list_items(
307307
quiet: bool,
308308
process: &Process,
309309
) -> Result<utils::ExitCode> {
310-
let mut t = process.stdout().terminal(process);
310+
let mut t = process.stdout();
311311
for (name, installed) in items {
312312
if installed && !installed_only && !quiet {
313313
t.attr(terminalsource::Attr::Bold)?;

src/cli/log.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ where
5454
Ok(s) if s.eq_ignore_ascii_case("never") => false,
5555
// `RUSTUP_TERM_COLOR` is prioritized over `NO_COLOR`.
5656
_ if process.var("NO_COLOR").is_ok() => false,
57-
_ => process.stderr().is_a_tty(process),
57+
_ => process.stderr().is_a_tty(),
5858
};
5959
let maybe_rustup_log_directives = process.var("RUSTUP_LOG");
6060
let process = process.clone();

src/cli/rustup_mode.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ async fn default_(
797797
}
798798

799799
async fn check_updates(cfg: &Cfg<'_>, opts: CheckOpts) -> Result<utils::ExitCode> {
800-
let t = cfg.process.stdout().terminal(cfg.process);
800+
let t = cfg.process.stdout();
801801
let use_colors = matches!(t.color_choice(), ColorChoice::Auto | ColorChoice::Always);
802802
let mut update_available = false;
803803
let channels = cfg.list_channels()?;
@@ -890,7 +890,7 @@ async fn check_updates(cfg: &Cfg<'_>, opts: CheckOpts) -> Result<utils::ExitCode
890890
.await
891891
};
892892

893-
let t = cfg.process.stdout().terminal(cfg.process);
893+
let t = cfg.process.stdout();
894894
for result in channels {
895895
let (update_a, message) = result?;
896896
if update_a {
@@ -1072,7 +1072,7 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
10721072

10731073
// Print host triple
10741074
{
1075-
let mut t = cfg.process.stdout().terminal(cfg.process);
1075+
let mut t = cfg.process.stdout();
10761076
t.attr(terminalsource::Attr::Bold)?;
10771077
write!(t.lock(), "Default host: ")?;
10781078
t.reset()?;
@@ -1081,7 +1081,7 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
10811081

10821082
// Print rustup home directory
10831083
{
1084-
let mut t = cfg.process.stdout().terminal(cfg.process);
1084+
let mut t = cfg.process.stdout();
10851085
t.attr(terminalsource::Attr::Bold)?;
10861086
write!(t.lock(), "rustup home: ")?;
10871087
t.reset()?;
@@ -1121,7 +1121,7 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
11211121

11221122
// show installed toolchains
11231123
{
1124-
let mut t = cfg.process.stdout().terminal(cfg.process);
1124+
let mut t = cfg.process.stdout();
11251125

11261126
print_header::<Error>(&mut t, "installed toolchains")?;
11271127

@@ -1157,7 +1157,7 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
11571157

11581158
// show active toolchain
11591159
{
1160-
let mut t = cfg.process.stdout().terminal(cfg.process);
1160+
let mut t = cfg.process.stdout();
11611161

11621162
writeln!(t.lock())?;
11631163

src/cli/self_update.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ pub(crate) async fn install(
537537
exit_code &= unix::do_anti_sudo_check(no_prompt, process)?;
538538
}
539539

540-
let mut term = process.stdout().terminal(process);
540+
let mut term = process.stdout();
541541

542542
#[cfg(windows)]
543543
windows::maybe_install_msvc(&mut term, no_prompt, quiet, &opts, process).await?;
@@ -991,7 +991,7 @@ pub(crate) fn uninstall(no_prompt: bool, process: &Process) -> Result<utils::Exi
991991
pre_uninstall_msg!(),
992992
cargo_home = canonical_cargo_home(process)?
993993
);
994-
md(&mut process.stdout().terminal(process), msg);
994+
md(&mut process.stdout(), msg);
995995
if !common::confirm("\nContinue? (y/N)", false, process)? {
996996
info!("aborting uninstallation");
997997
return Ok(utils::ExitCode(0));
@@ -1347,7 +1347,7 @@ impl fmt::Display for SchemaVersion {
13471347

13481348
/// Returns whether an update was available
13491349
pub(crate) async fn check_rustup_update(process: &Process) -> anyhow::Result<bool> {
1350-
let mut t = process.stdout().terminal(process);
1350+
let mut t = process.stdout();
13511351
// Get current rustup version
13521352
let current_version = env!("CARGO_PKG_VERSION");
13531353

src/cli/setup_mode.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ pub async fn main(
9797
} = match RustupInit::try_parse() {
9898
Ok(args) => args,
9999
Err(e) if [ErrorKind::DisplayHelp, ErrorKind::DisplayVersion].contains(&e.kind()) => {
100+
use std::io::Write as _;
100101
write!(process.stdout().lock(), "{e}")?;
101102
return Ok(utils::ExitCode(0));
102103
}

src/dist/component/package.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ fn unpack_without_first_dir<R: Read>(
456456
None => {
457457
// Tar has item before containing directory
458458
// Complain about this so we can see if these exist.
459+
use std::io::Write as _;
459460
writeln!(
460461
cx.process.stderr().lock(),
461462
"Unexpected: missing parent '{}' for '{}'",

src/process.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,25 @@ impl Process {
148148
}
149149
}
150150

151-
pub(crate) fn stdout(&self) -> Box<dyn filesource::Writer> {
151+
pub(crate) fn stdout(&self) -> terminalsource::ColorableTerminal {
152152
match self {
153-
Process::OsProcess(_) => Box::new(io::stdout()),
153+
Process::OsProcess(_) => terminalsource::ColorableTerminal::stdout(self),
154154
#[cfg(feature = "test")]
155-
Process::TestProcess(p) => Box::new(filesource::TestWriter(p.stdout.clone())),
155+
Process::TestProcess(p) => terminalsource::ColorableTerminal::test(
156+
filesource::TestWriter(p.stdout.clone()),
157+
self,
158+
),
156159
}
157160
}
158161

159-
pub(crate) fn stderr(&self) -> Box<dyn filesource::Writer> {
162+
pub(crate) fn stderr(&self) -> terminalsource::ColorableTerminal {
160163
match self {
161-
Process::OsProcess(_) => Box::new(io::stderr()),
164+
Process::OsProcess(_) => terminalsource::ColorableTerminal::stderr(self),
162165
#[cfg(feature = "test")]
163-
Process::TestProcess(p) => Box::new(filesource::TestWriter(p.stderr.clone())),
166+
Process::TestProcess(p) => terminalsource::ColorableTerminal::test(
167+
filesource::TestWriter(p.stderr.clone()),
168+
self,
169+
),
164170
}
165171
}
166172

@@ -178,7 +184,7 @@ impl Process {
178184
#[cfg(feature = "test")]
179185
Process::TestProcess(_) => return ProgressDrawTarget::hidden(),
180186
}
181-
let t = self.stdout().terminal(self);
187+
let t = self.stdout();
182188
match self.var("RUSTUP_TERM_PROGRESS_WHEN") {
183189
Ok(s) if s.eq_ignore_ascii_case("always") => ProgressDrawTarget::term_like(Box::new(t)),
184190
Ok(s) if s.eq_ignore_ascii_case("never") => ProgressDrawTarget::hidden(),

0 commit comments

Comments
 (0)