mirror of
https://github.com/rustic-rs/rustic.git
synced 2025-10-26 11:18:51 +00:00
support progress bars without length (#968)
Co-authored-by: simonsan <14062932+simonsan@users.noreply.github.com>
This commit is contained in:
parent
9bbabce165
commit
ea22ff0778
@ -46,7 +46,7 @@ impl ProgressOptions {
|
||||
|
||||
/// Create a hidden progress bar
|
||||
pub fn no_progress() -> RusticProgress {
|
||||
RusticProgress(ProgressBar::hidden())
|
||||
RusticProgress(ProgressBar::hidden(), ProgressType::Hidden)
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,7 +64,7 @@ impl ProgressBars for ProgressOptions {
|
||||
);
|
||||
p.set_prefix(prefix);
|
||||
p.enable_steady_tick(self.progress_interval());
|
||||
RusticProgress(p)
|
||||
RusticProgress(p, ProgressType::Spinner)
|
||||
}
|
||||
|
||||
fn progress_counter(&self, prefix: impl Into<Cow<'static, str>>) -> RusticProgress {
|
||||
@ -73,12 +73,12 @@ impl ProgressBars for ProgressOptions {
|
||||
}
|
||||
let p = ProgressBar::new(0).with_style(
|
||||
ProgressStyle::default_bar()
|
||||
.template("[{elapsed_precise}] {prefix:30} {bar:40.cyan/blue} {pos:>10}/{len:10}")
|
||||
.template("[{elapsed_precise}] {prefix:30} {bar:40.cyan/blue} {pos:>10}")
|
||||
.unwrap(),
|
||||
);
|
||||
p.set_prefix(prefix);
|
||||
p.enable_steady_tick(self.progress_interval());
|
||||
RusticProgress(p)
|
||||
RusticProgress(p, ProgressType::Counter)
|
||||
}
|
||||
|
||||
fn progress_hidden(&self) -> RusticProgress {
|
||||
@ -91,23 +91,26 @@ impl ProgressBars for ProgressOptions {
|
||||
}
|
||||
let p = ProgressBar::new(0).with_style(
|
||||
ProgressStyle::default_bar()
|
||||
.with_key("my_eta", |s: &ProgressState, w: &mut dyn Write|
|
||||
match (s.pos(), s.len()){
|
||||
(pos,Some(len)) if pos != 0 => write!(w,"{:#}", HumanDuration(Duration::from_secs(s.elapsed().as_secs() * (len-pos)/pos))),
|
||||
(_, _) => write!(w,"-"),
|
||||
}.unwrap())
|
||||
.template("[{elapsed_precise}] {prefix:30} {bar:40.cyan/blue} {bytes:>10}/{total_bytes:10} {bytes_per_sec:12} (ETA {my_eta})")
|
||||
.template("[{elapsed_precise}] {prefix:30} {bar:40.cyan/blue} {bytes:>10} {bytes_per_sec:12}")
|
||||
.unwrap()
|
||||
);
|
||||
p.set_prefix(prefix);
|
||||
p.enable_steady_tick(self.progress_interval());
|
||||
RusticProgress(p)
|
||||
RusticProgress(p, ProgressType::Bytes)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum ProgressType {
|
||||
Hidden,
|
||||
Spinner,
|
||||
Counter,
|
||||
Bytes,
|
||||
}
|
||||
|
||||
/// A default progress bar
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RusticProgress(ProgressBar);
|
||||
pub struct RusticProgress(ProgressBar, ProgressType);
|
||||
|
||||
impl Progress for RusticProgress {
|
||||
fn is_hidden(&self) -> bool {
|
||||
@ -115,6 +118,30 @@ impl Progress for RusticProgress {
|
||||
}
|
||||
|
||||
fn set_length(&self, len: u64) {
|
||||
match self.1 {
|
||||
ProgressType::Counter => {
|
||||
self.0.set_style(
|
||||
ProgressStyle::default_bar()
|
||||
.template(
|
||||
"[{elapsed_precise}] {prefix:30} {bar:40.cyan/blue} {pos:>10}/{len:10}",
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
}
|
||||
ProgressType::Bytes => {
|
||||
self.0.set_style(
|
||||
ProgressStyle::default_bar()
|
||||
.with_key("my_eta", |s: &ProgressState, w: &mut dyn Write|
|
||||
match (s.pos(), s.len()){
|
||||
(pos,Some(len)) if pos != 0 => write!(w,"{:#}", HumanDuration(Duration::from_secs(s.elapsed().as_secs() * (len-pos)/pos))),
|
||||
(_, _) => write!(w,"-"),
|
||||
}.unwrap())
|
||||
.template("[{elapsed_precise}] {prefix:30} {bar:40.cyan/blue} {bytes:>10}/{total_bytes:10} {bytes_per_sec:12} (ETA {my_eta})")
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
self.0.set_length(len);
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user