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

misc: Use 'and' joined summary for install/uninstall #11925

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 6 additions & 3 deletions src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::ops::{common_for_install_and_uninstall::*, FilterRule};
use crate::ops::{CompileFilter, Packages};
use crate::sources::{GitSource, PathSource, SourceConfigMap};
use crate::util::errors::CargoResult;
use crate::util::{Config, Filesystem, Rustc, ToSemver, VersionReqExt};
use crate::util::{and_joined_words, Config, Filesystem, Rustc, ToSemver, VersionReqExt};
use crate::{drop_println, ops};

use anyhow::{bail, format_err, Context as _};
Expand Down Expand Up @@ -686,12 +686,15 @@ pub fn install(

let mut summary = vec![];
if !succeeded.is_empty() {
summary.push(format!("Successfully installed {}!", succeeded.join(", ")));
summary.push(format!(
"Successfully installed {}!",
and_joined_words(&succeeded)
));
}
if !failed.is_empty() {
summary.push(format!(
"Failed to install {} (see error(s) above).",
failed.join(", ")
and_joined_words(&failed)
));
}
if !succeeded.is_empty() || !failed.is_empty() {
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/ops/cargo_uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::core::{PackageIdSpec, SourceId};
use crate::ops::common_for_install_and_uninstall::*;
use crate::sources::PathSource;
use crate::util::errors::CargoResult;
use crate::util::Config;
use crate::util::Filesystem;
use crate::util::{and_joined_words, Config};
use anyhow::bail;
use cargo_util::paths;
use std::collections::BTreeSet;
Expand Down Expand Up @@ -45,13 +45,13 @@ pub fn uninstall(
if !succeeded.is_empty() {
summary.push(format!(
"Successfully uninstalled {}!",
succeeded.join(", ")
and_joined_words(&succeeded)
));
}
if !failed.is_empty() {
summary.push(format!(
"Failed to uninstall {} (see error(s) above).",
failed.join(", ")
and_joined_words(&failed)
));
}

Expand Down
32 changes: 32 additions & 0 deletions src/cargo/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ pub fn truncate_with_ellipsis(s: &str, max_width: usize) -> String {
prefix
}

/// Formats a slice of strings by joining them together using comma, "and" word
/// or both, depending on the number of words.
pub fn and_joined_words(words: &[&str]) -> String {
match words.len() {
0 => String::default(),
1 => words.first().map(|w| w.to_string()).unwrap(),
2 => words.join(" and "),
len => {
let (left, right) = words.split_at(len - 2);
format!("{}, {}", left.join(", "), right.join(" and "))
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -165,4 +179,22 @@ mod test {
);
assert_eq!(human_readable_bytes(u64::MAX), (16., "EiB"));
}

#[test]
fn test_and_joined_words() {
assert_eq!(and_joined_words(&[]), String::from(""));
assert_eq!(and_joined_words(&["foo"]), String::from("foo"));
assert_eq!(
and_joined_words(&["foo", "bar"]),
String::from("foo and bar")
);
assert_eq!(
and_joined_words(&["foo", "bar", "baz"]),
String::from("foo, bar and baz")
);
assert_eq!(
and_joined_words(&["foo", "bar", "baz", "qux"]),
String::from("foo, bar, baz and qux")
);
}
}
8 changes: 4 additions & 4 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn multiple_pkgs() {
[FINISHED] release [optimized] target(s) in [..]
[INSTALLING] [CWD]/home/.cargo/bin/bar[EXE]
[INSTALLED] package `bar v0.0.2` (executable `bar[EXE]`)
[SUMMARY] Successfully installed foo, bar! Failed to install baz (see error(s) above).
[SUMMARY] Successfully installed foo and bar! Failed to install baz (see error(s) above).
[WARNING] be sure to add `[..]` to your PATH to be able to run the installed binaries
[ERROR] some crates failed to install
",
Expand All @@ -203,7 +203,7 @@ fn multiple_pkgs() {
"\
[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]
[REMOVING] [CWD]/home/.cargo/bin/bar[EXE]
[SUMMARY] Successfully uninstalled foo, bar!
[SUMMARY] Successfully uninstalled foo and bar!
",
)
.run();
Expand Down Expand Up @@ -249,7 +249,7 @@ fn multiple_pkgs_path_set() {
[FINISHED] release [optimized] target(s) in [..]
[INSTALLING] [CWD]/home/.cargo/bin/bar[EXE]
[INSTALLED] package `bar v0.0.2` (executable `bar[EXE]`)
[SUMMARY] Successfully installed foo, bar! Failed to install baz (see error(s) above).
[SUMMARY] Successfully installed foo and bar! Failed to install baz (see error(s) above).
[ERROR] some crates failed to install
",
)
Expand All @@ -262,7 +262,7 @@ fn multiple_pkgs_path_set() {
"\
[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]
[REMOVING] [CWD]/home/.cargo/bin/bar[EXE]
[SUMMARY] Successfully uninstalled foo, bar!
[SUMMARY] Successfully uninstalled foo and bar!
",
)
.run();
Expand Down
6 changes: 3 additions & 3 deletions tests/testsuite/install_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ fn multiple_report() {
[INSTALLING] [..]/.cargo/bin/x[EXE]
[INSTALLING] [..]/.cargo/bin/y[EXE]
[INSTALLED] package `three v1.0.0` (executables `three[EXE]`, `x[EXE]`, `y[EXE]`)
[SUMMARY] Successfully installed one, two, three!
[SUMMARY] Successfully installed one, two and three!
[WARNING] be sure to add `[..]/.cargo/bin` to your PATH [..]
",
)
Expand All @@ -639,7 +639,7 @@ fn multiple_report() {
[REPLACING] [..]/.cargo/bin/x[EXE]
[REPLACING] [..]/.cargo/bin/y[EXE]
[REPLACED] package `three v1.0.0` with `three v1.0.1` (executables `three[EXE]`, `x[EXE]`, `y[EXE]`)
[SUMMARY] Successfully installed one, two, three!
[SUMMARY] Successfully installed one, two and three!
[WARNING] be sure to add `[..]/.cargo/bin` to your PATH [..]
",
)
Expand Down Expand Up @@ -854,7 +854,7 @@ fn partially_already_installed_does_one_update() {
[FINISHED] release [optimized] target(s) in [..]
[INSTALLING] [CWD]/home/.cargo/bin/baz[EXE]
[INSTALLED] package `baz v1.0.0` (executable `baz[EXE]`)
[SUMMARY] Successfully installed foo, bar, baz!
[SUMMARY] Successfully installed foo, bar and baz!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the test output, I feel like using and makes it harder to quickly scan. Using an oxford comma would help but that doesn't help with the foo and bar (2 item) case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, makes perfect sense, as it's subjective. I'm fine closing the PR, so no worries :)

[WARNING] be sure to add [..]
",
)
Expand Down