Skip to content

Commit 6ca9398

Browse files
committed
feat(tree): Auto-detect '--charset' mode
1 parent 403fbe2 commit 6ca9398

File tree

8 files changed

+61
-41
lines changed

8 files changed

+61
-41
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ unicase.workspace = true
204204
unicode-width.workspace = true
205205
url.workspace = true
206206
walkdir.workspace = true
207+
supports-unicode = "2.1.0"
207208

208209
[target.'cfg(target_has_atomic = "64")'.dependencies]
209210
tracing-chrome.workspace = true

src/bin/cargo/commands/tree.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use cargo::ops::Packages;
77
use cargo::util::print_available_packages;
88
use cargo::util::CargoResult;
99
use std::collections::HashSet;
10+
use std::io::IsTerminal as _;
1011
use std::str::FromStr;
1112

1213
pub fn cli() -> Command {
@@ -69,8 +70,7 @@ pub fn cli() -> Command {
6970
.arg(
7071
opt("charset", "Character set to use in output")
7172
.value_name("CHARSET")
72-
.value_parser(["utf8", "ascii"])
73-
.default_value("utf8"),
73+
.value_parser(["utf8", "ascii"]),
7474
)
7575
.arg(
7676
opt("format", "Format string used for printing dependencies")
@@ -181,8 +181,18 @@ subtree of the package given to -p.\n\
181181
print_available_packages(&ws)?;
182182
}
183183

184-
let charset = tree::Charset::from_str(args.get_one::<String>("charset").unwrap())
184+
let charset = args.get_one::<String>("charset");
185+
let charset = charset
186+
.map(|c| tree::Charset::from_str(c))
187+
.transpose()
185188
.map_err(|e| anyhow::anyhow!("{}", e))?;
189+
let charset = charset.unwrap_or_else(|| {
190+
if supports_unicode::supports_unicode() || !std::io::stdout().is_terminal() {
191+
tree::Charset::Utf8
192+
} else {
193+
tree::Charset::Ascii
194+
}
195+
});
186196
let opts = tree::TreeOptions {
187197
cli_features: args.cli_features()?,
188198
packages,

src/doc/man/cargo-tree.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ The default is the host platform. Use the value `all` to include *all* targets.
150150

151151
{{#option "`--charset` _charset_" }}
152152
Chooses the character set to use for the tree. Valid values are "utf8" or
153-
"ascii". Default is "utf8".
153+
"ascii". When unspecified, cargo will auto-select a value.
154154
{{/option}}
155155

156156
{{#option "`-f` _format_" "`--format` _format_" }}

src/doc/man/generated_txt/cargo-tree.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ OPTIONS
141141
Tree Formatting Options
142142
--charset charset
143143
Chooses the character set to use for the tree. Valid values are
144-
“utf8” or “ascii”. Default is “utf8”.
144+
“utf8” or “ascii”. When unspecified, cargo will auto-select
145+
a value.
145146

146147
-f format, --format format
147148
Set the format string for each package. The default is “{p}”.

src/doc/src/commands/cargo-tree.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ The default is the host platform. Use the value <code>all</code> to include <em>
146146

147147
<dt class="option-term" id="option-cargo-tree---charset"><a class="option-anchor" href="#option-cargo-tree---charset"></a><code>--charset</code> <em>charset</em></dt>
148148
<dd class="option-desc">Chooses the character set to use for the tree. Valid values are “utf8” or
149-
“ascii”. Default is “utf8”.</dd>
149+
“ascii”. When unspecified, cargo will auto-select a value.</dd>
150150

151151

152152
<dt class="option-term" id="option-cargo-tree--f"><a class="option-anchor" href="#option-cargo-tree--f"></a><code>-f</code> <em>format</em></dt>

src/etc/man/cargo-tree.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ The default is the host platform. Use the value \fBall\fR to include \fIall\fR t
175175
\fB\-\-charset\fR \fIcharset\fR
176176
.RS 4
177177
Chooses the character set to use for the tree. Valid values are \[lq]utf8\[rq] or
178-
\[lq]ascii\[rq]\&. Default is \[lq]utf8\[rq]\&.
178+
\[lq]ascii\[rq]\&. When unspecified, cargo will auto\-select a value.
179179
.RE
180180
.sp
181181
\fB\-f\fR \fIformat\fR,

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

Lines changed: 32 additions & 34 deletions
Loading

0 commit comments

Comments
 (0)