Binary: begin moving flag discovery out of program logic

This commit is contained in:
Nathan Fisher 2024-12-15 01:30:38 -05:00
parent 84a0f2c049
commit 5420684061

View File

@ -25,14 +25,14 @@ mod cli;
static TEMPLATE: &str = "[ {prefix:^30!} ] {wide_bar}{pos:>5.cyan}/{len:5.green}"; static TEMPLATE: &str = "[ {prefix:^30!} ] {wide_bar}{pos:>5.cyan}/{len:5.green}";
struct Flags { struct ListingFlags {
files: bool, files: bool,
color: bool, color: bool,
long: bool, long: bool,
human: bool, human: bool,
} }
impl From<&ArgMatches> for Flags { impl From<&ArgMatches> for ListingFlags {
fn from(value: &ArgMatches) -> Self { fn from(value: &ArgMatches) -> Self {
Self { Self {
files: value.get_flag("files"), files: value.get_flag("files"),
@ -43,6 +43,39 @@ impl From<&ArgMatches> for Flags {
} }
} }
enum Verbosity {
Quiet,
Normal,
Verbose,
}
impl From<&ArgMatches> for Verbosity {
fn from(value: &ArgMatches) -> Self {
if matches.get_flag("quiet") {
Self::Quiet
} else if matches.get_flag("verbose") && !matches.get_flag("stdout") {
Self::Verbose
} else {
Self::Normal
}
}
}
struct CreateFlags {
algorithm: Algorithm,
zstd_compression: Option<i32>,
verbosity: Verbosity,
stdout: bool,
uid: Option<u32>,
gid: Option<u32>,
}
impl From<&ArgMatches> for CreateFlags {
fn from(value: &ArgMatches) -> Self {
todo!()
}
}
fn main() { fn main() {
let matches = cli::haggis().get_matches(); let matches = cli::haggis().get_matches();
match matches.subcommand() { match matches.subcommand() {
@ -318,17 +351,18 @@ fn progress(file: &str, receiver: &mpsc::Receiver<StreamMessage>, len: u64, matc
} }
fn print_listing(li: &Listing, matches: &ArgMatches) -> Result<(), haggis::Error> { fn print_listing(li: &Listing, matches: &ArgMatches) -> Result<(), haggis::Error> {
if matches.get_flag("files") && li.kind == ListingKind::Directory { let flags = ListingFlags::from(matches);
if flags.files && li.kind == ListingKind::Directory {
return Ok(()); return Ok(());
} }
if matches.get_flag("color") { if flags.color {
if matches.get_flag("long") { if flags.long {
li.print_color(matches.get_flag("human"))?; li.print_color(flags.human)?;
} else { } else {
li.print_color_simple()?; li.print_color_simple()?;
} }
} else if matches.get_flag("long") { } else if flags.long {
li.print(matches.get_flag("human")); li.print(flags.human);
} else { } else {
println!("{}", li.name); println!("{}", li.name);
} }