Skip to content

Commit

Permalink
Auto merge of #12880 - epage:shell-fmt, r=weihanglo
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Oct 26, 2023
2 parents 7f3fc2b + de691b9 commit f327dbd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 63 deletions.
36 changes: 0 additions & 36 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,20 +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)
}

/// 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 {
Expand Down Expand Up @@ -416,28 +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(())
}

/// 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 {
Expand Down
25 changes: 10 additions & 15 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,41 +947,36 @@ 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();
let total_deactivated = deactivated.len();

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")?;
}
}

Expand Down
29 changes: 17 additions & 12 deletions src/cargo/ops/registry/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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() {
Expand All @@ -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
);
}

Expand Down

0 comments on commit f327dbd

Please sign in to comment.