Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix move cursor flag not working #648

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion src/draw_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ impl ProgressDrawTarget {
}
}

/// Set whether or not to just move cursor instead of clearing lines
pub(crate) fn set_move_cursor(&mut self, move_cursor: bool) {
match &mut self.kind {
TargetKind::Term { draw_state, .. } => draw_state.move_cursor = move_cursor,
TargetKind::TermLike { draw_state, .. } => draw_state.move_cursor = move_cursor,
_ => {}
}
}

/// Apply the given draw state (draws it).
pub(crate) fn drawable(&mut self, force_draw: bool, now: Instant) -> Option<Drawable<'_>> {
match &mut self.kind {
Expand Down Expand Up @@ -473,7 +482,9 @@ impl DrawState {
}

if !self.lines.is_empty() && self.move_cursor {
term.move_cursor_up(last_line_count.as_usize())?;
// Move up to first line (assuming the last line doesn't contain a '\n') and then move to then front of the line
term.move_cursor_up(last_line_count.as_usize().saturating_sub(1))?;
term.write_str("\r")?;
} else {
// Fork of console::clear_last_lines that assumes that the last line doesn't contain a '\n'
let n = last_line_count.as_usize();
Expand Down
14 changes: 6 additions & 8 deletions src/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ impl MultiProgress {
/// This can reduce flickering, but do not enable it if you intend to change the number of
/// progress bars.
pub fn set_move_cursor(&self, move_cursor: bool) {
self.state.write().unwrap().move_cursor = move_cursor;
self.state
.write()
.unwrap()
.draw_target
.set_move_cursor(move_cursor);
}

/// Set alignment flag
Expand Down Expand Up @@ -209,8 +213,6 @@ pub(crate) struct MultiState {
ordering: Vec<usize>,
/// Target for draw operation for MultiProgress
draw_target: ProgressDrawTarget,
/// Whether or not to just move cursor instead of clearing lines
move_cursor: bool,
/// Controls how the multi progress is aligned if some of its progress bars get removed, default is `Top`
alignment: MultiProgressAlignment,
/// Lines to be drawn above everything else in the MultiProgress. These specifically come from
Expand All @@ -227,7 +229,6 @@ impl MultiState {
free_set: vec![],
ordering: vec![],
draw_target,
move_cursor: false,
alignment: MultiProgressAlignment::default(),
orphan_lines: Vec::new(),
zombie_lines_count: VisualLines::default(),
Expand Down Expand Up @@ -376,10 +377,7 @@ impl MultiState {
let member = self.members.get_mut(idx).unwrap();
// alignment is handled by the `MultiProgress`'s underlying draw target, so there is no
// point in propagating it here.
let state = member.draw_state.get_or_insert(DrawState {
move_cursor: self.move_cursor,
..Default::default()
});
let state = member.draw_state.get_or_insert(DrawState::default());

DrawStateWrapper::for_multi(state, &mut self.orphan_lines)
}
Expand Down
48 changes: 48 additions & 0 deletions tests/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ fn multi_progress_single_bar_and_clear() {
drop(pb1);
assert_eq!(in_mem.contents(), "");
}

#[test]
fn multi_progress_two_bars() {
let in_mem = InMemoryTerm::new(10, 80);
Expand Down Expand Up @@ -417,6 +418,53 @@ Another line printed"#
);
}

#[test]
fn multi_progress_move_cursor() {
let in_mem = InMemoryTerm::new(10, 80);
let mp =
MultiProgress::with_draw_target(ProgressDrawTarget::term_like(Box::new(in_mem.clone())));
mp.set_move_cursor(true);

let pb1 = mp.add(ProgressBar::new(10));
pb1.tick();
assert_eq!(
in_mem.moves_since_last_check(),
r#"Str("\r")
Str("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 0/10")
Str("")
Flush
"#
);

let pb2 = mp.add(ProgressBar::new(10));
pb2.tick();
assert_eq!(
in_mem.moves_since_last_check(),
r#"Str("\r")
Str("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 0/10")
Str("")
NewLine
Str("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 0/10")
Str("")
Flush
"#
);

pb1.inc(1);
assert_eq!(
in_mem.moves_since_last_check(),
r#"Up(1)
Str("\r")
Str("███████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 1/10")
Str("")
NewLine
Str("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 0/10")
Str("")
Flush
"#
);
}

#[test]
fn ticker_drop() {
let in_mem = InMemoryTerm::new(10, 80);
Expand Down