Skip to content

Commit

Permalink
Simplfy apply_draw_state
Browse files Browse the repository at this point in the history
Make it more obvious we only draw if we need to
  • Loading branch information
aj-bagwell committed May 17, 2021
1 parent 7904013 commit 8cacfd2
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,37 +503,32 @@ impl ProgressDrawTarget {

/// Apply the given draw state (draws it).
pub(crate) fn apply_draw_state(&mut self, draw_state: ProgressDrawState) -> io::Result<()> {
let (term, last_line_count, last_draw) = match self.kind {
match self.kind {
ProgressDrawTargetKind::Term {
ref term,
ref mut last_line_count,
rate,
ref mut last_draw,
} if draw_state.finished || draw_state.force_draw || last_draw.elapsed() > rate => {
(term, last_line_count, last_draw)
if !draw_state.lines.is_empty() && draw_state.move_cursor {
term.move_cursor_up(*last_line_count)?;
} else {
term.clear_last_lines(*last_line_count)?;
}

draw_state.draw_to_term(term)?;
term.flush()?;
*last_line_count = draw_state.lines.len() - draw_state.orphan_lines;
*last_draw = Instant::now();

Ok(())
}
ProgressDrawTargetKind::Remote { idx, ref state, .. } => {
return state
.write()
.unwrap()
.draw(idx, draw_state)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e));
state.write().unwrap().draw(idx, draw_state)
}
// Hidden, finished, or no need to refresh yet
_ => return Ok(()),
};

if !draw_state.lines.is_empty() && draw_state.move_cursor {
term.move_cursor_up(*last_line_count)?;
} else {
term.clear_last_lines(*last_line_count)?;
_ => Ok(()),
}

draw_state.draw_to_term(term)?;
term.flush()?;
*last_line_count = draw_state.lines.len() - draw_state.orphan_lines;
*last_draw = Instant::now();
Ok(())
}

/// Properly disconnects from the draw target
Expand Down

0 comments on commit 8cacfd2

Please sign in to comment.