From 2b34d9ed01e00a262624d19102335fc9a6738b47 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sat, 14 Dec 2024 18:57:46 -0500 Subject: [PATCH] Binary: Add "verbose" flag --- src/haggis.rs | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/src/haggis.rs b/src/haggis.rs index a4d1f33..8aa4f40 100644 --- a/src/haggis.rs +++ b/src/haggis.rs @@ -25,6 +25,24 @@ mod cli; static TEMPLATE: &str = "[ {prefix:^30!} ] {wide_bar}{pos:>5.cyan}/{len:5.green}"; +struct Flags { + files: bool, + color: bool, + long: bool, + human: bool, +} + +impl From<&ArgMatches> for Flags { + fn from(value: &ArgMatches) -> Self { + Self { + files: value.get_flag("files"), + color: value.get_flag("color"), + long: value.get_flag("long"), + human: value.get_flag("human"), + } + } +} + fn main() { let matches = cli::haggis().get_matches(); match matches.subcommand() { @@ -92,6 +110,7 @@ fn create(matches: &ArgMatches) -> Result<(), haggis::Error> { if let Some(o) = output { pb.println(format!("Creating archive {o}")); } + let m = matches.clone(); handle = Some(thread::spawn(move || { for msg in &receiver { match msg { @@ -103,6 +122,11 @@ fn create(matches: &ArgMatches) -> Result<(), haggis::Error> { pb.set_prefix(format!("{name} added")); } pb.inc(1); + if m.get_flag("verbose") { + pb.suspend(|| { + let _res = print_listing(&n, &m); + }); + } } Message::Eof => { pb.finish_and_clear(); @@ -186,8 +210,9 @@ fn extract(matches: &ArgMatches) -> Result<(), haggis::Error> { let reader = Decoder::new(fd)?; let mut stream = NodeStream::new(reader)?; let handle = if matches.get_flag("quiet") { + let matches = matches.clone(); Some(thread::spawn(move || { - let t = progress(&file, &receiver, u64::from(stream.length)); + let t = progress(&file, &receiver, u64::from(stream.length), &matches); total_ref.store(t, Ordering::Relaxed); Ok::<(), haggis::Error>(()) })) @@ -200,8 +225,9 @@ fn extract(matches: &ArgMatches) -> Result<(), haggis::Error> { let reader = BufReader::new(fd); let mut stream = NodeStream::new(reader)?; let handle = if matches.get_flag("quiet") { + let matches = matches.clone(); Some(thread::spawn(move || { - let t = progress(&file, &receiver, u64::from(stream.length)); + let t = progress(&file, &receiver, u64::from(stream.length), &matches); total_ref.store(t, Ordering::Relaxed); Ok::<(), haggis::Error>(()) })) @@ -234,7 +260,7 @@ fn extract(matches: &ArgMatches) -> Result<(), haggis::Error> { } } -fn progress(file: &str, receiver: &mpsc::Receiver, len: u64) -> u64 { +fn progress(file: &str, receiver: &mpsc::Receiver, len: u64, matches: &ArgMatches) -> u64 { let mut total: u64 = 0; let pb = ProgressBar::new(len); pb.set_style(ProgressStyle::with_template(TEMPLATE).unwrap()); @@ -250,8 +276,8 @@ fn progress(file: &str, receiver: &mpsc::Receiver, len: u64) -> u pb.inc(1); total += size; } - ListingKind::SoftLink(t) - | ListingKind::HardLink(t) => { + ListingKind::SoftLink(ref t) + | ListingKind::HardLink(ref t) => { pb.set_prefix(format!("{name} -> {t}")); pb.inc(1); } @@ -259,8 +285,8 @@ fn progress(file: &str, receiver: &mpsc::Receiver, len: u64) -> u pb.set_prefix(format!("mkdir {name}")); pb.inc(1); } - ListingKind::Block(_d) - | ListingKind::Character(_d) => { + ListingKind::Block(_) + | ListingKind::Character(_) => { pb.set_prefix(format!("mknod {name}")); pb.inc(1); } @@ -273,6 +299,11 @@ fn progress(file: &str, receiver: &mpsc::Receiver, len: u64) -> u break; } } + if matches.get_flag("verbose") { + pb.suspend(|| { + let _res = print_listing(&n, matches); + }); + } } StreamMessage::Eof => { pb.finish_and_clear();