Added progress bar styling

This commit is contained in:
Nathan Fisher 2023-03-28 21:18:55 -04:00
parent cf9d6aa7e6
commit efe77b97d8
2 changed files with 27 additions and 14 deletions

4
Cargo.lock generated
View File

@ -618,7 +618,7 @@ checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
[[package]] [[package]]
name = "hpk" name = "hpk"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.hitchhiker-linux.org/jeang3nie/hpk.git#3d0151610d80f5453248ebd8e7e73f7cefd0fc13" source = "git+https://git.hitchhiker-linux.org/jeang3nie/hpk.git#0a94691685433b64c2a7acc2d11094a783297b00"
dependencies = [ dependencies = [
"chrono", "chrono",
"deku", "deku",
@ -1358,7 +1358,7 @@ checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]] [[package]]
name = "tar" name = "tar"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.hitchhiker-linux.org/jeang3nie/hpk.git#3d0151610d80f5453248ebd8e7e73f7cefd0fc13" source = "git+https://git.hitchhiker-linux.org/jeang3nie/hpk.git#0a94691685433b64c2a7acc2d11094a783297b00"
dependencies = [ dependencies = [
"deku", "deku",
"libc", "libc",

View File

@ -1,19 +1,18 @@
use std::io::ErrorKind; #![warn(clippy::all, clippy::pedantic)]
use { use {
clap::ArgMatches, clap::ArgMatches,
hpk::{Creator, Dependency, Message, Specs, Version}, hpk::{Creator, Dependency, Message, Specs, Version},
hpk_cli::cli, hpk_cli::cli,
indicatif::ProgressBar, indicatif::{ProgressBar, ProgressStyle},
ron::ser::{to_writer_pretty, PrettyConfig}, ron::ser::{to_writer_pretty, PrettyConfig},
std::{ std::{
env, env,
error::Error, error::Error,
ffi::OsStr, ffi::OsStr,
fs::File, fs::File,
io::{self, BufWriter}, io::{self, BufWriter, ErrorKind},
path::PathBuf, path::PathBuf,
sync::{mpsc, Mutex}, sync::mpsc,
thread, thread,
}, },
}; };
@ -67,19 +66,30 @@ fn create(matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
specs.dependencies.push(d); specs.dependencies.push(d);
} }
} }
let creator = Creator::new(&dir, specs)?; let creator = Creator::new(&dir, specs.clone())?;
let (sender, receiver) = mpsc::channel(); let (sender, receiver) = mpsc::channel();
let len = creator.len(); let len = creator.len();
let pb = Mutex::new(ProgressBar::new(len as u64)); let pb = ProgressBar::new(len as u64);
pb.set_style(
ProgressStyle::with_template("[ {prefix} ] {wide_bar}{pos:>5.cyan}/{len:5.green}{msg:>30}")
.unwrap(),
);
pb.set_prefix("Adding files");
pb.println(format!(
"Creating package {}-{}_{}.tar.zst",
&specs.name, &specs.version, &specs.release
));
let handle = thread::spawn(move || { let handle = thread::spawn(move || {
for msg in receiver.iter() { for msg in receiver.iter() {
match msg { match msg {
Message::MemberAdded(_s) => pb.lock().unwrap().inc(1), Message::MemberAdded(s) => {
Message::Success(_s) => { pb.set_message(s.split('/').last().unwrap().to_string());
let pb = pb.lock().unwrap();
pb.inc(1); pb.inc(1);
pb.finish_with_message("done");
}, },
Message::Success(_s) => {
pb.inc(1);
pb.finish_and_clear();
}
Message::Failure(s) => { Message::Failure(s) => {
eprint!("{s}"); eprint!("{s}");
return Err(io::Error::new(io::ErrorKind::Other, s)); return Err(io::Error::new(io::ErrorKind::Other, s));
@ -90,7 +100,10 @@ fn create(matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
}); });
creator.create(&outdir, sender)?; creator.create(&outdir, sender)?;
match handle.join() { match handle.join() {
Ok(_) => Ok(()), Ok(_) => {
println!("Package created successfully");
Ok(())
},
Err(e) => Err(io::Error::new(ErrorKind::Other, format!("{e:?}")).into()), Err(e) => Err(io::Error::new(ErrorKind::Other, format!("{e:?}")).into()),
} }
} }