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 d69eb80 commit 135e4d6
Show file tree
Hide file tree
Showing 2 changed files with 14 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 @@ -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();
Expand Down
19 changes: 13 additions & 6 deletions src/progress.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::io;
use std::time::{Duration, Instant};

use crossterm::terminal::{Clear, ClearType};
Expand All @@ -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);

Expand All @@ -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(())
}
}

Expand Down

0 comments on commit 135e4d6

Please sign in to comment.