From 1035edeb63d0794974f92e43e9038de3823409b4 Mon Sep 17 00:00:00 2001 From: cassaundra Date: Mon, 27 Jun 2022 19:41:45 -0700 Subject: [PATCH 1/2] fix: align "upgrading" message correctly Fixes an off-by-one relative to the rest of the project's output formatting. --- src/manifest.rs | 2 +- tests/cmd/upgrade/alt_registry.toml | 4 +-- tests/cmd/upgrade/dry_run.toml | 2 +- tests/cmd/upgrade/dry_run_prerelease.toml | 2 +- tests/cmd/upgrade/exclude_dep.toml | 28 ++++++++--------- tests/cmd/upgrade/exclude_renamed.toml | 2 +- tests/cmd/upgrade/implicit_prerelease.toml | 4 +-- tests/cmd/upgrade/optional_dep.toml | 2 +- tests/cmd/upgrade/prerelease.toml | 2 +- .../cmd/upgrade/preserve_precision_major.toml | 2 +- .../cmd/upgrade/preserve_precision_minor.toml | 2 +- .../cmd/upgrade/preserve_precision_patch.toml | 2 +- tests/cmd/upgrade/single_dep.toml | 2 +- tests/cmd/upgrade/skip_compatible.toml | 2 +- tests/cmd/upgrade/skip_pinned.toml | 12 ++++---- tests/cmd/upgrade/specified.toml | 2 +- tests/cmd/upgrade/to_lockfile.toml | 12 ++++---- tests/cmd/upgrade/to_version.toml | 2 +- tests/cmd/upgrade/upgrade_all.toml | 12 ++++---- tests/cmd/upgrade/upgrade_everything.toml | 30 +++++++++---------- tests/cmd/upgrade/upgrade_renamed.toml | 4 +-- tests/cmd/upgrade/upgrade_workspace.toml | 12 ++++---- tests/cmd/upgrade/workspace_member_cwd.toml | 2 +- 23 files changed, 73 insertions(+), 73 deletions(-) diff --git a/src/manifest.rs b/src/manifest.rs index 2f49513582..dd7b1d6f0a 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -709,7 +709,7 @@ fn print_upgrade_if_necessary( buffer .set_color(ColorSpec::new().set_fg(Some(Color::Green)).set_bold(true)) .with_context(|| "Failed to set output colour")?; - write!(&mut buffer, " Upgrading ").with_context(|| "Failed to write upgrade message")?; + write!(&mut buffer, " Upgrading ").with_context(|| "Failed to write upgrade message")?; buffer .set_color(&ColorSpec::new()) .with_context(|| "Failed to clear output colour")?; diff --git a/tests/cmd/upgrade/alt_registry.toml b/tests/cmd/upgrade/alt_registry.toml index dc9d024f96..5f564b9b63 100644 --- a/tests/cmd/upgrade/alt_registry.toml +++ b/tests/cmd/upgrade/alt_registry.toml @@ -5,8 +5,8 @@ stdout = """ none: """ stderr = """ - Upgrading regex v0.2 -> v99999.0.0 - Upgrading toml_edit v0.1.5 -> v99999.0.0 + Upgrading regex v0.2 -> v99999.0.0 + Upgrading toml_edit v0.1.5 -> v99999.0.0 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/dry_run.toml b/tests/cmd/upgrade/dry_run.toml index 0438a9d6eb..6856f431e6 100644 --- a/tests/cmd/upgrade/dry_run.toml +++ b/tests/cmd/upgrade/dry_run.toml @@ -5,7 +5,7 @@ stdout = """ cargo-list-test-fixture: """ stderr = """ - Upgrading docopt v0.8.0 -> v99999.0.0 + Upgrading docopt v0.8.0 -> v99999.0.0 warning: aborting upgrade due to dry run """ fs.sandbox = true diff --git a/tests/cmd/upgrade/dry_run_prerelease.toml b/tests/cmd/upgrade/dry_run_prerelease.toml index 216e78e601..df30a653a7 100644 --- a/tests/cmd/upgrade/dry_run_prerelease.toml +++ b/tests/cmd/upgrade/dry_run_prerelease.toml @@ -5,7 +5,7 @@ stdout = """ cargo-list-test-fixture: """ stderr = """ - Upgrading docopt v0.8.0 -> v99999.0.0-alpha.1 + Upgrading docopt v0.8.0 -> v99999.0.0-alpha.1 warning: aborting upgrade due to dry run """ fs.sandbox = true diff --git a/tests/cmd/upgrade/exclude_dep.toml b/tests/cmd/upgrade/exclude_dep.toml index ef7edc4694..38d4aa75fb 100644 --- a/tests/cmd/upgrade/exclude_dep.toml +++ b/tests/cmd/upgrade/exclude_dep.toml @@ -5,20 +5,20 @@ stdout = """ None: """ stderr = """ - Upgrading assert_cli v0.2.0 -> v99999.0.0 - Upgrading ftp v2.2.1 -> v99999.0.0 - Upgrading ftp v2.2.1 -> v99999.0.0 - Upgrading geo v0.7.0 -> v99999.0.0 - Upgrading openssl v0.9 -> v99999.0.0 - Upgrading pad v0.1 -> v99999.0.0 - Upgrading renamed v0.1 -> v99999.0.0 - Upgrading rget v0.3.0 -> v99999.0.0 - Upgrading semver v0.7 -> v99999.0.0 - Upgrading serde_json v1.0 -> v99999.0.0 - Upgrading syn v0.11.10 -> v99999.0.0 - Upgrading tar v0.4 -> v99999.0.0 - Upgrading tempdir v0.3 -> v99999.0.0 - Upgrading toml_edit v0.1.5 -> v99999.0.0 + Upgrading assert_cli v0.2.0 -> v99999.0.0 + Upgrading ftp v2.2.1 -> v99999.0.0 + Upgrading ftp v2.2.1 -> v99999.0.0 + Upgrading geo v0.7.0 -> v99999.0.0 + Upgrading openssl v0.9 -> v99999.0.0 + Upgrading pad v0.1 -> v99999.0.0 + Upgrading renamed v0.1 -> v99999.0.0 + Upgrading rget v0.3.0 -> v99999.0.0 + Upgrading semver v0.7 -> v99999.0.0 + Upgrading serde_json v1.0 -> v99999.0.0 + Upgrading syn v0.11.10 -> v99999.0.0 + Upgrading tar v0.4 -> v99999.0.0 + Upgrading tempdir v0.3 -> v99999.0.0 + Upgrading toml_edit v0.1.5 -> v99999.0.0 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/exclude_renamed.toml b/tests/cmd/upgrade/exclude_renamed.toml index 719a991b50..0ff8374902 100644 --- a/tests/cmd/upgrade/exclude_renamed.toml +++ b/tests/cmd/upgrade/exclude_renamed.toml @@ -5,7 +5,7 @@ stdout = """ cargo-list-test-fixture: """ stderr = """ - Upgrading toml_edit v0.1.5 -> v99999.0.0 + Upgrading toml_edit v0.1.5 -> v99999.0.0 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/implicit_prerelease.toml b/tests/cmd/upgrade/implicit_prerelease.toml index 7dd7756879..f708edaaf5 100644 --- a/tests/cmd/upgrade/implicit_prerelease.toml +++ b/tests/cmd/upgrade/implicit_prerelease.toml @@ -5,8 +5,8 @@ stdout = """ cargo-list-test-fixture: """ stderr = """ - Upgrading a v1.0 -> v99999.0.0 - Upgrading b v0.8.0-alpha -> v99999.0.0-alpha.1 + Upgrading a v1.0 -> v99999.0.0 + Upgrading b v0.8.0-alpha -> v99999.0.0-alpha.1 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/optional_dep.toml b/tests/cmd/upgrade/optional_dep.toml index 664857ec81..ba47041369 100644 --- a/tests/cmd/upgrade/optional_dep.toml +++ b/tests/cmd/upgrade/optional_dep.toml @@ -5,7 +5,7 @@ stdout = """ cargo-list-test-fixture: """ stderr = """ - Upgrading docopt v0.8.0 -> v99999.0.0 + Upgrading docopt v0.8.0 -> v99999.0.0 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/prerelease.toml b/tests/cmd/upgrade/prerelease.toml index aac7e1ac7f..ca0734086c 100644 --- a/tests/cmd/upgrade/prerelease.toml +++ b/tests/cmd/upgrade/prerelease.toml @@ -5,7 +5,7 @@ stdout = """ cargo-list-test-fixture: """ stderr = """ - Upgrading docopt v0.8.0 -> v99999.0.0-alpha.1 + Upgrading docopt v0.8.0 -> v99999.0.0-alpha.1 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/preserve_precision_major.toml b/tests/cmd/upgrade/preserve_precision_major.toml index fc725463f7..828787cf30 100644 --- a/tests/cmd/upgrade/preserve_precision_major.toml +++ b/tests/cmd/upgrade/preserve_precision_major.toml @@ -5,7 +5,7 @@ stdout = """ cargo-list-test-fixture: """ stderr = """ - Upgrading docopt v0 -> v99999 + Upgrading docopt v0 -> v99999 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/preserve_precision_minor.toml b/tests/cmd/upgrade/preserve_precision_minor.toml index 64de835a7a..09df3443d9 100644 --- a/tests/cmd/upgrade/preserve_precision_minor.toml +++ b/tests/cmd/upgrade/preserve_precision_minor.toml @@ -5,7 +5,7 @@ stdout = """ cargo-list-test-fixture: """ stderr = """ - Upgrading docopt v0.8 -> v99999.0 + Upgrading docopt v0.8 -> v99999.0 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/preserve_precision_patch.toml b/tests/cmd/upgrade/preserve_precision_patch.toml index e786fd6a64..8499ef6882 100644 --- a/tests/cmd/upgrade/preserve_precision_patch.toml +++ b/tests/cmd/upgrade/preserve_precision_patch.toml @@ -5,7 +5,7 @@ stdout = """ cargo-list-test-fixture: """ stderr = """ - Upgrading docopt v0.8.0 -> v99999.0.0 + Upgrading docopt v0.8.0 -> v99999.0.0 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/single_dep.toml b/tests/cmd/upgrade/single_dep.toml index 664857ec81..ba47041369 100644 --- a/tests/cmd/upgrade/single_dep.toml +++ b/tests/cmd/upgrade/single_dep.toml @@ -5,7 +5,7 @@ stdout = """ cargo-list-test-fixture: """ stderr = """ - Upgrading docopt v0.8.0 -> v99999.0.0 + Upgrading docopt v0.8.0 -> v99999.0.0 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/skip_compatible.toml b/tests/cmd/upgrade/skip_compatible.toml index d59c577f2f..d6a657660b 100644 --- a/tests/cmd/upgrade/skip_compatible.toml +++ b/tests/cmd/upgrade/skip_compatible.toml @@ -5,7 +5,7 @@ stdout = """ cargo-list-test-fixture: """ stderr = """ - Upgrading test_breaking v0.1 -> v0.2.0 + Upgrading test_breaking v0.1 -> v0.2.0 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/skip_pinned.toml b/tests/cmd/upgrade/skip_pinned.toml index 33fd2cf724..04ca0254e4 100644 --- a/tests/cmd/upgrade/skip_pinned.toml +++ b/tests/cmd/upgrade/skip_pinned.toml @@ -5,12 +5,12 @@ stdout = """ cargo-list-test-fixture: """ stderr = """ - Upgrading caret v^3.0 -> v99999.0.0 - Upgrading default v1.0 -> v99999.0.0 - Upgrading greaterorequal v>=2.1.0 -> v99999.0.0 - Upgrading greaterthan v>2.0 -> v99999.0.0 - Upgrading tilde v~4.1.0 -> v99999.0.0 - Upgrading wildcard v3.* -> v99999.0.0 + Upgrading caret v^3.0 -> v99999.0.0 + Upgrading default v1.0 -> v99999.0.0 + Upgrading greaterorequal v>=2.1.0 -> v99999.0.0 + Upgrading greaterthan v>2.0 -> v99999.0.0 + Upgrading tilde v~4.1.0 -> v99999.0.0 + Upgrading wildcard v3.* -> v99999.0.0 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/specified.toml b/tests/cmd/upgrade/specified.toml index 8db9271a06..d42a7b6e19 100644 --- a/tests/cmd/upgrade/specified.toml +++ b/tests/cmd/upgrade/specified.toml @@ -5,7 +5,7 @@ stdout = """ cargo-list-test-fixture: """ stderr = """ - Upgrading a v1.0 -> v99999.0.0 + Upgrading a v1.0 -> v99999.0.0 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/to_lockfile.toml b/tests/cmd/upgrade/to_lockfile.toml index c8b33180e7..ac8a4cd30d 100644 --- a/tests/cmd/upgrade/to_lockfile.toml +++ b/tests/cmd/upgrade/to_lockfile.toml @@ -8,12 +8,12 @@ two: four: """ stderr = """ - Upgrading libc v0.2.28 -> v0.2.62 - Upgrading rand v0.3 -> v0.3.23 - Upgrading libc v0.2.28 -> v0.2.62 - Upgrading libc v0.2.28 -> v0.2.62 - Upgrading rand v0.2 -> v0.2.1 - Upgrading libc v0.2.28 -> v0.2.62 + Upgrading libc v0.2.28 -> v0.2.62 + Upgrading rand v0.3 -> v0.3.23 + Upgrading libc v0.2.28 -> v0.2.62 + Upgrading libc v0.2.28 -> v0.2.62 + Upgrading rand v0.2 -> v0.2.1 + Upgrading libc v0.2.28 -> v0.2.62 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/to_version.toml b/tests/cmd/upgrade/to_version.toml index 98b0fc0f1c..9c7d4eaac4 100644 --- a/tests/cmd/upgrade/to_version.toml +++ b/tests/cmd/upgrade/to_version.toml @@ -5,7 +5,7 @@ stdout = """ cargo-list-test-fixture: """ stderr = """ - Upgrading docopt v0.8.0 -> v1000000.0.0 + Upgrading docopt v0.8.0 -> v1000000.0.0 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/upgrade_all.toml b/tests/cmd/upgrade/upgrade_all.toml index 476d971c59..25822ee935 100644 --- a/tests/cmd/upgrade/upgrade_all.toml +++ b/tests/cmd/upgrade/upgrade_all.toml @@ -9,12 +9,12 @@ four: """ stderr = """ The flag `--all` has been deprecated in favor of `--workspace` - Upgrading libc v0.2.28 -> v99999.0.0 - Upgrading rand v0.3 -> v99999.0.0 - Upgrading libc v0.2.28 -> v99999.0.0 - Upgrading libc v0.2.28 -> v99999.0.0 - Upgrading rand v0.2 -> v99999.0.0 - Upgrading libc v0.2.28 -> v99999.0.0 + Upgrading libc v0.2.28 -> v99999.0.0 + Upgrading rand v0.3 -> v99999.0.0 + Upgrading libc v0.2.28 -> v99999.0.0 + Upgrading libc v0.2.28 -> v99999.0.0 + Upgrading rand v0.2 -> v99999.0.0 + Upgrading libc v0.2.28 -> v99999.0.0 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/upgrade_everything.toml b/tests/cmd/upgrade/upgrade_everything.toml index b92653f264..955e55b076 100644 --- a/tests/cmd/upgrade/upgrade_everything.toml +++ b/tests/cmd/upgrade/upgrade_everything.toml @@ -5,21 +5,21 @@ stdout = """ None: """ stderr = """ - Upgrading assert_cli v0.2.0 -> v99999.0.0 - Upgrading docopt v0.8 -> v99999.0.0 - Upgrading ftp v2.2.1 -> v99999.0.0 - Upgrading ftp v2.2.1 -> v99999.0.0 - Upgrading geo v0.7.0 -> v99999.0.0 - Upgrading openssl v0.9 -> v99999.0.0 - Upgrading pad v0.1 -> v99999.0.0 - Upgrading renamed v0.1 -> v99999.0.0 - Upgrading rget v0.3.0 -> v99999.0.0 - Upgrading semver v0.7 -> v99999.0.0 - Upgrading serde_json v1.0 -> v99999.0.0 - Upgrading syn v0.11.10 -> v99999.0.0 - Upgrading tar v0.4 -> v99999.0.0 - Upgrading tempdir v0.3 -> v99999.0.0 - Upgrading toml_edit v0.1.5 -> v99999.0.0 + Upgrading assert_cli v0.2.0 -> v99999.0.0 + Upgrading docopt v0.8 -> v99999.0.0 + Upgrading ftp v2.2.1 -> v99999.0.0 + Upgrading ftp v2.2.1 -> v99999.0.0 + Upgrading geo v0.7.0 -> v99999.0.0 + Upgrading openssl v0.9 -> v99999.0.0 + Upgrading pad v0.1 -> v99999.0.0 + Upgrading renamed v0.1 -> v99999.0.0 + Upgrading rget v0.3.0 -> v99999.0.0 + Upgrading semver v0.7 -> v99999.0.0 + Upgrading serde_json v1.0 -> v99999.0.0 + Upgrading syn v0.11.10 -> v99999.0.0 + Upgrading tar v0.4 -> v99999.0.0 + Upgrading tempdir v0.3 -> v99999.0.0 + Upgrading toml_edit v0.1.5 -> v99999.0.0 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/upgrade_renamed.toml b/tests/cmd/upgrade/upgrade_renamed.toml index e9d7ac3885..22f372499a 100644 --- a/tests/cmd/upgrade/upgrade_renamed.toml +++ b/tests/cmd/upgrade/upgrade_renamed.toml @@ -5,8 +5,8 @@ stdout = """ cargo-list-test-fixture: """ stderr = """ - Upgrading regex v0.2 -> v99999.0.0 - Upgrading toml_edit v0.1.5 -> v99999.0.0 + Upgrading regex v0.2 -> v99999.0.0 + Upgrading toml_edit v0.1.5 -> v99999.0.0 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/upgrade_workspace.toml b/tests/cmd/upgrade/upgrade_workspace.toml index 7989ae87d7..2e78552375 100644 --- a/tests/cmd/upgrade/upgrade_workspace.toml +++ b/tests/cmd/upgrade/upgrade_workspace.toml @@ -8,12 +8,12 @@ two: four: """ stderr = """ - Upgrading libc v0.2.28 -> v99999.0.0 - Upgrading rand v0.3 -> v99999.0.0 - Upgrading libc v0.2.28 -> v99999.0.0 - Upgrading libc v0.2.28 -> v99999.0.0 - Upgrading rand v0.2 -> v99999.0.0 - Upgrading libc v0.2.28 -> v99999.0.0 + Upgrading libc v0.2.28 -> v99999.0.0 + Upgrading rand v0.3 -> v99999.0.0 + Upgrading libc v0.2.28 -> v99999.0.0 + Upgrading libc v0.2.28 -> v99999.0.0 + Upgrading rand v0.2 -> v99999.0.0 + Upgrading libc v0.2.28 -> v99999.0.0 """ fs.sandbox = true diff --git a/tests/cmd/upgrade/workspace_member_cwd.toml b/tests/cmd/upgrade/workspace_member_cwd.toml index 4537e0521f..edefa37bbf 100644 --- a/tests/cmd/upgrade/workspace_member_cwd.toml +++ b/tests/cmd/upgrade/workspace_member_cwd.toml @@ -5,7 +5,7 @@ stdout = """ one: """ stderr = """ - Upgrading libc v0.2.28 -> v99999.0.0 + Upgrading libc v0.2.28 -> v99999.0.0 """ fs.sandbox = true fs.cwd = "workspace_member_cwd.in/one" From fcdef03f561cc8007fcaa54434115caa7df86975 Mon Sep 17 00:00:00 2001 From: cassaundra Date: Mon, 27 Jun 2022 19:36:37 -0700 Subject: [PATCH 2/2] refactor: clean up styled console output with utils --- src/bin/rm/rm.rs | 28 +++++++++------------------- src/bin/upgrade/upgrade.rs | 19 ++----------------- src/fetch.rs | 23 ++++------------------- src/lib.rs | 2 +- src/manifest.rs | 26 +++++--------------------- src/util.rs | 37 ++++++++++++++++++++++++++++++++++++- 6 files changed, 57 insertions(+), 78 deletions(-) diff --git a/src/bin/rm/rm.rs b/src/bin/rm/rm.rs index d653a91533..7cd0f557dc 100644 --- a/src/bin/rm/rm.rs +++ b/src/bin/rm/rm.rs @@ -1,10 +1,9 @@ +use cargo_edit::shell_status; use cargo_edit::CargoResult; -use cargo_edit::{colorize_stderr, manifest_from_pkgid, LocalManifest}; +use cargo_edit::{manifest_from_pkgid, LocalManifest}; use clap::Args; use std::borrow::Cow; -use std::io::Write; use std::path::PathBuf; -use termcolor::{Color, ColorSpec, StandardStream, WriteColor}; /// Remove a dependency from a Cargo.toml manifest file. #[derive(Debug, Args)] @@ -81,21 +80,6 @@ impl RmArgs { #[derive(Copy, Clone, Debug, PartialEq, Eq, clap::ArgEnum)] enum UnstableOptions {} -fn print_msg(name: &str, section: &[String]) -> CargoResult<()> { - let colorchoice = colorize_stderr(); - let mut output = StandardStream::stderr(colorchoice); - output.set_color(ColorSpec::new().set_fg(Some(Color::Green)).set_bold(true))?; - write!(output, "{:>12}", "Removing")?; - output.reset()?; - let section = if section.len() == 1 { - section[0].clone() - } else { - format!("{} for target `{}`", §ion[2], §ion[1]) - }; - writeln!(output, " {} from {}", name, section)?; - Ok(()) -} - fn exec(args: &RmArgs) -> CargoResult<()> { let manifest_path = if let Some(ref pkgid) = args.pkgid { let pkg = manifest_from_pkgid(args.manifest_path.as_deref(), pkgid)?; @@ -109,7 +93,13 @@ fn exec(args: &RmArgs) -> CargoResult<()> { deps.iter() .map(|dep| { if !args.quiet { - print_msg(dep, &args.get_section())?; + let section = args.get_section(); + let section = if section.len() >= 3 { + format!("{} for target `{}`", §ion[2], §ion[1]) + } else { + section[0].clone() + }; + shell_status("Removing", &format!("{dep} from {section}",))?; } let result = manifest .remove_from_table(&args.get_section(), dep) diff --git a/src/bin/upgrade/upgrade.rs b/src/bin/upgrade/upgrade.rs index 2359b05943..f5e5600b90 100644 --- a/src/bin/upgrade/upgrade.rs +++ b/src/bin/upgrade/upgrade.rs @@ -3,7 +3,7 @@ use std::io::Write; use std::path::{Path, PathBuf}; use cargo_edit::{ - colorize_stderr, find, get_latest_dependency, manifest_from_pkgid, registry_url, + colorize_stderr, find, get_latest_dependency, manifest_from_pkgid, registry_url, shell_warn, update_registry_index, CargoResult, Context, CrateSpec, Dependency, LocalManifest, }; use clap::Args; @@ -191,7 +191,7 @@ fn exec(args: UpgradeArgs) -> CargoResult<()> { } if args.dry_run { - dry_run_message()?; + shell_warn("aborting upgrade due to dry run")?; } Ok(()) @@ -517,18 +517,3 @@ fn deprecated_message(message: &str) -> CargoResult<()> { .with_context(|| "Failed to clear output colour")?; Ok(()) } - -fn dry_run_message() -> CargoResult<()> { - let colorchoice = colorize_stderr(); - let mut output = StandardStream::stderr(colorchoice); - output - .set_color(ColorSpec::new().set_fg(Some(Color::Yellow)).set_bold(true)) - .with_context(|| "Failed to set output colour")?; - write!(output, "warning").with_context(|| "Failed to write dry run message")?; - output - .set_color(&ColorSpec::new()) - .with_context(|| "Failed to clear output colour")?; - writeln!(output, ": aborting upgrade due to dry run") - .with_context(|| "Failed to write dry run message")?; - Ok(()) -} diff --git a/src/fetch.rs b/src/fetch.rs index 4dc7d26f01..cfef6f5b49 100644 --- a/src/fetch.rs +++ b/src/fetch.rs @@ -1,16 +1,16 @@ use std::collections::BTreeMap; use std::env; -use std::io::Write; use std::path::Path; use std::time::Duration; -use termcolor::{Color, ColorSpec, StandardStream, WriteColor}; use url::Url; use super::errors::*; use super::registry::registry_url; +use super::shell_status; use super::VersionExt; use super::{Dependency, LocalManifest, Manifest}; + use regex::Regex; /// Query latest version from a registry index @@ -250,19 +250,13 @@ fn registry_features(v: &crates_index::Version) -> BTreeMap> /// update registry index for given project pub fn update_registry_index(registry: &Url, quiet: bool) -> CargoResult<()> { - let colorchoice = super::colorize_stderr(); - let mut output = StandardStream::stderr(colorchoice); - let mut index = crates_index::Index::from_url(registry.as_str())?; if !quiet { - output.set_color(ColorSpec::new().set_fg(Some(Color::Green)).set_bold(true))?; - write!(output, "{:>12}", "Updating")?; - output.reset()?; - writeln!(output, " '{}' index", registry)?; + shell_status("Updating", &format!("'{}' index", registry))?; } while need_retry(index.update())? { - registry_blocked_message(&mut output)?; + shell_status("Blocking", "waiting for lock on registry index")?; std::thread::sleep(REGISTRY_BACKOFF); } @@ -287,15 +281,6 @@ fn need_retry(res: Result<(), crates_index::Error>) -> CargoResult { } } -/// Report to user that the Registry is locked -fn registry_blocked_message(output: &mut StandardStream) -> CargoResult<()> { - output.set_color(ColorSpec::new().set_fg(Some(Color::Green)).set_bold(true))?; - write!(output, "{:>12}", "Blocking")?; - output.reset()?; - writeln!(output, " waiting for lock on registry index")?; - Ok(()) -} - /// Load Cargo.toml in a local path /// /// This will fail, when Cargo.toml is not present in the root of the path. diff --git a/src/lib.rs b/src/lib.rs index 6a29816512..97216e12b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,5 +36,5 @@ pub use fetch::{ pub use manifest::{find, LocalManifest, Manifest}; pub use metadata::{manifest_from_pkgid, workspace_members}; pub use registry::registry_url; -pub use util::{colorize_stderr, ColorChoice}; +pub use util::{colorize_stderr, shell_print, shell_status, shell_warn, Color, ColorChoice}; pub use version::{upgrade_requirement, VersionExt}; diff --git a/src/manifest.rs b/src/manifest.rs index dd7b1d6f0a..91bde9ac00 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -1,15 +1,14 @@ use std::collections::BTreeMap; use std::fs; -use std::io::Write; use std::ops::{Deref, DerefMut}; use std::path::{Path, PathBuf}; use std::{env, str}; use semver::{Version, VersionReq}; -use termcolor::{BufferWriter, Color, ColorSpec, WriteColor}; use super::dependency::Dependency; use super::errors::*; +use super::shell_status; const MANIFEST_FILENAME: &str = "Cargo.toml"; const DEP_TABLES: &[&str] = &["dependencies", "dev-dependencies", "build-dependencies"]; @@ -703,25 +702,10 @@ fn print_upgrade_if_necessary( return Ok(()); } - let colorchoice = super::colorize_stderr(); - let bufwtr = BufferWriter::stderr(colorchoice); - let mut buffer = bufwtr.buffer(); - buffer - .set_color(ColorSpec::new().set_fg(Some(Color::Green)).set_bold(true)) - .with_context(|| "Failed to set output colour")?; - write!(&mut buffer, " Upgrading ").with_context(|| "Failed to write upgrade message")?; - buffer - .set_color(&ColorSpec::new()) - .with_context(|| "Failed to clear output colour")?; - writeln!( - &mut buffer, - "{} v{} -> v{}", - crate_name, old_version, new_version, - ) - .with_context(|| "Failed to write upgrade versions")?; - bufwtr - .print(&buffer) - .with_context(|| "Failed to print upgrade message")?; + shell_status( + "Upgrading", + &format!("{crate_name} v{old_version} -> v{new_version}"), + )?; Ok(()) } diff --git a/src/util.rs b/src/util.rs index 31687a0ba2..88ddd6b1b4 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,9 @@ -pub use termcolor::ColorChoice; +use std::io::Write; + +pub use termcolor::{Color, ColorChoice}; +use termcolor::{ColorSpec, StandardStream, WriteColor}; + +use crate::{CargoResult, Context}; /// Whether to color logged output pub fn colorize_stderr() -> ColorChoice { @@ -8,3 +13,33 @@ pub fn colorize_stderr() -> ColorChoice { ColorChoice::Never } } + +/// Print a message with a colored title in the style of Cargo shell messages. +pub fn shell_print(status: &str, message: &str, color: Color, justified: bool) -> CargoResult<()> { + let color_choice = colorize_stderr(); + let mut output = StandardStream::stderr(color_choice); + + output.set_color(ColorSpec::new().set_fg(Some(color)).set_bold(true))?; + if justified { + write!(output, "{status:>12}")?; + } else { + write!(output, "{}", status)?; + output.set_color(ColorSpec::new().set_bold(true))?; + write!(output, ":")?; + } + output.reset()?; + + writeln!(output, " {message}").with_context(|| "Failed to write message")?; + + Ok(()) +} + +/// Print a styled action message. +pub fn shell_status(action: &str, message: &str) -> CargoResult<()> { + shell_print(action, message, Color::Green, true) +} + +/// Print a styled warning message. +pub fn shell_warn(message: &str) -> CargoResult<()> { + shell_print("warning", message, Color::Yellow, false) +}