Skip to content

Commit f327dbd

Browse files
committed
Auto merge of #12880 - epage:shell-fmt, r=weihanglo
refactor(shell): Write at once rather than in fragments ### What does this PR try to resolve? For `cargo add` and `cargo search`, rather than expose `termcolor`s API, we wrote a styled fragment at a time. This is no longer needed as of #12751 because we switched from termcolor to anstream which decorates `stdout` and `stderr` with any of the logic we need to adapt to the configured terminal. . ### How should we test and review this PR? Ran the commands locally to verify styled output. Tests verify unstyled output. ### Additional information
2 parents 7f3fc2b + de691b9 commit f327dbd

File tree

3 files changed

+27
-63
lines changed

3 files changed

+27
-63
lines changed

src/cargo/core/shell.rs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -340,20 +340,6 @@ impl Shell {
340340
}
341341
}
342342

343-
/// Write a styled fragment
344-
///
345-
/// Caller is responsible for deciding whether [`Shell::verbosity`] is affects output.
346-
pub fn write_stdout(&mut self, fragment: impl fmt::Display, color: &Style) -> CargoResult<()> {
347-
self.output.write_stdout(fragment, color)
348-
}
349-
350-
/// Write a styled fragment
351-
///
352-
/// Caller is responsible for deciding whether [`Shell::verbosity`] is affects output.
353-
pub fn write_stderr(&mut self, fragment: impl fmt::Display, color: &Style) -> CargoResult<()> {
354-
self.output.write_stderr(fragment, color)
355-
}
356-
357343
/// Prints a message to stderr and translates ANSI escape code into console colors.
358344
pub fn print_ansi_stderr(&mut self, message: &[u8]) -> CargoResult<()> {
359345
if self.needs_clear {
@@ -416,28 +402,6 @@ impl ShellOut {
416402
Ok(())
417403
}
418404

419-
/// Write a styled fragment
420-
fn write_stdout(&mut self, fragment: impl fmt::Display, style: &Style) -> CargoResult<()> {
421-
let style = style.render();
422-
let reset = anstyle::Reset.render();
423-
424-
let mut buffer = Vec::new();
425-
write!(buffer, "{style}{}{reset}", fragment)?;
426-
self.stdout().write_all(&buffer)?;
427-
Ok(())
428-
}
429-
430-
/// Write a styled fragment
431-
fn write_stderr(&mut self, fragment: impl fmt::Display, style: &Style) -> CargoResult<()> {
432-
let style = style.render();
433-
let reset = anstyle::Reset.render();
434-
435-
let mut buffer = Vec::new();
436-
write!(buffer, "{style}{}{reset}", fragment)?;
437-
self.stderr().write_all(&buffer)?;
438-
Ok(())
439-
}
440-
441405
/// Gets stdout as a `io::Write`.
442406
fn stdout(&mut self) -> &mut dyn Write {
443407
match *self {

src/cargo/ops/cargo_add/mod.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -947,41 +947,36 @@ fn print_dep_table_msg(shell: &mut Shell, dep: &DependencyUI) -> CargoResult<()>
947947
return Ok(());
948948
}
949949

950+
let stderr = shell.err();
951+
let good = style::GOOD.render();
952+
let error = style::ERROR.render();
953+
let reset = anstyle::Reset.render();
954+
950955
let (activated, deactivated) = dep.features();
951956
if !activated.is_empty() || !deactivated.is_empty() {
952957
let prefix = format!("{:>13}", " ");
953958
let suffix = format_features_version_suffix(&dep);
954959

955-
shell.write_stderr(format_args!("{prefix}Features{suffix}:\n"), &style::NOP)?;
960+
writeln!(stderr, "{prefix}Features{suffix}:")?;
956961

957962
const MAX_FEATURE_PRINTS: usize = 30;
958963
let total_activated = activated.len();
959964
let total_deactivated = deactivated.len();
960965

961966
if total_activated <= MAX_FEATURE_PRINTS {
962967
for feat in activated {
963-
shell.write_stderr(&prefix, &style::NOP)?;
964-
shell.write_stderr('+', &style::GOOD)?;
965-
shell.write_stderr(format_args!(" {feat}\n"), &style::NOP)?;
968+
writeln!(stderr, "{prefix}{good}+{reset} {feat}")?;
966969
}
967970
} else {
968-
shell.write_stderr(
969-
format_args!("{prefix}{total_activated} activated features\n"),
970-
&style::NOP,
971-
)?;
971+
writeln!(stderr, "{prefix}{total_activated} activated features")?;
972972
}
973973

974974
if total_activated + total_deactivated <= MAX_FEATURE_PRINTS {
975975
for feat in deactivated {
976-
shell.write_stderr(&prefix, &style::NOP)?;
977-
shell.write_stderr('-', &style::ERROR)?;
978-
shell.write_stderr(format_args!(" {feat}\n"), &style::NOP)?;
976+
writeln!(stderr, "{prefix}{error}-{reset} {feat}")?;
979977
}
980978
} else {
981-
shell.write_stderr(
982-
format_args!("{prefix}{total_deactivated} deactivated features\n"),
983-
&style::NOP,
984-
)?;
979+
writeln!(stderr, "{prefix}{total_deactivated} deactivated features")?;
985980
}
986981
}
987982

src/cargo/ops/registry/search.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ pub fn search(
4646
.map(|desc| truncate_with_ellipsis(&desc.replace("\n", " "), description_length))
4747
});
4848

49+
let mut shell = config.shell();
50+
let stdout = shell.out();
51+
let good = style::GOOD.render();
52+
let reset = anstyle::Reset.render();
53+
4954
for (name, description) in names.into_iter().zip(descriptions) {
5055
let line = match description {
5156
Some(desc) => {
@@ -58,22 +63,20 @@ pub fn search(
5863
};
5964
let mut fragments = line.split(query).peekable();
6065
while let Some(fragment) = fragments.next() {
61-
let _ = config.shell().write_stdout(fragment, &style::NOP);
66+
let _ = write!(stdout, "{fragment}");
6267
if fragments.peek().is_some() {
63-
let _ = config.shell().write_stdout(query, &style::GOOD);
68+
let _ = write!(stdout, "{good}{query}{reset}");
6469
}
6570
}
66-
let _ = config.shell().write_stdout("\n", &style::NOP);
71+
let _ = writeln!(stdout);
6772
}
6873

6974
let search_max_limit = 100;
7075
if total_crates > limit && limit < search_max_limit {
71-
let _ = config.shell().write_stdout(
72-
format_args!(
73-
"... and {} crates more (use --limit N to see more)\n",
74-
total_crates - limit
75-
),
76-
&style::NOP,
76+
let _ = writeln!(
77+
stdout,
78+
"... and {} crates more (use --limit N to see more)",
79+
total_crates - limit
7780
);
7881
} else if total_crates > limit && limit >= search_max_limit {
7982
let extra = if source_ids.original.is_crates_io() {
@@ -82,9 +85,11 @@ pub fn search(
8285
} else {
8386
String::new()
8487
};
85-
let _ = config.shell().write_stdout(
86-
format_args!("... and {} crates more{}\n", total_crates - limit, extra),
87-
&style::NOP,
88+
let _ = writeln!(
89+
stdout,
90+
"... and {} crates more{}",
91+
total_crates - limit,
92+
extra
8893
);
8994
}
9095

0 commit comments

Comments
 (0)