blob: 498e3f6a52e976d405b5e3343761695174bc3c78 (
plain)
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
|
use std::io::Write;
use std::io::stdout;
use termion::raw::{IntoRawMode, RawTerminal};
use termion::screen::AlternateScreen;
use tui::backend::TermionBackend;
use tui::widgets::Widget;
pub struct TuiBackend {
pub terminal: Option<tui::Terminal<TermionBackend<AlternateScreen<RawTerminal<std::io::Stdout>>>>>,
}
impl TuiBackend {
pub fn new() -> std::io::Result<Self> {
let stdout = std::io::stdout().into_raw_mode()?;
let stdout = AlternateScreen::from(stdout);
let backend = TermionBackend::new(stdout);
let mut terminal = tui::Terminal::new(backend)?;
terminal.hide_cursor()?;
Ok(Self { terminal: Some(terminal) })
}
pub fn render<W>(&mut self, widget: &mut W)
where
W: Widget,
{
self.terminal_mut().draw(|mut frame| {
let rect = frame.size();
widget.render(&mut frame, rect);
});
}
pub fn terminal_mut(&mut self) -> &mut tui::Terminal<TermionBackend<AlternateScreen<RawTerminal<std::io::Stdout>>>> {
self.terminal.as_mut().unwrap()
}
pub fn terminal_drop(&mut self) {
let _ = self.terminal.take();
stdout().flush();
}
pub fn terminal_restore(&mut self) -> std::io::Result<()> {
let mut new_backend = TuiBackend::new()?;
std::mem::swap(&mut self.terminal, &mut new_backend.terminal);
Ok(())
}
}
|