Skip to content

Commit bfc7324

Browse files
author
Stephan Dilly
committed
fix spinner drawing if char didn't change (#764)
1 parent 5320d0e commit bfc7324

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

src/spinner.rs

+32-17
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1-
use std::io;
2-
use tui::{backend::Backend, buffer::Cell, Terminal};
1+
use std::{cell::Cell, char, io};
2+
use tui::{backend::Backend, Terminal};
33

44
// static SPINNER_CHARS: &[char] = &['◢', '◣', '◤', '◥'];
55
// static SPINNER_CHARS: &[char] = &['⢹', '⢺', '⢼', '⣸', '⣇', '⡧', '⡗', '⡏'];
66
static SPINNER_CHARS: &[char] =
77
&['⣷', '⣯', '⣟', '⡿', '⢿', '⣻', '⣽', '⣾'];
88

99
///
10-
#[derive(Default)]
1110
pub struct Spinner {
1211
idx: usize,
13-
pending: bool,
12+
active: bool,
13+
last_char: Cell<char>,
14+
}
15+
16+
impl Default for Spinner {
17+
fn default() -> Self {
18+
Self {
19+
idx: 0,
20+
active: false,
21+
last_char: Cell::new(' '),
22+
}
23+
}
1424
}
1525

1626
impl Spinner {
@@ -21,8 +31,8 @@ impl Spinner {
2131
}
2232

2333
///
24-
pub fn set_state(&mut self, pending: bool) {
25-
self.pending = pending;
34+
pub fn set_state(&mut self, active: bool) {
35+
self.active = active;
2636
}
2737

2838
/// draws or removes spinner char depending on `pending` state
@@ -32,17 +42,22 @@ impl Spinner {
3242
) -> io::Result<()> {
3343
let idx = self.idx;
3444

35-
let c: Cell = Cell::default()
36-
.set_char(if self.pending {
37-
SPINNER_CHARS[idx]
38-
} else {
39-
' '
40-
})
41-
.clone();
42-
terminal
43-
.backend_mut()
44-
.draw(vec![(0_u16, 0_u16, &c)].into_iter())?;
45-
tui::backend::Backend::flush(terminal.backend_mut())?;
45+
let char_to_draw =
46+
if self.active { SPINNER_CHARS[idx] } else { ' ' };
47+
48+
if self.last_char.get() != char_to_draw {
49+
self.last_char.set(char_to_draw);
50+
51+
let c = tui::buffer::Cell::default()
52+
.set_char(char_to_draw)
53+
.clone();
54+
55+
terminal
56+
.backend_mut()
57+
.draw(vec![(0_u16, 0_u16, &c)].into_iter())?;
58+
59+
tui::backend::Backend::flush(terminal.backend_mut())?;
60+
}
4661

4762
Ok(())
4863
}

0 commit comments

Comments
 (0)