From 5420684061ace2bb847f3e9c14a86d3cc1062c75 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sun, 15 Dec 2024 01:30:38 -0500 Subject: [PATCH] Binary: begin moving flag discovery out of program logic --- src/haggis.rs | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/src/haggis.rs b/src/haggis.rs index 8aa4f40..2c8622f 100644 --- a/src/haggis.rs +++ b/src/haggis.rs @@ -25,14 +25,14 @@ mod cli; static TEMPLATE: &str = "[ {prefix:^30!} ] {wide_bar}{pos:>5.cyan}/{len:5.green}"; -struct Flags { +struct ListingFlags { files: bool, color: bool, long: bool, human: bool, } -impl From<&ArgMatches> for Flags { +impl From<&ArgMatches> for ListingFlags { fn from(value: &ArgMatches) -> Self { Self { 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, + verbosity: Verbosity, + stdout: bool, + uid: Option, + gid: Option, +} + +impl From<&ArgMatches> for CreateFlags { + fn from(value: &ArgMatches) -> Self { + todo!() + } +} + fn main() { let matches = cli::haggis().get_matches(); match matches.subcommand() { @@ -318,17 +351,18 @@ fn progress(file: &str, receiver: &mpsc::Receiver, len: u64, matc } 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(()); } - if matches.get_flag("color") { - if matches.get_flag("long") { - li.print_color(matches.get_flag("human"))?; + if flags.color { + if flags.long { + li.print_color(flags.human)?; } else { li.print_color_simple()?; } - } else if matches.get_flag("long") { - li.print(matches.get_flag("human")); + } else if flags.long { + li.print(flags.human); } else { println!("{}", li.name); }