From 6ca939863fbd81cea0e70d40e110789e65e5b7f6 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 19 Jan 2024 16:44:37 -0600 Subject: [PATCH 1/5] feat(tree): Auto-detect '--charset' mode --- Cargo.lock | 10 +++ Cargo.toml | 1 + src/bin/cargo/commands/tree.rs | 16 ++++- src/doc/man/cargo-tree.md | 2 +- src/doc/man/generated_txt/cargo-tree.txt | 3 +- src/doc/src/commands/cargo-tree.md | 2 +- src/etc/man/cargo-tree.1 | 2 +- .../testsuite/cargo_tree/help/stdout.term.svg | 66 +++++++++---------- 8 files changed, 61 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ea8e4354359..70a087e0d8c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -336,6 +336,7 @@ dependencies = [ "shell-escape", "snapbox", "supports-hyperlinks", + "supports-unicode", "tar", "tempfile", "time", @@ -3243,6 +3244,15 @@ version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2c0a1e5168041f5f3ff68ff7d95dcb9c8749df29f6e7e89ada40dd4c9de404ee" +[[package]] +name = "supports-unicode" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f850c19edd184a205e883199a261ed44471c81e39bd95b1357f5febbef00e77a" +dependencies = [ + "is-terminal", +] + [[package]] name = "syn" version = "1.0.109" diff --git a/Cargo.toml b/Cargo.toml index b5b508c7b7f..cf40797619d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -204,6 +204,7 @@ unicase.workspace = true unicode-width.workspace = true url.workspace = true walkdir.workspace = true +supports-unicode = "2.1.0" [target.'cfg(target_has_atomic = "64")'.dependencies] tracing-chrome.workspace = true diff --git a/src/bin/cargo/commands/tree.rs b/src/bin/cargo/commands/tree.rs index aa1b526758d..ea862536c94 100644 --- a/src/bin/cargo/commands/tree.rs +++ b/src/bin/cargo/commands/tree.rs @@ -7,6 +7,7 @@ use cargo::ops::Packages; use cargo::util::print_available_packages; use cargo::util::CargoResult; use std::collections::HashSet; +use std::io::IsTerminal as _; use std::str::FromStr; pub fn cli() -> Command { @@ -69,8 +70,7 @@ pub fn cli() -> Command { .arg( opt("charset", "Character set to use in output") .value_name("CHARSET") - .value_parser(["utf8", "ascii"]) - .default_value("utf8"), + .value_parser(["utf8", "ascii"]), ) .arg( opt("format", "Format string used for printing dependencies") @@ -181,8 +181,18 @@ subtree of the package given to -p.\n\ print_available_packages(&ws)?; } - let charset = tree::Charset::from_str(args.get_one::("charset").unwrap()) + let charset = args.get_one::("charset"); + let charset = charset + .map(|c| tree::Charset::from_str(c)) + .transpose() .map_err(|e| anyhow::anyhow!("{}", e))?; + let charset = charset.unwrap_or_else(|| { + if supports_unicode::supports_unicode() || !std::io::stdout().is_terminal() { + tree::Charset::Utf8 + } else { + tree::Charset::Ascii + } + }); let opts = tree::TreeOptions { cli_features: args.cli_features()?, packages, diff --git a/src/doc/man/cargo-tree.md b/src/doc/man/cargo-tree.md index 1bb52883c11..f8773430c16 100644 --- a/src/doc/man/cargo-tree.md +++ b/src/doc/man/cargo-tree.md @@ -150,7 +150,7 @@ The default is the host platform. Use the value `all` to include *all* targets. {{#option "`--charset` _charset_" }} Chooses the character set to use for the tree. Valid values are "utf8" or -"ascii". Default is "utf8". +"ascii". When unspecified, cargo will auto-select a value. {{/option}} {{#option "`-f` _format_" "`--format` _format_" }} diff --git a/src/doc/man/generated_txt/cargo-tree.txt b/src/doc/man/generated_txt/cargo-tree.txt index 5b81f0aa1f3..c0fa4485f0a 100644 --- a/src/doc/man/generated_txt/cargo-tree.txt +++ b/src/doc/man/generated_txt/cargo-tree.txt @@ -141,7 +141,8 @@ OPTIONS Tree Formatting Options --charset charset Chooses the character set to use for the tree. Valid values are - “utf8” or “ascii”. Default is “utf8”. + “utf8” or “ascii”. When unspecified, cargo will auto-select + a value. -f format, --format format Set the format string for each package. The default is “{p}”. diff --git a/src/doc/src/commands/cargo-tree.md b/src/doc/src/commands/cargo-tree.md index ec930581706..a859f27433a 100644 --- a/src/doc/src/commands/cargo-tree.md +++ b/src/doc/src/commands/cargo-tree.md @@ -146,7 +146,7 @@ The default is the host platform. Use the value all to include
--charset charset
Chooses the character set to use for the tree. Valid values are “utf8” or -“ascii”. Default is “utf8”.
+“ascii”. When unspecified, cargo will auto-select a value.
-f format
diff --git a/src/etc/man/cargo-tree.1 b/src/etc/man/cargo-tree.1 index 2fab70be971..7861c28fbb4 100644 --- a/src/etc/man/cargo-tree.1 +++ b/src/etc/man/cargo-tree.1 @@ -175,7 +175,7 @@ The default is the host platform. Use the value \fBall\fR to include \fIall\fR t \fB\-\-charset\fR \fIcharset\fR .RS 4 Chooses the character set to use for the tree. Valid values are \[lq]utf8\[rq] or -\[lq]ascii\[rq]\&. Default is \[lq]utf8\[rq]\&. +\[lq]ascii\[rq]\&. When unspecified, cargo will auto\-select a value. .RE .sp \fB\-f\fR \fIformat\fR, diff --git a/tests/testsuite/cargo_tree/help/stdout.term.svg b/tests/testsuite/cargo_tree/help/stdout.term.svg index c7ab6b80047..984758e3916 100644 --- a/tests/testsuite/cargo_tree/help/stdout.term.svg +++ b/tests/testsuite/cargo_tree/help/stdout.term.svg @@ -1,4 +1,4 @@ - +