Skip to content

Commit a9ff02e

Browse files
committed
Change --no-indent and --prefix-depth to a single --prefix option.
1 parent e1c95e2 commit a9ff02e

File tree

6 files changed

+103
-46
lines changed

6 files changed

+103
-46
lines changed

src/bin/cargo/commands/tree.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,21 @@ pub fn cli() -> App {
2222
))
2323
.arg(opt("no-dev-dependencies", "Skip dev dependencies"))
2424
.arg(opt("invert", "Invert the tree direction").short("i"))
25-
.arg(opt(
26-
"no-indent",
27-
"Display the dependencies as a list (rather than a tree)",
28-
))
29-
.arg(opt(
30-
"prefix-depth",
31-
"Display the dependencies as a list (rather than a tree), but prefixed with the depth",
32-
))
25+
.arg(Arg::with_name("no-indent").long("no-indent").hidden(true))
26+
.arg(
27+
Arg::with_name("prefix-depth")
28+
.long("prefix-depth")
29+
.hidden(true),
30+
)
31+
.arg(
32+
opt(
33+
"prefix",
34+
"Change the prefix (indentation) of how each entry is displayed",
35+
)
36+
.value_name("PREFIX")
37+
.possible_values(&["depth", "indent", "none"])
38+
.default_value("indent"),
39+
)
3340
.arg(opt(
3441
"no-dedupe",
3542
"Do not de-duplicate (repeats all shared dependencies)",
@@ -58,9 +65,22 @@ pub fn cli() -> App {
5865
}
5966

6067
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
68+
if args.is_present("no-indent") {
69+
return Err(
70+
anyhow::format_err!("the --no-indent flag has been changed to --prefix=none").into(),
71+
);
72+
}
73+
if args.is_present("prefix-depth") {
74+
return Err(anyhow::format_err!(
75+
"the --prefix-depth flag has been changed to --prefix=depth"
76+
)
77+
.into());
78+
}
6179
let ws = args.workspace(config)?;
6280
let charset = tree::Charset::from_str(args.value_of("charset").unwrap())
6381
.map_err(|e| anyhow::anyhow!("{}", e))?;
82+
let prefix = tree::Prefix::from_str(args.value_of("prefix").unwrap())
83+
.map_err(|e| anyhow::anyhow!("{}", e))?;
6484
let opts = tree::TreeOptions {
6585
features: values(args, "features"),
6686
all_features: args.is_present("all-features"),
@@ -70,8 +90,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
7090
no_filter_targets: args.is_present("no-filter-targets"),
7191
no_dev_dependencies: args.is_present("no-dev-dependencies"),
7292
invert: args.is_present("invert"),
73-
no_indent: args.is_present("no-indent"),
74-
prefix_depth: args.is_present("prefix-depth"),
93+
prefix,
7594
no_dedupe: args.is_present("no-dedupe"),
7695
duplicates: args.is_present("duplicates"),
7796
charset,

src/cargo/ops/tree/mod.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ pub struct TreeOptions {
3030
pub no_filter_targets: bool,
3131
pub no_dev_dependencies: bool,
3232
pub invert: bool,
33-
/// Displays a list, with no indentation.
34-
pub no_indent: bool,
35-
/// Displays a list, with a number indicating the depth instead of using indentation.
36-
pub prefix_depth: bool,
33+
/// The style of prefix for each line.
34+
pub prefix: Prefix,
3735
/// If `true`, duplicates will be repeated.
3836
/// If `false`, duplicates will be marked with `*`, and their dependencies
3937
/// won't be shown.
@@ -68,12 +66,25 @@ impl FromStr for Charset {
6866
}
6967

7068
#[derive(Clone, Copy)]
71-
enum Prefix {
69+
pub enum Prefix {
7270
None,
7371
Indent,
7472
Depth,
7573
}
7674

75+
impl FromStr for Prefix {
76+
type Err = &'static str;
77+
78+
fn from_str(s: &str) -> Result<Prefix, &'static str> {
79+
match s {
80+
"none" => Ok(Prefix::None),
81+
"indent" => Ok(Prefix::Indent),
82+
"depth" => Ok(Prefix::Depth),
83+
_ => Err("invalid prefix"),
84+
}
85+
}
86+
}
87+
7788
struct Symbols {
7889
down: &'static str,
7990
tee: &'static str,
@@ -174,14 +185,6 @@ fn print(opts: &TreeOptions, roots: Vec<usize>, graph: &Graph<'_>) -> CargoResul
174185
Charset::Ascii => &ASCII_SYMBOLS,
175186
};
176187

177-
let prefix = if opts.prefix_depth {
178-
Prefix::Depth
179-
} else if opts.no_indent {
180-
Prefix::None
181-
} else {
182-
Prefix::Indent
183-
};
184-
185188
// The visited deps is used to display a (*) whenever a dep has
186189
// already been printed (ignored with --no-dedupe).
187190
let mut visited_deps = HashSet::new();
@@ -203,7 +206,7 @@ fn print(opts: &TreeOptions, roots: Vec<usize>, graph: &Graph<'_>) -> CargoResul
203206
root_index,
204207
&format,
205208
symbols,
206-
prefix,
209+
opts.prefix,
207210
opts.no_dedupe,
208211
&mut visited_deps,
209212
&mut levels_continue,

src/doc/man/cargo-tree.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ strings will be replaced with the corresponding value:
9999
- `{r}` — The package repository URL.
100100
- `{f}` — Comma-separated list of package features that are enabled.
101101

102-
*--no-indent*::
103-
Display the dependencies as a list (rather than a tree).
104-
105-
*--prefix-depth*::
106-
Display the dependencies as a list (rather than a tree), but prefixed with
107-
the depth.
102+
*--prefix* _PREFIX_::
103+
Sets how each line is displayed. The _PREFIX_ value can be one of:
104+
+
105+
- `indent` (default) — Shows each line indented as a tree.
106+
- `depth` — Show as a list, with the numeric depth printed before each entry.
107+
- `none` — Show as a flat list.
108108

109109
=== Package Selection
110110

src/doc/man/generated/cargo-tree.html

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,22 @@ <h3 id="cargo_tree_tree_formatting_options">Tree Formatting Options</h3>
133133
</ul>
134134
</div>
135135
</dd>
136-
<dt class="hdlist1"><strong>--no-indent</strong></dt>
136+
<dt class="hdlist1"><strong>--prefix</strong> <em>PREFIX</em></dt>
137137
<dd>
138-
<p>Display the dependencies as a list (rather than a tree).</p>
139-
</dd>
140-
<dt class="hdlist1"><strong>--prefix-depth</strong></dt>
141-
<dd>
142-
<p>Display the dependencies as a list (rather than a tree), but prefixed with
143-
the depth.</p>
138+
<p>Sets how each line is displayed. The <em>PREFIX</em> value can be one of:</p>
139+
<div class="ulist">
140+
<ul>
141+
<li>
142+
<p><code>indent</code> (default) — Shows each line indented as a tree.</p>
143+
</li>
144+
<li>
145+
<p><code>depth</code> — Show as a list, with the numeric depth printed before each entry.</p>
146+
</li>
147+
<li>
148+
<p><code>none</code> — Show as a flat list.</p>
149+
</li>
150+
</ul>
151+
</div>
144152
</dd>
145153
</dl>
146154
</div>

src/etc/man/cargo-tree.1

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
.\" Title: cargo-tree
33
.\" Author: [see the "AUTHOR(S)" section]
44
.\" Generator: Asciidoctor 2.0.10
5-
.\" Date: 2020-03-30
5+
.\" Date: 2020-04-02
66
.\" Manual: \ \&
77
.\" Source: \ \&
88
.\" Language: English
99
.\"
10-
.TH "CARGO\-TREE" "1" "2020-03-30" "\ \&" "\ \&"
10+
.TH "CARGO\-TREE" "1" "2020-04-02" "\ \&" "\ \&"
1111
.ie \n(.g .ds Aq \(aq
1212
.el .ds Aq '
1313
.ss \n[.ss] 0
@@ -172,15 +172,42 @@ strings will be replaced with the corresponding value:
172172
.RE
173173
.RE
174174
.sp
175-
\fB\-\-no\-indent\fP
175+
\fB\-\-prefix\fP \fIPREFIX\fP
176176
.RS 4
177-
Display the dependencies as a list (rather than a tree).
177+
Sets how each line is displayed. The \fIPREFIX\fP value can be one of:
178+
.sp
179+
.RS 4
180+
.ie n \{\
181+
\h'-04'\(bu\h'+03'\c
182+
.\}
183+
.el \{\
184+
. sp -1
185+
. IP \(bu 2.3
186+
.\}
187+
\fBindent\fP (default) — Shows each line indented as a tree.
178188
.RE
179189
.sp
180-
\fB\-\-prefix\-depth\fP
181190
.RS 4
182-
Display the dependencies as a list (rather than a tree), but prefixed with
183-
the depth.
191+
.ie n \{\
192+
\h'-04'\(bu\h'+03'\c
193+
.\}
194+
.el \{\
195+
. sp -1
196+
. IP \(bu 2.3
197+
.\}
198+
\fBdepth\fP — Show as a list, with the numeric depth printed before each entry.
199+
.RE
200+
.sp
201+
.RS 4
202+
.ie n \{\
203+
\h'-04'\(bu\h'+03'\c
204+
.\}
205+
.el \{\
206+
. sp -1
207+
. IP \(bu 2.3
208+
.\}
209+
\fBnone\fP — Show as a flat list.
210+
.RE
184211
.RE
185212
.SS "Package Selection"
186213
.sp

tests/testsuite/tree.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ common v1.0.0
630630
fn no_indent() {
631631
let p = make_simple_proj();
632632

633-
p.cargo("tree --no-indent")
633+
p.cargo("tree --prefix=none")
634634
.with_stdout(
635635
"\
636636
foo v0.1.0 ([..]/foo)
@@ -651,7 +651,7 @@ b v1.0.0 (*)
651651
fn prefix_depth() {
652652
let p = make_simple_proj();
653653

654-
p.cargo("tree --prefix-depth")
654+
p.cargo("tree --prefix=depth")
655655
.with_stdout(
656656
"\
657657
0foo v0.1.0 ([..]/foo)

0 commit comments

Comments
 (0)