Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(shell): Write at once rather than in fragments #12880

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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