Skip to content
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
49 changes: 36 additions & 13 deletions src/cargo/ops/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,26 +300,31 @@ fn print_node<'a>(
no_dedupe: bool,
display_depth: DisplayDepth,
visited_deps: &mut HashSet<NodeId>,
levels_continue: &mut Vec<bool>,
levels_continue: &mut Vec<(anstyle::Style, bool)>,
print_stack: &mut Vec<NodeId>,
) {
let new = no_dedupe || visited_deps.insert(node_index);

match prefix {
Prefix::Depth => drop_print!(ws.gctx(), "{}", levels_continue.len()),
Prefix::Indent => {
if let Some((last_continues, rest)) = levels_continue.split_last() {
for continues in rest {
if let Some(((last_style, last_continues), rest)) = levels_continue.split_last() {
for (style, continues) in rest {
let c = if *continues { symbols.down } else { " " };
drop_print!(ws.gctx(), "{} ", c);
drop_print!(ws.gctx(), "{style}{c}{style:#} ");
}

let c = if *last_continues {
symbols.tee
} else {
symbols.ell
};
drop_print!(ws.gctx(), "{0}{1}{1} ", c, symbols.right);
drop_print!(
ws.gctx(),
"{last_style}{0}{1}{1}{last_style:#} ",
c,
symbols.right
);
}
}
Prefix::None => {}
Expand All @@ -333,7 +338,7 @@ fn print_node<'a>(
let star = if (new && !in_cycle) || !has_deps {
""
} else {
" (*)"
color_print::cstr!(" <yellow,dim>(*)</>")
};
drop_println!(ws.gctx(), "{}{}", format.display(graph, node_index), star);

Expand Down Expand Up @@ -379,7 +384,7 @@ fn print_dependencies<'a>(
no_dedupe: bool,
display_depth: DisplayDepth,
visited_deps: &mut HashSet<NodeId>,
levels_continue: &mut Vec<bool>,
levels_continue: &mut Vec<(anstyle::Style, bool)>,
print_stack: &mut Vec<NodeId>,
kind: &EdgeKind,
) {
Expand All @@ -390,19 +395,23 @@ fn print_dependencies<'a>(

let name = match kind {
EdgeKind::Dep(DepKind::Normal) => None,
EdgeKind::Dep(DepKind::Build) => Some("[build-dependencies]"),
EdgeKind::Dep(DepKind::Development) => Some("[dev-dependencies]"),
EdgeKind::Dep(DepKind::Build) => {
Some(color_print::cstr!("<blue,bold>[build-dependencies]</>"))
}
EdgeKind::Dep(DepKind::Development) => {
Some(color_print::cstr!("<cyan,bold>[dev-dependencies]</>"))
}
EdgeKind::Feature => None,
};

if let Prefix::Indent = prefix {
if let Some(name) = name {
for continues in &**levels_continue {
for (style, continues) in &**levels_continue {
let c = if *continues { symbols.down } else { " " };
drop_print!(ws.gctx(), "{} ", c);
drop_print!(ws.gctx(), "{style}{c}{style:#} ");
}

drop_println!(ws.gctx(), "{}", name);
drop_println!(ws.gctx(), "{name}");
}
}

Expand Down Expand Up @@ -433,7 +442,8 @@ fn print_dependencies<'a>(
.peekable();

while let Some(dependency) = it.next() {
levels_continue.push(it.peek().is_some());
let style = edge_line_color(dependency.kind());
levels_continue.push((style, it.peek().is_some()));
print_node(
ws,
graph,
Expand All @@ -451,3 +461,16 @@ fn print_dependencies<'a>(
levels_continue.pop();
}
}

fn edge_line_color(kind: EdgeKind) -> anstyle::Style {
match kind {
EdgeKind::Dep(DepKind::Normal) => anstyle::Style::new() | anstyle::Effects::DIMMED,
EdgeKind::Dep(DepKind::Build) => {
anstyle::AnsiColor::Blue.on_default() | anstyle::Effects::BOLD
}
EdgeKind::Dep(DepKind::Development) => {
anstyle::AnsiColor::Cyan.on_default() | anstyle::Effects::BOLD
}
EdgeKind::Feature => anstyle::AnsiColor::Magenta.on_default() | anstyle::Effects::DIMMED,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cargo_test_support::registry::{Dependency, Package};
use cargo_test_support::str;
use cargo_test_support::{basic_manifest, git, project, rustc_host, Project};

use super::features2::switch_to_resolver_2;
use crate::features2::switch_to_resolver_2;

fn make_simple_proj() -> Project {
Package::new("c", "1.0.0").publish();
Expand Down
36 changes: 36 additions & 0 deletions tests/testsuite/cargo_tree/dupe/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::project;
use cargo_test_support::registry::Package;

#[cargo_test]
fn case() {
Package::new("a", "1.0.0").dep("b", "1.0").publish();
Package::new("b", "1.0.0").dep("c", "1.0").publish();
Package::new("c", "1.0.0").publish();

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"

[dependencies]
a = "1.0"
b = "1.0"
"#,
)
.file("src/lib.rs", "")
.file("build.rs", "fn main() {}")
.build();

snapbox::cmd::Command::cargo_ui()
.arg("tree")
.current_dir(p.root())
.assert()
.success()
.stdout_eq(file!["stdout.term.svg"])
.stderr_eq(file!["stderr.term.svg"]);
}
37 changes: 37 additions & 0 deletions tests/testsuite/cargo_tree/dupe/stderr.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions tests/testsuite/cargo_tree/dupe/stdout.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
100 changes: 100 additions & 0 deletions tests/testsuite/cargo_tree/edge_kind/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::project;
use cargo_test_support::registry::Package;

#[cargo_test]
fn case() {
Package::new("normal_a", "1.0.0")
.dep("normal_b", "1.0")
.publish();
Package::new("normal_b", "1.0.0")
.dep("normal_c", "1.0")
.build_dep("normal_b_build_a", "1.0.0")
.dev_dep("normal_b_dev_a", "1.0.0")
.publish();
Package::new("normal_c", "1.0.0").publish();
Package::new("normal_b_build_a", "1.0.0")
.dep("normal_b_build_a_normal_a", "1.0.0")
.publish();
Package::new("normal_b_build_a_normal_a", "1.0.0").publish();
Package::new("normal_b_dev_a", "1.0.0")
.dep("normal_b_dev_a_normal_a", "1.0.0")
.publish();
Package::new("normal_b_dev_a_normal_a", "1.0.0").publish();
Package::new("normal_d", "1.0.0").publish();

Package::new("build_a", "1.0.0")
.dep("build_b", "1.0")
.publish();
Package::new("build_b", "1.0.0")
.dep("build_c", "1.0")
.build_dep("build_b_build_a", "1.0.0")
.dev_dep("build_b_dev_a", "1.0.0")
.publish();
Package::new("build_c", "1.0.0").publish();
Package::new("build_b_build_a", "1.0.0")
.dep("build_b_build_a_normal_a", "1.0.0")
.publish();
Package::new("build_b_build_a_normal_a", "1.0.0").publish();
Package::new("build_b_dev_a", "1.0.0")
.dep("build_b_dev_a_normal_a", "1.0.0")
.publish();
Package::new("build_b_dev_a_normal_a", "1.0.0").publish();
Package::new("build_d", "1.0.0").publish();

Package::new("dev_a", "1.0.0").dep("dev_b", "1.0").publish();
Package::new("dev_b", "1.0.0")
.dep("dev_c", "1.0")
.build_dep("dev_b_build_a", "1.0.0")
.dev_dep("dev_b_dev_a", "1.0.0")
.publish();
Package::new("dev_c", "1.0.0").publish();
Package::new("dev_b_build_a", "1.0.0")
.dep("dev_b_build_a_normal_a", "1.0.0")
.publish();
Package::new("dev_b_build_a_normal_a", "1.0.0").publish();
Package::new("dev_b_dev_a", "1.0.0")
.dep("dev_b_dev_a_normal_a", "1.0.0")
.publish();
Package::new("dev_b_dev_a_normal_a", "1.0.0").publish();
Package::new("dev_d", "1.0.0").publish();

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"

[features]
default = ["foo"]
foo = ["dep:normal_a"]

[dependencies]
normal_a = { version = "1.0", optional = true }
normal_d = "1.0"

[build-dependencies]
build_a = "1.0"
build_d = "1.0"

[dev-dependencies]
dev_a = "1.0"
dev_d = "1.0"
"#,
)
.file("src/lib.rs", "")
.file("build.rs", "fn main() {}")
.build();

snapbox::cmd::Command::cargo_ui()
.arg("tree")
.arg("--edges=features")
.current_dir(p.root())
.assert()
.success()
.stdout_eq(file!["stdout.term.svg"])
.stderr_eq(file!["stderr.term.svg"]);
}
67 changes: 67 additions & 0 deletions tests/testsuite/cargo_tree/edge_kind/stderr.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading