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