Skip to content

Commit 7cbc365

Browse files
committed
feat: new --depth option for cargo tree
1 parent 44596fa commit 7cbc365

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/bin/cargo/commands/tree.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub fn cli() -> App {
5555
)
5656
.short("i"),
5757
)
58+
.arg(opt("depth", "Maximum display depth of the dependency tree").value_name("DEPTH"))
5859
// Deprecated, use --prefix=none instead.
5960
.arg(Arg::with_name("no-indent").long("no-indent").hidden(true))
6061
// Deprecated, use --prefix=depth instead.
@@ -201,6 +202,7 @@ subtree of the package given to -p.\n\
201202
charset,
202203
format: args.value_of("format").unwrap().to_string(),
203204
graph_features,
205+
max_display_depth: args.value_of_u32("depth")?.unwrap_or(u32::MAX),
204206
};
205207

206208
tree::build_and_print(&ws, &opts)?;

src/cargo/ops/tree/mod.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ pub struct TreeOptions {
4343
pub format: String,
4444
/// Includes features in the tree as separate nodes.
4545
pub graph_features: bool,
46+
/// Maximum display depth of the dependency tree.
47+
pub max_display_depth: u32,
4648
}
4749

4850
#[derive(PartialEq)]
@@ -241,6 +243,7 @@ fn print(
241243
symbols,
242244
opts.prefix,
243245
opts.no_dedupe,
246+
opts.max_display_depth,
244247
&mut visited_deps,
245248
&mut levels_continue,
246249
&mut print_stack,
@@ -259,6 +262,7 @@ fn print_node<'a>(
259262
symbols: &Symbols,
260263
prefix: Prefix,
261264
no_dedupe: bool,
265+
max_display_depth: u32,
262266
visited_deps: &mut HashSet<usize>,
263267
levels_continue: &mut Vec<bool>,
264268
print_stack: &mut Vec<usize>,
@@ -316,6 +320,7 @@ fn print_node<'a>(
316320
symbols,
317321
prefix,
318322
no_dedupe,
323+
max_display_depth,
319324
visited_deps,
320325
levels_continue,
321326
print_stack,
@@ -334,6 +339,7 @@ fn print_dependencies<'a>(
334339
symbols: &Symbols,
335340
prefix: Prefix,
336341
no_dedupe: bool,
342+
max_display_depth: u32,
337343
visited_deps: &mut HashSet<usize>,
338344
levels_continue: &mut Vec<bool>,
339345
print_stack: &mut Vec<usize>,
@@ -364,19 +370,22 @@ fn print_dependencies<'a>(
364370

365371
let mut it = deps.iter().peekable();
366372
while let Some(dependency) = it.next() {
367-
levels_continue.push(it.peek().is_some());
368-
print_node(
369-
config,
370-
graph,
371-
*dependency,
372-
format,
373-
symbols,
374-
prefix,
375-
no_dedupe,
376-
visited_deps,
377-
levels_continue,
378-
print_stack,
379-
);
380-
levels_continue.pop();
373+
if levels_continue.len() + 1 <= max_display_depth as usize {
374+
levels_continue.push(it.peek().is_some());
375+
print_node(
376+
config,
377+
graph,
378+
*dependency,
379+
format,
380+
symbols,
381+
prefix,
382+
no_dedupe,
383+
max_display_depth,
384+
visited_deps,
385+
levels_continue,
386+
print_stack,
387+
);
388+
levels_continue.pop();
389+
}
381390
}
382391
}

0 commit comments

Comments
 (0)