Skip to content

Commit

Permalink
Auto merge of #12702 - Angelin01:refactor-cargo-add-features-display,…
Browse files Browse the repository at this point in the history
… r=epage

Cargo add displays either feature list or summarized count
  • Loading branch information
bors committed Sep 19, 2023
2 parents e4de526 + 72dccec commit 40b78fe
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 137 deletions.
97 changes: 47 additions & 50 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,72 +943,69 @@ fn print_dep_table_msg(shell: &mut Shell, dep: &DependencyUI) -> CargoResult<()>
if matches!(shell.verbosity(), crate::core::shell::Verbosity::Quiet) {
return Ok(());
}

let (activated, deactivated) = dep.features();
if !activated.is_empty() || !deactivated.is_empty() {
let prefix = format!("{:>13}", " ");
let suffix = if let Some(version) = &dep.available_version {
let mut version = version.clone();
version.build = Default::default();
let version = version.to_string();
// Avoid displaying the version if it will visually look like the version req that we
// showed earlier
let version_req = dep
.version()
.and_then(|v| semver::VersionReq::parse(v).ok())
.and_then(|v| precise_version(&v));
if version_req.as_deref() != Some(version.as_str()) {
format!(" as of v{version}")
} else {
"".to_owned()
}
} else {
"".to_owned()
};
let suffix = format_features_version_suffix(&dep);

shell.write_stderr(format_args!("{}Features{}:\n", prefix, suffix), &style::NOP)?;
shell.write_stderr(format_args!("{prefix}Features{suffix}:\n"), &style::NOP)?;

const MAX_FEATURE_PRINTS: usize = 50;

let mut activated_printed = 0;
const MAX_FEATURE_PRINTS: usize = 30;
let total_activated = activated.len();
for feat in activated {
if activated_printed >= MAX_FEATURE_PRINTS {
let remaining = total_activated - activated_printed;
shell.write_stderr(
format_args!("{prefix}... {remaining} more activated features\n"),
&style::NOP,
)?;
break;
}
let total_deactivated = deactivated.len();

shell.write_stderr(&prefix, &style::NOP)?;
shell.write_stderr('+', &style::GOOD)?;
shell.write_stderr(format_args!(" {}\n", feat), &style::NOP)?;
activated_printed += 1;
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)?;
}
} else {
shell.write_stderr(
format_args!("{prefix}{total_activated} activated features\n"),
&style::NOP,
)?;
}

let mut deactivated_printed = 0;
let total_deactivated = deactivated.len();
for feat in deactivated {
if activated_printed + deactivated_printed >= MAX_FEATURE_PRINTS {
let remaining = total_deactivated - deactivated_printed;
shell.write_stderr(
format_args!("{prefix}... {remaining} more deactivated features\n"),
&style::NOP,
)?;
break;
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)?;
}

shell.write_stderr(&prefix, &style::NOP)?;
shell.write_stderr('-', &style::ERROR)?;
shell.write_stderr(format_args!(" {}\n", feat), &style::NOP)?;
deactivated_printed += 1;
} else {
shell.write_stderr(
format_args!("{prefix}{total_deactivated} deactivated features\n"),
&style::NOP,
)?;
}
}

Ok(())
}

fn format_features_version_suffix(dep: &DependencyUI) -> String {
if let Some(version) = &dep.available_version {
let mut version = version.clone();
version.build = Default::default();
let version = version.to_string();
// Avoid displaying the version if it will visually look like the version req that we
// showed earlier
let version_req = dep
.version()
.and_then(|v| semver::VersionReq::parse(v).ok())
.and_then(|v| precise_version(&v));
if version_req.as_deref() != Some(version.as_str()) {
format!(" as of v{version}")
} else {
"".to_owned()
}
} else {
"".to_owned()
}
}

// Based on Iterator::is_sorted from nightly std; remove in favor of that when stabilized.
fn is_sorted(mut it: impl Iterator<Item = impl PartialOrd>) -> bool {
let Some(mut last) = it.next() else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Updating `dummy-registry` index
Adding your-face v99999.0.0 to dependencies.
Features:
100 activated features
100 deactivated features
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cargo_test_support::curr_dir;
#[cargo_test]
fn case() {
const MANY_FEATURES_COUNT: usize = 200;
const ACTIVATED_FEATURES_COUNT: usize = 10;
const ACTIVATED_FEATURES_COUNT: usize = 30;

cargo_test_support::registry::init();
let mut test_package =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[workspace]

[package]
name = "cargo-list-test-fixture"
version = "0.0.0"

[dependencies]
your-face = { version = "99999.0.0", features = ["eyes000", "eyes001", "eyes002", "eyes003", "eyes004", "eyes005", "eyes006", "eyes007", "eyes008", "eyes009", "eyes010", "eyes011", "eyes012", "eyes013", "eyes014", "eyes015", "eyes016", "eyes017", "eyes018", "eyes019", "eyes020", "eyes021", "eyes022", "eyes023", "eyes024", "eyes025", "eyes026", "eyes027", "eyes028", "eyes029"] }
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,4 @@
+ eyes027
+ eyes028
+ eyes029
+ eyes030
+ eyes031
+ eyes032
+ eyes033
+ eyes034
+ eyes035
+ eyes036
+ eyes037
+ eyes038
+ eyes039
+ eyes040
+ eyes041
+ eyes042
+ eyes043
+ eyes044
+ eyes045
+ eyes046
+ eyes047
+ eyes048
+ eyes049
... 50 more activated features
... 100 more deactivated features
170 deactivated features

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions tests/testsuite/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ mod dev_prefer_existing_version;
mod dry_run;
mod empty_dep_table;
mod features;
mod features_activated_over_limit;
mod features_deactivated_over_limit;
mod features_empty;
mod features_multiple_occurrences;
mod features_preserve;
mod features_spaced_values;
mod features_too_many_activated;
mod features_too_many_few_activated;
mod features_unknown;
mod features_unknown_no_features;
mod git;
Expand Down

0 comments on commit 40b78fe

Please sign in to comment.