Skip to content

Commit

Permalink
Convert to anstyle/anstream
Browse files Browse the repository at this point in the history
Remove the previous color/no color plumbing, and the enable-ansi-support
and supports-color crates that it used. Also remove a redundant check
for CliName::Help

Start using anstream::AutoStream

...still need to remove the previous color/no color plumbing

Remove enable-ansi-support & supports-color crates

Remove code that did manual color/no-color choice

...since anstream::Autostream now does that for us

Refactor: remove redundant check for CliName::Help
  • Loading branch information
yarrow committed Feb 2, 2024
1 parent 1843dda commit 548108f
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 230 deletions.
140 changes: 56 additions & 84 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ edition = '2021'

[dependencies]
anyhow = "1.0.42"
anstyle = "1.0.4"
anstream = "0.6.5"
bstr = { version = "1.1.0", default-features = false, features = ["std", "alloc"] }
encoding_rs = "0.8.28"
encoding_rs_io = "0.1.7"
Expand All @@ -22,8 +24,6 @@ clap = { version = "4.1.4", default-features = false, features = ["std","error-c
memchr = "2.4.0"
indexmap = "1.7.0"
is-terminal = "0.4.2"
enable-ansi-support = "0.2.1"
supports-color = "2.0.0"
textwrap = "0.16.0"
once_cell = "1.17.1"
terminal_size = "0.2.5"
Expand Down
53 changes: 28 additions & 25 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::help;
use crate::operations::LogType;
use crate::styles::{set_color_choice, ColorChoice};
use crate::styles::ColorChoice;
use clap::{Parser, ValueEnum};
use std::path::PathBuf;

Expand All @@ -12,34 +12,16 @@ use std::path::PathBuf;
pub fn parsed() -> Args {
let parsed = CliArgs::parse();
let cc = parsed.color.unwrap_or(ColorChoice::Auto);
set_color_choice(cc);
if parsed.help {
help_and_exit();
help_and_exit(&cc);
}
if parsed.version {
println!("{}", help::version());
exit_success();
}
let Some(op) = parsed.command else { help_and_exit() };
if op == CliName::Help {
help_and_exit()
}
let log_type = if parsed.count_files {
LogType::Files
} else if parsed.count_lines {
LogType::Lines
} else if parsed.count {
if parsed.files {
LogType::Files
} else {
LogType::Lines
}
} else {
LogType::None
};

let Some(op) = parsed.command else { help_and_exit(&cc) };
let op = match op {
CliName::Help => help_and_exit(), // This can't happen, but...
CliName::Help => help_and_exit(&cc),
CliName::Intersect => OpName::Intersect,
CliName::Union => OpName::Union,
CliName::Diff => OpName::Diff,
Expand All @@ -58,12 +40,33 @@ pub fn parsed() -> Args {
}
}
};

let log_type = if parsed.count_files {
LogType::Files
} else if parsed.count_lines {
LogType::Lines
} else if parsed.count {
if parsed.files {
LogType::Files
} else {
LogType::Lines
}
} else {
LogType::None
};

Args { op, log_type, paths: parsed.paths }
}

fn help_and_exit() -> ! {
help::print();
exit_success();
fn help_and_exit(cc: &ColorChoice) -> ! {
let code = match help::print(cc) {
Err(e) => {
eprintln!("{e}");
1
}
Ok(()) => SUCCESS_CODE,
};
safe_exit(code);
}

const SUCCESS_CODE: i32 = 0;
Expand Down
Loading

0 comments on commit 548108f

Please sign in to comment.