From 6e7429402f6488a1a5265c7e550d5f53bf8853a6 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 4 Mar 2024 14:30:36 -0600 Subject: [PATCH] refactor(tree): Pass --charset through config --- src/bin/cargo/commands/tree.rs | 31 +++++++++++++++++++++++++++---- src/cargo/ops/tree/mod.rs | 34 ++++------------------------------ 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/bin/cargo/commands/tree.rs b/src/bin/cargo/commands/tree.rs index f466b001b8ce..6d83f8e8e952 100644 --- a/src/bin/cargo/commands/tree.rs +++ b/src/bin/cargo/commands/tree.rs @@ -100,6 +100,24 @@ pub fn cli() -> Command { )) } +#[derive(Copy, Clone)] +pub enum Charset { + Utf8, + Ascii, +} + +impl FromStr for Charset { + type Err = &'static str; + + fn from_str(s: &str) -> Result { + match s { + "utf8" => Ok(Charset::Utf8), + "ascii" => Ok(Charset::Ascii), + _ => Err("invalid charset"), + } + } +} + pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult { if args.flag("version") { let verbose = args.verbose() > 0; @@ -181,10 +199,16 @@ subtree of the package given to -p.\n\ } let charset = args.get_one::("charset"); - let charset = charset - .map(|c| tree::Charset::from_str(c)) + if let Some(charset) = charset + .map(|c| Charset::from_str(c)) .transpose() - .map_err(|e| anyhow::anyhow!("{}", e))?; + .map_err(|e| anyhow::anyhow!("{}", e))? + { + match charset { + Charset::Utf8 => gctx.shell().set_unicode(true)?, + Charset::Ascii => gctx.shell().set_unicode(false)?, + } + } let opts = tree::TreeOptions { cli_features: args.cli_features()?, packages, @@ -195,7 +219,6 @@ subtree of the package given to -p.\n\ prefix, no_dedupe, duplicates: args.flag("duplicates"), - charset, format: args.get_one::("format").cloned().unwrap(), graph_features, max_display_depth: args.value_of_u32("depth")?.unwrap_or(u32::MAX), diff --git a/src/cargo/ops/tree/mod.rs b/src/cargo/ops/tree/mod.rs index 81f62fcc4479..da502da8ba9f 100644 --- a/src/cargo/ops/tree/mod.rs +++ b/src/cargo/ops/tree/mod.rs @@ -39,8 +39,6 @@ pub struct TreeOptions { /// appear with different versions, and report if any where found. Implies /// `invert`. pub duplicates: bool, - /// The style of characters to use. - pub charset: Option, /// A format string indicating how each package should be displayed. pub format: String, /// Includes features in the tree as separate nodes. @@ -68,24 +66,6 @@ impl Target { } } -#[derive(Copy, Clone)] -pub enum Charset { - Utf8, - Ascii, -} - -impl FromStr for Charset { - type Err = &'static str; - - fn from_str(s: &str) -> Result { - match s { - "utf8" => Ok(Charset::Utf8), - "ascii" => Ok(Charset::Ascii), - _ => Err("invalid charset"), - } - } -} - #[derive(Clone, Copy)] pub enum Prefix { None, @@ -237,16 +217,10 @@ fn print( let format = Pattern::new(&opts.format) .with_context(|| format!("tree format `{}` not valid", opts.format))?; - let charset = opts.charset.unwrap_or_else(|| { - if gctx.shell().out_unicode() { - Charset::Utf8 - } else { - Charset::Ascii - } - }); - let symbols = match charset { - Charset::Utf8 => &UTF8_SYMBOLS, - Charset::Ascii => &ASCII_SYMBOLS, + let symbols = if gctx.shell().out_unicode() { + &UTF8_SYMBOLS + } else { + &ASCII_SYMBOLS }; // The visited deps is used to display a (*) whenever a dep has