Skip to content

Commit

Permalink
cli: clean up progress error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralith committed Nov 7, 2022
1 parent 4ff69fa commit 62b9387
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4065,7 +4065,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, &mut *ui.lock().unwrap());
_ = progress.update(Instant::now(), x, &mut *ui.lock().unwrap());
});
}
let mut callbacks = git::RemoteCallbacks::default();
Expand Down
14 changes: 8 additions & 6 deletions src/progress.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::time::{Duration, Instant};
use std::io;

use crossterm::terminal::{Clear, ClearType};
use jujutsu_lib::git;
Expand All @@ -20,19 +21,19 @@ 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);

Expand All @@ -54,8 +55,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(())
}
}

Expand Down

0 comments on commit 62b9387

Please sign in to comment.