// This file is part of Deja-vu. // // Deja-vu is free software: you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Deja-vu is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. // See the GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Deja-vu. If not, see . // use crate::app::{App, AppResult}; use crate::event::Handler; use crate::ui; use crossterm::event::{DisableMouseCapture, EnableMouseCapture}; use crossterm::terminal::{self, EnterAlternateScreen, LeaveAlternateScreen}; use ratatui::backend::Backend; use ratatui::Terminal; use std::io; use std::panic; /// Representation of a terminal user interface. /// /// It is responsible for setting up the terminal, /// initializing the interface and handling the draw events. #[derive(Debug)] pub struct Tui { /// Interface to the Terminal. terminal: Terminal, /// Terminal event handler. pub events: Handler, } impl Tui { /// Constructs a new instance of [`Tui`]. pub fn new(terminal: Terminal, events: Handler) -> Self { Self { terminal, events } } /// Initializes the terminal interface. /// /// It enables the raw mode and sets terminal properties. /// /// # Errors /// /// Will return error if `enable_raw_mode` fails /// /// # Panics /// /// Will panic if `reset` fails pub fn init(&mut self) -> AppResult<()> { terminal::enable_raw_mode()?; crossterm::execute!(io::stderr(), EnterAlternateScreen, EnableMouseCapture)?; // Define a custom panic hook to reset the terminal properties. // This way, you won't have your terminal messed up if an unexpected error happens. 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(()) } /// [`Draw`] the terminal interface by [`rendering`] the widgets. /// /// [`Draw`]: ratatui::Terminal::draw /// [`rendering`]: crate::ui::render /// /// # Errors /// /// Will return error if `draw` fails pub fn draw(&mut self, app: &mut App) -> AppResult<()> { self.terminal.draw(|frame| ui::render(app, frame))?; Ok(()) } /// Resets the terminal interface. /// /// This function is also used for the panic hook to revert /// the terminal properties if unexpected errors occur. /// /// # Errors /// /// Will return error if `disable_raw_mode` fails fn reset() -> AppResult<()> { terminal::disable_raw_mode()?; crossterm::execute!(io::stderr(), LeaveAlternateScreen, DisableMouseCapture)?; Ok(()) } /// Exits the terminal interface. /// /// It disables the raw mode and reverts back the terminal properties. /// /// # Errors /// /// Will return error if either `reset` or `show_cursor` fails pub fn exit(&mut self) -> AppResult<()> { Self::reset()?; self.terminal.show_cursor()?; Ok(()) } }