From 27b5ac7076f201c3f0aacfbdf43cf3b8d418d9fd Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 25 Oct 2023 11:14:25 -0500 Subject: [PATCH 1/4] refactor(add): Rely on anstream for cleaner code --- src/cargo/ops/cargo_add/mod.rs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index a8a8b76fa54..9c2915c59bc 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -947,12 +947,17 @@ fn print_dep_table_msg(shell: &mut Shell, dep: &DependencyUI) -> CargoResult<()> return Ok(()); } + let stderr = shell.err(); + let good = style::GOOD.render(); + let error = style::ERROR.render(); + let reset = anstyle::Reset.render(); + let (activated, deactivated) = dep.features(); if !activated.is_empty() || !deactivated.is_empty() { let prefix = format!("{:>13}", " "); let suffix = format_features_version_suffix(&dep); - shell.write_stderr(format_args!("{prefix}Features{suffix}:\n"), &style::NOP)?; + writeln!(stderr, "{prefix}Features{suffix}:")?; const MAX_FEATURE_PRINTS: usize = 30; let total_activated = activated.len(); @@ -960,28 +965,18 @@ fn print_dep_table_msg(shell: &mut Shell, dep: &DependencyUI) -> CargoResult<()> if total_activated <= MAX_FEATURE_PRINTS { for feat in activated { - shell.write_stderr(&prefix, &style::NOP)?; - shell.write_stderr('+', &style::GOOD)?; - shell.write_stderr(format_args!(" {feat}\n"), &style::NOP)?; + writeln!(stderr, "{prefix}{good}+{reset} {feat}")?; } } else { - shell.write_stderr( - format_args!("{prefix}{total_activated} activated features\n"), - &style::NOP, - )?; + writeln!(stderr, "{prefix}{total_activated} activated features")?; } if total_activated + total_deactivated <= MAX_FEATURE_PRINTS { for feat in deactivated { - shell.write_stderr(&prefix, &style::NOP)?; - shell.write_stderr('-', &style::ERROR)?; - shell.write_stderr(format_args!(" {feat}\n"), &style::NOP)?; + writeln!(stderr, "{prefix}{error}-{reset} {feat}")?; } } else { - shell.write_stderr( - format_args!("{prefix}{total_deactivated} deactivated features\n"), - &style::NOP, - )?; + writeln!(stderr, "{prefix}{total_deactivated} deactivated features")?; } } From 1b9aefb1036339a8cbb65c6b307d29db94e50919 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 25 Oct 2023 11:15:49 -0500 Subject: [PATCH 2/4] refactor(shell): Remove unused write_stderr --- src/cargo/core/shell.rs | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index a9b9e84afcd..d4df8a661d7 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -347,13 +347,6 @@ impl Shell { self.output.write_stdout(fragment, color) } - /// Write a styled fragment - /// - /// Caller is responsible for deciding whether [`Shell::verbosity`] is affects output. - pub fn write_stderr(&mut self, fragment: impl fmt::Display, color: &Style) -> CargoResult<()> { - self.output.write_stderr(fragment, color) - } - /// Prints a message to stderr and translates ANSI escape code into console colors. pub fn print_ansi_stderr(&mut self, message: &[u8]) -> CargoResult<()> { if self.needs_clear { @@ -427,17 +420,6 @@ impl ShellOut { Ok(()) } - /// Write a styled fragment - fn write_stderr(&mut self, fragment: impl fmt::Display, style: &Style) -> CargoResult<()> { - let style = style.render(); - let reset = anstyle::Reset.render(); - - let mut buffer = Vec::new(); - write!(buffer, "{style}{}{reset}", fragment)?; - self.stderr().write_all(&buffer)?; - Ok(()) - } - /// Gets stdout as a `io::Write`. fn stdout(&mut self) -> &mut dyn Write { match *self { From 6a390da44bd9e9c259c350ce7b3d20feba4d9b88 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 25 Oct 2023 11:27:32 -0500 Subject: [PATCH 3/4] refactor(search): Rely on anstream for cleaner code --- src/cargo/ops/registry/search.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/cargo/ops/registry/search.rs b/src/cargo/ops/registry/search.rs index 10b4d600e6b..2672e0ecabb 100644 --- a/src/cargo/ops/registry/search.rs +++ b/src/cargo/ops/registry/search.rs @@ -46,6 +46,11 @@ pub fn search( .map(|desc| truncate_with_ellipsis(&desc.replace("\n", " "), description_length)) }); + let mut shell = config.shell(); + let stdout = shell.out(); + let good = style::GOOD.render(); + let reset = anstyle::Reset.render(); + for (name, description) in names.into_iter().zip(descriptions) { let line = match description { Some(desc) => { @@ -58,22 +63,20 @@ pub fn search( }; let mut fragments = line.split(query).peekable(); while let Some(fragment) = fragments.next() { - let _ = config.shell().write_stdout(fragment, &style::NOP); + let _ = write!(stdout, "{fragment}"); if fragments.peek().is_some() { - let _ = config.shell().write_stdout(query, &style::GOOD); + let _ = write!(stdout, "{good}{query}{reset}"); } } - let _ = config.shell().write_stdout("\n", &style::NOP); + let _ = writeln!(stdout); } let search_max_limit = 100; if total_crates > limit && limit < search_max_limit { - let _ = config.shell().write_stdout( - format_args!( - "... and {} crates more (use --limit N to see more)\n", - total_crates - limit - ), - &style::NOP, + let _ = writeln!( + stdout, + "... and {} crates more (use --limit N to see more)", + total_crates - limit ); } else if total_crates > limit && limit >= search_max_limit { let extra = if source_ids.original.is_crates_io() { @@ -82,9 +85,11 @@ pub fn search( } else { String::new() }; - let _ = config.shell().write_stdout( - format_args!("... and {} crates more{}\n", total_crates - limit, extra), - &style::NOP, + let _ = writeln!( + stdout, + "... and {} crates more{}", + total_crates - limit, + extra ); } From de691b9a02dba95c3ebb4d262480309f83c3b553 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 25 Oct 2023 11:28:09 -0500 Subject: [PATCH 4/4] refactor(shell): Remove unused write_stdout --- src/cargo/core/shell.rs | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index d4df8a661d7..768c86f7a91 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -340,13 +340,6 @@ impl Shell { } } - /// Write a styled fragment - /// - /// Caller is responsible for deciding whether [`Shell::verbosity`] is affects output. - pub fn write_stdout(&mut self, fragment: impl fmt::Display, color: &Style) -> CargoResult<()> { - self.output.write_stdout(fragment, color) - } - /// Prints a message to stderr and translates ANSI escape code into console colors. pub fn print_ansi_stderr(&mut self, message: &[u8]) -> CargoResult<()> { if self.needs_clear { @@ -409,17 +402,6 @@ impl ShellOut { Ok(()) } - /// Write a styled fragment - fn write_stdout(&mut self, fragment: impl fmt::Display, style: &Style) -> CargoResult<()> { - let style = style.render(); - let reset = anstyle::Reset.render(); - - let mut buffer = Vec::new(); - write!(buffer, "{style}{}{reset}", fragment)?; - self.stdout().write_all(&buffer)?; - Ok(()) - } - /// Gets stdout as a `io::Write`. fn stdout(&mut self) -> &mut dyn Write { match *self {