Skip to content

Commit d000f25

Browse files
committed
refactor(tree): Pass --charset through config
1 parent bdd30e7 commit d000f25

File tree

2 files changed

+31
-34
lines changed

2 files changed

+31
-34
lines changed

src/bin/cargo/commands/tree.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,24 @@ pub fn cli() -> Command {
100100
))
101101
}
102102

103+
#[derive(Copy, Clone)]
104+
pub enum Charset {
105+
Utf8,
106+
Ascii,
107+
}
108+
109+
impl FromStr for Charset {
110+
type Err = &'static str;
111+
112+
fn from_str(s: &str) -> Result<Charset, &'static str> {
113+
match s {
114+
"utf8" => Ok(Charset::Utf8),
115+
"ascii" => Ok(Charset::Ascii),
116+
_ => Err("invalid charset"),
117+
}
118+
}
119+
}
120+
103121
pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
104122
if args.flag("version") {
105123
let verbose = args.verbose() > 0;
@@ -181,10 +199,16 @@ subtree of the package given to -p.\n\
181199
}
182200

183201
let charset = args.get_one::<String>("charset");
184-
let charset = charset
185-
.map(|c| tree::Charset::from_str(c))
202+
if let Some(charset) = charset
203+
.map(|c| Charset::from_str(c))
186204
.transpose()
187-
.map_err(|e| anyhow::anyhow!("{}", e))?;
205+
.map_err(|e| anyhow::anyhow!("{}", e))?
206+
{
207+
match charset {
208+
Charset::Utf8 => gctx.shell().set_unicode(true)?,
209+
Charset::Ascii => gctx.shell().set_unicode(false)?,
210+
}
211+
}
188212
let opts = tree::TreeOptions {
189213
cli_features: args.cli_features()?,
190214
packages,
@@ -195,7 +219,6 @@ subtree of the package given to -p.\n\
195219
prefix,
196220
no_dedupe,
197221
duplicates: args.flag("duplicates"),
198-
charset,
199222
format: args.get_one::<String>("format").cloned().unwrap(),
200223
graph_features,
201224
max_display_depth: args.value_of_u32("depth")?.unwrap_or(u32::MAX),

src/cargo/ops/tree/mod.rs

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ pub struct TreeOptions {
3939
/// appear with different versions, and report if any where found. Implies
4040
/// `invert`.
4141
pub duplicates: bool,
42-
/// The style of characters to use.
43-
pub charset: Option<Charset>,
4442
/// A format string indicating how each package should be displayed.
4543
pub format: String,
4644
/// Includes features in the tree as separate nodes.
@@ -68,24 +66,6 @@ impl Target {
6866
}
6967
}
7068

71-
#[derive(Copy, Clone)]
72-
pub enum Charset {
73-
Utf8,
74-
Ascii,
75-
}
76-
77-
impl FromStr for Charset {
78-
type Err = &'static str;
79-
80-
fn from_str(s: &str) -> Result<Charset, &'static str> {
81-
match s {
82-
"utf8" => Ok(Charset::Utf8),
83-
"ascii" => Ok(Charset::Ascii),
84-
_ => Err("invalid charset"),
85-
}
86-
}
87-
}
88-
8969
#[derive(Clone, Copy)]
9070
pub enum Prefix {
9171
None,
@@ -237,16 +217,10 @@ fn print(
237217
let format = Pattern::new(&opts.format)
238218
.with_context(|| format!("tree format `{}` not valid", opts.format))?;
239219

240-
let charset = opts.charset.unwrap_or_else(|| {
241-
if gctx.shell().out_unicode() {
242-
Charset::Utf8
243-
} else {
244-
Charset::Ascii
245-
}
246-
});
247-
let symbols = match charset {
248-
Charset::Utf8 => &UTF8_SYMBOLS,
249-
Charset::Ascii => &ASCII_SYMBOLS,
220+
let symbols = if gctx.shell().out_unicode() {
221+
&UTF8_SYMBOLS
222+
} else {
223+
&ASCII_SYMBOLS
250224
};
251225

252226
// The visited deps is used to display a (*) whenever a dep has

0 commit comments

Comments
 (0)