Skip to content

Commit 3d00eff

Browse files
committed
Forward move_cursor flag to draw state
The move_cursor flag needs to be set in the wrapped Term draw state, as that's the one, that is then actually used to draw to the terminal. The move_cursor flag in the multi draw state doesn't have any effect on drawing to the terminal.
1 parent 9fb2ca7 commit 3d00eff

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

src/draw_target.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ impl ProgressDrawTarget {
146146
}
147147
}
148148

149+
/// Set whether or not to just move cursor instead of clearing lines
150+
pub(crate) fn set_move_cursor(&mut self, move_cursor: bool) {
151+
match &mut self.kind {
152+
TargetKind::Term { draw_state, .. } => draw_state.move_cursor = move_cursor,
153+
TargetKind::TermLike { draw_state, .. } => draw_state.move_cursor = move_cursor,
154+
_ => {}
155+
}
156+
}
157+
149158
/// Apply the given draw state (draws it).
150159
pub(crate) fn drawable(&mut self, force_draw: bool, now: Instant) -> Option<Drawable<'_>> {
151160
match &mut self.kind {

src/multi.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ impl MultiProgress {
5858
/// This can reduce flickering, but do not enable it if you intend to change the number of
5959
/// progress bars.
6060
pub fn set_move_cursor(&self, move_cursor: bool) {
61-
self.state.write().unwrap().move_cursor = move_cursor;
61+
self.state
62+
.write()
63+
.unwrap()
64+
.draw_target
65+
.set_move_cursor(move_cursor);
6266
}
6367

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

384382
DrawStateWrapper::for_multi(state, &mut self.orphan_lines)
385383
}

tests/render.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ fn multi_progress_single_bar_and_clear() {
169169
drop(pb1);
170170
assert_eq!(in_mem.contents(), "");
171171
}
172+
172173
#[test]
173174
fn multi_progress_two_bars() {
174175
let in_mem = InMemoryTerm::new(10, 80);
@@ -417,6 +418,51 @@ Another line printed"#
417418
);
418419
}
419420

421+
#[test]
422+
fn multi_progress_move_cursor() {
423+
let in_mem = InMemoryTerm::new(10, 80);
424+
let mp =
425+
MultiProgress::with_draw_target(ProgressDrawTarget::term_like(Box::new(in_mem.clone())));
426+
mp.set_move_cursor(true);
427+
428+
let pb1 = mp.add(ProgressBar::new(10));
429+
pb1.tick();
430+
assert_eq!(
431+
in_mem.moves_since_last_check(),
432+
r#"Str("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 0/10")
433+
Str("")
434+
Flush
435+
"#
436+
);
437+
438+
let pb2 = mp.add(ProgressBar::new(10));
439+
pb2.tick();
440+
assert_eq!(
441+
in_mem.moves_since_last_check(),
442+
r#"Up(1)
443+
Str("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 0/10")
444+
Str("")
445+
NewLine
446+
Str("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 0/10")
447+
Str("")
448+
Flush
449+
"#
450+
);
451+
452+
pb1.inc(1);
453+
assert_eq!(
454+
in_mem.moves_since_last_check(),
455+
r#"Up(2)
456+
Str("███████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 1/10")
457+
Str("")
458+
NewLine
459+
Str("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 0/10")
460+
Str("")
461+
Flush
462+
"#
463+
);
464+
}
465+
420466
#[test]
421467
fn ticker_drop() {
422468
let in_mem = InMemoryTerm::new(10, 80);

0 commit comments

Comments
 (0)