Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 18 additions & 26 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,6 @@ pub(crate) struct ProgressDrawState {
}

impl ProgressDrawState {
pub fn clear_term(&self, term: &Term) -> io::Result<()> {
term.clear_last_lines(self.lines.len() - self.orphan_lines)
}

pub fn move_cursor(&self, term: &Term) -> io::Result<()> {
term.move_cursor_up(self.lines.len() - self.orphan_lines)
}

pub fn draw_to_term(&self, term: &Term) -> io::Result<()> {
for line in &self.lines {
term.write_line(line)?;
Expand Down Expand Up @@ -412,13 +404,16 @@ impl ProgressDrawTarget {
///
/// Will panic if refresh_rate is `Some(0)`. To disable rate limiting use `None` instead.
pub fn term(term: Term, refresh_rate: impl Into<Option<u64>>) -> ProgressDrawTarget {
let rate = refresh_rate.into().map(|x| Duration::from_millis(1000 / x));
let rate = refresh_rate
.into()
.map(|x| Duration::from_millis(1000 / x))
.unwrap_or_else(|| Duration::from_secs(0));
ProgressDrawTarget {
kind: ProgressDrawTargetKind::Term {
term,
last_state: None,
last_line_count: 0,
rate,
last_draw: None,
last_draw: Instant::now() - rate,
},
}
}
Expand Down Expand Up @@ -462,27 +457,24 @@ impl ProgressDrawTarget {
match self.kind {
ProgressDrawTargetKind::Term {
ref term,
ref mut last_state,
ref mut last_line_count,
rate,
ref mut last_draw,
} => {
if draw_state.finished
|| draw_state.force_draw
|| rate.is_none()
|| last_draw.is_none()
|| last_draw.unwrap().elapsed() > rate.unwrap()
|| rate == Duration::from_secs(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly don't like this use of a sentinel value. The benefit of saving 8 bytes doesn't really seem worth it to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

|| rate == Duration::from_secs(0) could be removed without affecting behavior (leaving just forced || finished || elapsed > rate), and there is some precedent for this kind of usage of duration, see rust-lang/rust#73544, and rust-lang/rust#84084 which stabilizes Duration::is_zero. However, I'm fine with reverting this part of the PR if that's not convincing

|| last_draw.elapsed() > rate
{
if let Some(ref last_state) = *last_state {
if !draw_state.lines.is_empty() && draw_state.move_cursor {
last_state.move_cursor(term)?;
} else {
last_state.clear_term(term)?;
}
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_state = Some(draw_state);
*last_draw = Some(Instant::now());
*last_line_count = draw_state.lines.len() - draw_state.orphan_lines;
*last_draw = Instant::now();
}
}
ProgressDrawTargetKind::Remote { idx, ref chan, .. } => {
Expand Down Expand Up @@ -523,9 +515,9 @@ impl ProgressDrawTarget {
pub(crate) enum ProgressDrawTargetKind {
Term {
term: Term,
last_state: Option<ProgressDrawState>,
rate: Option<Duration>,
last_draw: Option<Instant>,
last_line_count: usize,
rate: Duration,
last_draw: Instant,
},
Remote {
state: Arc<RwLock<MultiProgressState>>,
Expand Down