diff --git a/src/cargo/ops/cargo_generate_lockfile.rs b/src/cargo/ops/cargo_generate_lockfile.rs index 36624af48cfc..93d9b5a6ca94 100644 --- a/src/cargo/ops/cargo_generate_lockfile.rs +++ b/src/cargo/ops/cargo_generate_lockfile.rs @@ -10,7 +10,6 @@ use crate::util::cache_lock::CacheLockMode; use crate::util::context::GlobalContext; use crate::util::style; use crate::util::CargoResult; -use anstyle::Style; use std::cmp::Ordering; use std::collections::{BTreeMap, HashSet}; use tracing::debug; @@ -160,7 +159,7 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes ops::write_pkg_lockfile(ws, &mut resolve)? }; if print { - print_lockfile_update(opts.gctx, &previous_resolve, &resolve, &mut registry)?; + print_lockfile_updates(opts.gctx, &previous_resolve, &resolve, &mut registry)?; } if opts.dry_run { @@ -172,16 +171,14 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes Ok(()) } -fn print_lockfile_update( +fn print_lockfile_updates( gctx: &GlobalContext, previous_resolve: &Resolve, resolve: &Resolve, registry: &mut PackageRegistry<'_>, ) -> CargoResult<()> { - // Summarize what is changing for the user. - let print_change = |status: &str, msg: String, color: &Style| { - gctx.shell().status_with_color(status, msg, color) - }; + let mut shell = gctx.shell(); + let mut unchanged_behind = 0; for diff in PackageDiff::diff(&previous_resolve, &resolve) { fn format_latest(version: semver::Version) -> String { @@ -229,13 +226,13 @@ fn print_lockfile_update( // This metadata is often stuff like git commit hashes, which are // not meaningfully ordered. if removed.version().cmp_precedence(added.version()) == Ordering::Greater { - print_change("Downgrading", msg, &style::WARN)?; + shell.status_with_color("Downgrading", msg, &style::WARN)?; } else { - print_change("Updating", msg, &style::GOOD)?; + shell.status_with_color("Updating", msg, &style::GOOD)?; } } else { for package in diff.removed.iter() { - print_change("Removing", format!("{package}"), &style::ERROR)?; + shell.status_with_color("Removing", format!("{package}"), &style::ERROR)?; } for package in diff.added.iter() { let latest = if !possibilities.is_empty() { @@ -251,7 +248,7 @@ fn print_lockfile_update( } .unwrap_or_default(); - print_change("Adding", format!("{package}{latest}"), &style::NOTE)?; + shell.status_with_color("Adding", format!("{package}{latest}"), &style::NOTE)?; } } for package in &diff.unchanged { @@ -269,8 +266,8 @@ fn print_lockfile_update( if let Some(latest) = latest { unchanged_behind += 1; - if gctx.shell().verbosity() == Verbosity::Verbose { - gctx.shell().status_with_color( + if shell.verbosity() == Verbosity::Verbose { + shell.status_with_color( "Unchanged", format!("{package}{latest}"), &anstyle::Style::new().bold(), @@ -279,13 +276,13 @@ fn print_lockfile_update( } } } - if gctx.shell().verbosity() == Verbosity::Verbose { - gctx.shell().note( + if shell.verbosity() == Verbosity::Verbose { + shell.note( "to see how you depend on a package, run `cargo tree --invert --package @`", )?; } else { if 0 < unchanged_behind { - gctx.shell().note(format!( + shell.note(format!( "pass `--verbose` to see {unchanged_behind} unchanged dependencies behind latest" ))?; }