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
74
75
76
77
78
#[cfg(not(windows))]
use super::super::state::commands::unix_command::EnableRawModeCommand;
#[cfg(windows)]
use state::commands::win_commands::EnableRawModeCommand;
use state::commands::IStateCommand;
use {CommandManager, Context};
use std::io::{self, Write};
use std::rc::Rc;
pub struct RawTerminal {
context: Rc<Context>,
command_id: u16,
}
pub trait IntoRawMode: Write + Sized {
fn into_raw_mode(&self, context: Rc<Context>) -> io::Result<RawTerminal>;
}
impl<W: Write> IntoRawMode for W {
fn into_raw_mode(&self, context: Rc<Context>) -> io::Result<RawTerminal> {
let command_id = EnableRawModeCommand::new(&context.state_manager);
let success = CommandManager::execute(context.clone(), command_id);
if success {
Ok(RawTerminal {
context: context.clone(),
command_id: command_id,
})
} else {
panic!("cannot move into raw mode")
}
}
}
impl Write for RawTerminal {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let mut screen = self.context.screen_manager.lock().unwrap();
{
screen.write(buf)
}
}
fn flush(&mut self) -> io::Result<()> {
let mut screen = self.context.screen_manager.lock().unwrap();
{
screen.flush()
}
}
}
impl Drop for RawTerminal {
fn drop(&mut self) {
let success = CommandManager::undo(self.context.clone(), self.command_id);
}
}