Binary: Add "verbose" flag

This commit is contained in:
Nathan Fisher 2024-12-14 18:57:46 -05:00
parent 22faadc34e
commit 2b34d9ed01

View File

@ -25,6 +25,24 @@ 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 {
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() { fn main() {
let matches = cli::haggis().get_matches(); let matches = cli::haggis().get_matches();
match matches.subcommand() { match matches.subcommand() {
@ -92,6 +110,7 @@ fn create(matches: &ArgMatches) -> Result<(), haggis::Error> {
if let Some(o) = output { if let Some(o) = output {
pb.println(format!("Creating archive {o}")); pb.println(format!("Creating archive {o}"));
} }
let m = matches.clone();
handle = Some(thread::spawn(move || { handle = Some(thread::spawn(move || {
for msg in &receiver { for msg in &receiver {
match msg { match msg {
@ -103,6 +122,11 @@ fn create(matches: &ArgMatches) -> Result<(), haggis::Error> {
pb.set_prefix(format!("{name} added")); pb.set_prefix(format!("{name} added"));
} }
pb.inc(1); pb.inc(1);
if m.get_flag("verbose") {
pb.suspend(|| {
let _res = print_listing(&n, &m);
});
}
} }
Message::Eof => { Message::Eof => {
pb.finish_and_clear(); pb.finish_and_clear();
@ -186,8 +210,9 @@ fn extract(matches: &ArgMatches) -> Result<(), haggis::Error> {
let reader = Decoder::new(fd)?; let reader = Decoder::new(fd)?;
let mut stream = NodeStream::new(reader)?; let mut stream = NodeStream::new(reader)?;
let handle = if matches.get_flag("quiet") { let handle = if matches.get_flag("quiet") {
let matches = matches.clone();
Some(thread::spawn(move || { 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); total_ref.store(t, Ordering::Relaxed);
Ok::<(), haggis::Error>(()) Ok::<(), haggis::Error>(())
})) }))
@ -200,8 +225,9 @@ fn extract(matches: &ArgMatches) -> Result<(), haggis::Error> {
let reader = BufReader::new(fd); let reader = BufReader::new(fd);
let mut stream = NodeStream::new(reader)?; let mut stream = NodeStream::new(reader)?;
let handle = if matches.get_flag("quiet") { let handle = if matches.get_flag("quiet") {
let matches = matches.clone();
Some(thread::spawn(move || { 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); total_ref.store(t, Ordering::Relaxed);
Ok::<(), haggis::Error>(()) Ok::<(), haggis::Error>(())
})) }))
@ -234,7 +260,7 @@ fn extract(matches: &ArgMatches) -> Result<(), haggis::Error> {
} }
} }
fn progress(file: &str, receiver: &mpsc::Receiver<StreamMessage>, len: u64) -> u64 { fn progress(file: &str, receiver: &mpsc::Receiver<StreamMessage>, len: u64, matches: &ArgMatches) -> u64 {
let mut total: u64 = 0; let mut total: u64 = 0;
let pb = ProgressBar::new(len); let pb = ProgressBar::new(len);
pb.set_style(ProgressStyle::with_template(TEMPLATE).unwrap()); pb.set_style(ProgressStyle::with_template(TEMPLATE).unwrap());
@ -250,8 +276,8 @@ fn progress(file: &str, receiver: &mpsc::Receiver<StreamMessage>, len: u64) -> u
pb.inc(1); pb.inc(1);
total += size; total += size;
} }
ListingKind::SoftLink(t) ListingKind::SoftLink(ref t)
| ListingKind::HardLink(t) => { | ListingKind::HardLink(ref t) => {
pb.set_prefix(format!("{name} -> {t}")); pb.set_prefix(format!("{name} -> {t}"));
pb.inc(1); pb.inc(1);
} }
@ -259,8 +285,8 @@ fn progress(file: &str, receiver: &mpsc::Receiver<StreamMessage>, len: u64) -> u
pb.set_prefix(format!("mkdir {name}")); pb.set_prefix(format!("mkdir {name}"));
pb.inc(1); pb.inc(1);
} }
ListingKind::Block(_d) ListingKind::Block(_)
| ListingKind::Character(_d) => { | ListingKind::Character(_) => {
pb.set_prefix(format!("mknod {name}")); pb.set_prefix(format!("mknod {name}"));
pb.inc(1); pb.inc(1);
} }
@ -273,6 +299,11 @@ fn progress(file: &str, receiver: &mpsc::Receiver<StreamMessage>, len: u64) -> u
break; break;
} }
} }
if matches.get_flag("verbose") {
pb.suspend(|| {
let _res = print_listing(&n, matches);
});
}
} }
StreamMessage::Eof => { StreamMessage::Eof => {
pb.finish_and_clear(); pb.finish_and_clear();