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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use std::time::Duration;
use error::*;
use prototty::*;
use prototty_grid::*;
use terminal::*;
use cell::*;
pub struct Context {
terminal: Terminal,
grid: Grid<Cell>,
}
impl Context {
pub fn new() -> Result<Self> {
Terminal::new().and_then(Self::from_terminal)
}
fn from_terminal(mut terminal: Terminal) -> Result<Self> {
let size = terminal.resize_if_necessary()?;
let grid = Grid::new(size);
Ok(Self {
terminal,
grid,
})
}
fn resize_if_necessary(&mut self) -> Result<()> {
let size = self.terminal.resize_if_necessary()?;
if size != self.grid.size() {
self.grid.resize(size);
}
Ok(())
}
pub fn drain_input(&mut self) -> Result<DrainInput> {
self.terminal.drain_input()
}
pub fn poll_input(&mut self) -> Result<Option<Input>> {
self.terminal.poll_input()
}
pub fn wait_input(&mut self) -> Result<Input> {
self.terminal.wait_input()
}
pub fn wait_input_timeout(&mut self, timeout: Duration) -> Result<Option<Input>> {
self.terminal.wait_input_timeout(timeout)
}
}
impl Renderer for Context {
type Error = Error;
fn render_at<V: View<T>, T>(&mut self, view: &mut V, data: &T, offset: Coord, depth: i32) -> Result<()> {
self.resize_if_necessary()?;
self.grid.clear();
view.view(data, offset, depth, &mut self.grid);
self.terminal.draw_grid(&self.grid)
}
}