-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.rs
62 lines (48 loc) · 1.79 KB
/
mod.rs
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
use std::io::Write;
use anyhow::Result;
use crossterm::{terminal::{enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode}, execute, event::{EnableMouseCapture, DisableMouseCapture}};
use tui::{backend::Backend, Terminal};
pub struct TerminalSettings<B: Backend + Write> {
terminal: Terminal<B>,
}
impl<B: Backend + Write> TerminalSettings<B> {
pub fn mangle_terminal<W, F>(mut stream: W, backend: F) -> Result<Self>
where
W: Write,
F: Fn(W) -> B,
{
enable_raw_mode()?;
std::panic::set_hook(Box::new(|panic_info| {
use crossterm::{terminal, cursor};
let mut stdout = std::io::stdout();
execute!(stdout, cursor::MoveTo(0, 0)).unwrap();
execute!(stdout, terminal::Clear(terminal::ClearType::All)).unwrap();
execute!(stdout, terminal::LeaveAlternateScreen).unwrap();
execute!(stdout, cursor::Show).unwrap();
terminal::disable_raw_mode().unwrap();
let panic_handler = better_panic::Settings::auto()
.create_panic_handler();
panic_handler(panic_info);
}));
execute!(stream, EnterAlternateScreen, EnableMouseCapture)?;
let terminal = Terminal::new(backend(stream))?;
Ok(
Self {
terminal,
}
)
}
pub fn terminal_mut(&mut self) -> &mut Terminal<B> {
&mut self.terminal
}
fn unmangle_terminal(&mut self) {
disable_raw_mode().unwrap();
execute!(self.terminal.backend_mut(), LeaveAlternateScreen, DisableMouseCapture).unwrap();
self.terminal.show_cursor().unwrap();
}
}
impl<B: Backend + Write> Drop for TerminalSettings<B> {
fn drop(&mut self) {
self.unmangle_terminal();
}
}