-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Philipp Reiter
committed
Sep 7, 2024
1 parent
995572d
commit 694a60f
Showing
4 changed files
with
296 additions
and
375 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crossterm::{ | ||
event::{DisableMouseCapture, EnableMouseCapture}, | ||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, | ||
}; | ||
use ratatui::prelude::CrosstermBackend; | ||
use std::{ | ||
error::Error, | ||
io::{stdout, Stdout}, | ||
ops::{Deref, DerefMut}, | ||
}; | ||
|
||
pub type Result<T> = std::result::Result<T, Box<dyn Error>>; | ||
|
||
pub struct Terminal(ratatui::Terminal<CrosstermBackend<Stdout>>); | ||
|
||
impl Deref for Terminal { | ||
type Target = ratatui::Terminal<CrosstermBackend<Stdout>>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl DerefMut for Terminal { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
impl Terminal { | ||
pub fn init() -> Result<Self> { | ||
crossterm::execute!(stdout(), EnterAlternateScreen, EnableMouseCapture)?; | ||
enable_raw_mode()?; | ||
|
||
let backend = CrosstermBackend::new(stdout()); | ||
|
||
let mut terminal = ratatui::Terminal::new(backend)?; | ||
terminal.hide_cursor()?; | ||
|
||
fn panic_hook() { | ||
let original_hook = std::panic::take_hook(); | ||
|
||
std::panic::set_hook(Box::new(move |panic| { | ||
Terminal::reset().unwrap(); | ||
original_hook(panic); | ||
})); | ||
} | ||
|
||
panic_hook(); | ||
|
||
Ok(Self(terminal)) | ||
} | ||
|
||
pub fn reset() -> Result<()> { | ||
disable_raw_mode()?; | ||
crossterm::execute!(stdout(), LeaveAlternateScreen, DisableMouseCapture)?; | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.