Skip to content

Commit d8eb809

Browse files
committed
feat(tree): Color lines by dependency type
1 parent 50d88f4 commit d8eb809

File tree

3 files changed

+70
-42
lines changed

3 files changed

+70
-42
lines changed

src/cargo/ops/tree/mod.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,26 +300,31 @@ fn print_node<'a>(
300300
no_dedupe: bool,
301301
display_depth: DisplayDepth,
302302
visited_deps: &mut HashSet<NodeId>,
303-
levels_continue: &mut Vec<bool>,
303+
levels_continue: &mut Vec<(anstyle::Style, bool)>,
304304
print_stack: &mut Vec<NodeId>,
305305
) {
306306
let new = no_dedupe || visited_deps.insert(node_index);
307307

308308
match prefix {
309309
Prefix::Depth => drop_print!(ws.gctx(), "{}", levels_continue.len()),
310310
Prefix::Indent => {
311-
if let Some((last_continues, rest)) = levels_continue.split_last() {
312-
for continues in rest {
311+
if let Some(((last_style, last_continues), rest)) = levels_continue.split_last() {
312+
for (style, continues) in rest {
313313
let c = if *continues { symbols.down } else { " " };
314-
drop_print!(ws.gctx(), "{} ", c);
314+
drop_print!(ws.gctx(), "{style}{c}{style:#} ");
315315
}
316316

317317
let c = if *last_continues {
318318
symbols.tee
319319
} else {
320320
symbols.ell
321321
};
322-
drop_print!(ws.gctx(), "{0}{1}{1} ", c, symbols.right);
322+
drop_print!(
323+
ws.gctx(),
324+
"{last_style}{0}{1}{1}{last_style:#} ",
325+
c,
326+
symbols.right
327+
);
323328
}
324329
}
325330
Prefix::None => {}
@@ -333,7 +338,7 @@ fn print_node<'a>(
333338
let star = if (new && !in_cycle) || !has_deps {
334339
""
335340
} else {
336-
color_print::cstr!(" <yellow,bold>(*)</>")
341+
color_print::cstr!(" <yellow,dim>(*)</>")
337342
};
338343
drop_println!(ws.gctx(), "{}{}", format.display(graph, node_index), star);
339344

@@ -379,7 +384,7 @@ fn print_dependencies<'a>(
379384
no_dedupe: bool,
380385
display_depth: DisplayDepth,
381386
visited_deps: &mut HashSet<NodeId>,
382-
levels_continue: &mut Vec<bool>,
387+
levels_continue: &mut Vec<(anstyle::Style, bool)>,
383388
print_stack: &mut Vec<NodeId>,
384389
kind: &EdgeKind,
385390
) {
@@ -390,19 +395,23 @@ fn print_dependencies<'a>(
390395

391396
let name = match kind {
392397
EdgeKind::Dep(DepKind::Normal) => None,
393-
EdgeKind::Dep(DepKind::Build) => Some("[build-dependencies]"),
394-
EdgeKind::Dep(DepKind::Development) => Some("[dev-dependencies]"),
398+
EdgeKind::Dep(DepKind::Build) => {
399+
Some(color_print::cstr!("<blue,bold>[build-dependencies]</>"))
400+
}
401+
EdgeKind::Dep(DepKind::Development) => {
402+
Some(color_print::cstr!("<cyan,bold>[dev-dependencies]</>"))
403+
}
395404
EdgeKind::Feature => None,
396405
};
397406

398407
if let Prefix::Indent = prefix {
399408
if let Some(name) = name {
400-
for continues in &**levels_continue {
409+
for (style, continues) in &**levels_continue {
401410
let c = if *continues { symbols.down } else { " " };
402-
drop_print!(ws.gctx(), "{} ", c);
411+
drop_print!(ws.gctx(), "{style}{c}{style:#} ");
403412
}
404413

405-
drop_println!(ws.gctx(), "{}", name);
414+
drop_println!(ws.gctx(), "{name}");
406415
}
407416
}
408417

@@ -433,7 +442,8 @@ fn print_dependencies<'a>(
433442
.peekable();
434443

435444
while let Some(dependency) = it.next() {
436-
levels_continue.push(it.peek().is_some());
445+
let style = edge_line_color(dependency.kind());
446+
levels_continue.push((style, it.peek().is_some()));
437447
print_node(
438448
ws,
439449
graph,
@@ -451,3 +461,16 @@ fn print_dependencies<'a>(
451461
levels_continue.pop();
452462
}
453463
}
464+
465+
fn edge_line_color(kind: EdgeKind) -> anstyle::Style {
466+
match kind {
467+
EdgeKind::Dep(DepKind::Normal) => anstyle::Style::new() | anstyle::Effects::DIMMED,
468+
EdgeKind::Dep(DepKind::Build) => {
469+
anstyle::AnsiColor::Blue.on_default() | anstyle::Effects::BOLD
470+
}
471+
EdgeKind::Dep(DepKind::Development) => {
472+
anstyle::AnsiColor::Cyan.on_default() | anstyle::Effects::BOLD
473+
}
474+
EdgeKind::Feature => anstyle::AnsiColor::Magenta.on_default() | anstyle::Effects::DIMMED,
475+
}
476+
}

tests/testsuite/cargo_tree/dupe/stdout.term.svg

Lines changed: 5 additions & 5 deletions
Loading

tests/testsuite/cargo_tree/edge_kind/stdout.term.svg

Lines changed: 29 additions & 24 deletions
Loading

0 commit comments

Comments
 (0)