Skip to content

Commit

Permalink
feat(tree): Auto-detect '--charset' mode
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 19, 2024
1 parent 63f4af9 commit 0142b6c
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 9 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions src/bin/cargo/commands/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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::<String>("charset").unwrap())
let charset = args.get_one::<String>("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,
Expand Down
2 changes: 1 addition & 1 deletion src/doc/man/cargo-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_" }}
Expand Down
3 changes: 2 additions & 1 deletion src/doc/man/generated_txt/cargo-tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}”.
Expand Down
2 changes: 1 addition & 1 deletion src/doc/src/commands/cargo-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ The default is the host platform. Use the value <code>all</code> to include <em>

<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>
<dd class="option-desc">Chooses the character set to use for the tree. Valid values are “utf8” or
“ascii”. Default is “utf8”.</dd>
“ascii”. When unspecified, cargo will auto-select a value.</dd>


<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>
Expand Down
2 changes: 1 addition & 1 deletion src/etc/man/cargo-tree.1
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions tests/testsuite/cargo_tree/help/stdout.log
Original file line number Diff line number Diff line change
Expand Up @@ -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 <CHARSET> Character set to use in output [default: utf8] [possible values: utf8,
ascii]
--charset <CHARSET> Character set to use in output [possible values: utf8, ascii]
-f, --format <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
Expand Down

0 comments on commit 0142b6c

Please sign in to comment.