ok/jj
1
0
Fork 0
forked from mirrors/jj

cli: clean up progress error handling

This commit is contained in:
Benjamin Saunders 2022-11-06 16:31:25 -08:00
parent d69eb808df
commit 135e4d64dd
2 changed files with 14 additions and 7 deletions

View file

@ -4104,7 +4104,7 @@ fn with_remote_callbacks<T>(ui: &mut Ui, f: impl FnOnce(git::RemoteCallbacks<'_>
let mut progress = Progress::new(Instant::now());
let ui = &ui;
callback = Some(move |x: &git::Progress| {
progress.update(Instant::now(), x, *ui.lock().unwrap());
_ = progress.update(Instant::now(), x, *ui.lock().unwrap());
});
}
let mut callbacks = git::RemoteCallbacks::default();

View file

@ -1,3 +1,4 @@
use std::io;
use std::time::{Duration, Instant};
use crossterm::terminal::{Clear, ClearType};
@ -20,19 +21,24 @@ impl Progress {
}
}
pub fn update(&mut self, now: Instant, progress: &git::Progress, ui: &mut Ui) {
pub fn update(
&mut self,
now: Instant,
progress: &git::Progress,
ui: &mut Ui,
) -> io::Result<()> {
use std::fmt::Write as _;
if progress.overall == 1.0 {
_ = write!(ui, "\r{}", Clear(ClearType::CurrentLine));
return;
write!(ui, "\r{}", Clear(ClearType::CurrentLine))?;
return Ok(());
}
let rate = progress
.bytes_downloaded
.and_then(|x| self.rate.update(now, x));
if now < self.next_print {
return;
return Ok(());
}
self.next_print = now.min(self.next_print + Duration::from_secs(1) / UPDATE_HZ);
@ -54,8 +60,9 @@ impl Progress {
draw_progress(progress.overall, &mut self.buffer, bar_width);
self.buffer.push(']');
_ = write!(ui, "{}", self.buffer);
_ = ui.flush();
write!(ui, "{}", self.buffer)?;
ui.flush()?;
Ok(())
}
}