thedes_tui_core/runtime/device/
native.rs1use crossterm::terminal;
2
3use crate::{
4 input::{self, device::InputDevice},
5 panic,
6 screen::{self, device::ScreenDevice},
7};
8
9use super::{Error, PanicRestoreGuard, RuntimeDevice};
10
11pub fn open() -> Box<dyn RuntimeDevice> {
12 Box::new(NativeRuntimeDevice::new())
13}
14
15#[derive(Debug)]
16struct NativeRuntimeDevice {
17 initialized: bool,
18}
19
20impl NativeRuntimeDevice {
21 pub fn new() -> Self {
22 Self { initialized: false }
23 }
24}
25
26impl RuntimeDevice for NativeRuntimeDevice {
27 fn blocking_init(&mut self) -> Result<(), Error> {
28 if self.initialized {
29 Err(Error::AlreadyInit)?
30 }
31 terminal::enable_raw_mode()?;
32 self.initialized = true;
33 Ok(())
34 }
35
36 fn blocking_shutdown(&mut self) -> Result<(), Error> {
37 if !self.initialized {
38 Err(Error::NotInit)?
39 }
40 terminal::disable_raw_mode()?;
41 self.initialized = false;
42 Ok(())
43 }
44
45 fn open_input_device(&mut self) -> Box<dyn InputDevice> {
46 input::device::native::open()
47 }
48
49 fn open_screen_device(&mut self) -> Box<dyn ScreenDevice> {
50 screen::device::native::open()
51 }
52
53 fn open_panic_restore_guard(&mut self) -> Box<dyn PanicRestoreGuard> {
54 panic::restore::native::open()
55 }
56}