use std::{io, panic};
use crossterm::{
event::{DisableMouseCapture, EnableMouseCapture},
terminal::{self, EnterAlternateScreen, LeaveAlternateScreen},
};
use crate::app::{event::EventHandler, ui, App};
pub type CrosstermTerminal = ratatui::Terminal<ratatui::backend::CrosstermBackend<std::io::Stderr>>;
pub struct Tui {
terminal: CrosstermTerminal,
pub events: EventHandler,
}
impl Tui {
pub fn new(terminal: CrosstermTerminal, events: EventHandler) -> Self {
Self { terminal, events }
}
pub fn enter(&mut self) -> Result<(), Box<dyn std::error::Error>> {
terminal::enable_raw_mode()?;
crossterm::execute!(io::stderr(), EnterAlternateScreen, EnableMouseCapture)?;
let panic_hook = panic::take_hook();
panic::set_hook(Box::new(move |panic| {
Self::reset().expect("failed to reset terminal");
panic_hook(panic);
}));
self.terminal.hide_cursor()?;
self.terminal.clear()?;
Ok(())
}
fn reset() -> Result<(), Box<dyn std::error::Error>> {
terminal::disable_raw_mode()?;
crossterm::execute!(io::stderr(), LeaveAlternateScreen, DisableMouseCapture)?;
Ok(())
}
pub fn exit(&mut self) -> Result<(), Box<dyn std::error::Error>> {
Self::reset()?;
self.terminal.show_cursor()?;
Ok(())
}
pub fn draw(&mut self, app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
self.terminal.draw(|frame| ui::render(app, frame))?;
Ok(())
}
}