1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use crate::cursor::CursorPosition;
use crate::line::Line;
use crate::update::UpdateBatch;
use termion::raw::RawTerminal;
use std::io::{Write, StdoutLock};
use crate::utility::move_cursor_to;
use crate::result::Result;
pub(crate) struct InterfaceState {
pub(crate) cursor: CursorPosition,
pub(crate) lines: Vec<Line>,
}
pub struct TTYInterface {
state: InterfaceState,
}
impl TTYInterface {
pub fn new() -> TTYInterface {
TTYInterface {
state: InterfaceState {
lines: Vec::new(),
cursor: CursorPosition::init(0, 0)
}
}
}
pub fn start_update(&self) -> UpdateBatch {
UpdateBatch { steps: Vec::new() }
}
pub fn perform_update(&mut self, stdout: &mut RawTerminal<StdoutLock>, batch: UpdateBatch) -> Result<()> {
let mut update_cursor = self.state.cursor;
for mut step in batch.steps {
step.do_update(stdout, &mut self.state, &mut update_cursor)?;
}
move_cursor_to(stdout, &mut update_cursor, &self.state.cursor)?;
stdout.flush()?;
Ok(())
}
pub fn end(&self, stdout: &mut RawTerminal<StdoutLock>) -> Result<()> {
write!(stdout, "{}", "\n".repeat(self.state.lines.len()))?;
stdout.flush()?;
Ok(())
}
}