Skip to content

Commit

Permalink
refactor(tree): Pass --charset through config
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Mar 4, 2024
1 parent 7bc4d3e commit 54db14f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
31 changes: 27 additions & 4 deletions src/bin/cargo/commands/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Charset, &'static str> {
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;
Expand Down Expand Up @@ -181,10 +199,16 @@ subtree of the package given to -p.\n\
}

let charset = args.get_one::<String>("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,
Expand All @@ -195,7 +219,6 @@ subtree of the package given to -p.\n\
prefix,
no_dedupe,
duplicates: args.flag("duplicates"),
charset,
format: args.get_one::<String>("format").cloned().unwrap(),
graph_features,
max_display_depth: args.value_of_u32("depth")?.unwrap_or(u32::MAX),
Expand Down
34 changes: 4 additions & 30 deletions src/cargo/ops/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Charset>,
/// A format string indicating how each package should be displayed.
pub format: String,
/// Includes features in the tree as separate nodes.
Expand Down Expand Up @@ -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<Charset, &'static str> {
match s {
"utf8" => Ok(Charset::Utf8),
"ascii" => Ok(Charset::Ascii),
_ => Err("invalid charset"),
}
}
}

#[derive(Clone, Copy)]
pub enum Prefix {
None,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 54db14f

Please sign in to comment.