diff --git a/Cargo.lock b/Cargo.lock index c0a7cc9b9d37..b98934fdf22b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -314,6 +314,7 @@ dependencies = [ "shell-escape", "snapbox", "supports-hyperlinks", + "supports-unicode", "tar", "tempfile", "time", @@ -3199,6 +3200,15 @@ dependencies = [ "is-terminal", ] +[[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 0a62314f057e..f3ddcb30c0ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -203,6 +203,7 @@ unicase.workspace = true unicode-width.workspace = true url.workspace = true walkdir.workspace = true +supports-unicode = "2.1.0" [target.'cfg(target_os = "linux")'.dependencies] cargo-credential-libsecret.workspace = true diff --git a/src/bin/cargo/commands/tree.rs b/src/bin/cargo/commands/tree.rs index 30cf4fe3a2d7..5a9dd23843fd 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") @@ -184,8 +184,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 1bb52883c11c..f8773430c16e 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 5b81f0aa1f3d..c0fa4485f0ae 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 2bbe546da67d..f945dee19183 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 2abad97320b8..e30edaace445 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.log b/tests/testsuite/cargo_tree/help/stdout.log index 9865fd59e283..b3f57e85cfe5 100644 --- a/tests/testsuite/cargo_tree/help/stdout.log +++ b/tests/testsuite/cargo_tree/help/stdout.log @@ -12,8 +12,7 @@ Options: indent] [possible values: depth, indent, none] --no-dedupe Do not de-duplicate (repeats all shared dependencies) -d, --duplicates Show only dependencies which come in multiple versions (implies -i) - --charset Character set to use in output [default: utf8] [possible values: utf8, - ascii] + --charset Character set to use in output [possible values: utf8, ascii] -f, --format Format string used for printing dependencies [default: {p}] -v, --verbose... Use verbose output (-vv very verbose/build.rs output) -q, --quiet Do not print cargo log messages