diff options
| author | lukeflo | 2024-10-02 21:45:26 +0200 |
|---|---|---|
| committer | lukeflo | 2024-10-02 21:45:26 +0200 |
| commit | 36f01f65b1804125e7d18a17ce57205248e1b7fd (patch) | |
| tree | c46e63486a3c96448db091174efdcf343693dd9b /src/frontend/tui.rs | |
| parent | c78ae222ea75d36f4aef6e274d2c986e37462276 (diff) | |
| download | bibiman-36f01f65b1804125e7d18a17ce57205248e1b7fd.tar.gz bibiman-36f01f65b1804125e7d18a17ce57205248e1b7fd.zip | |
reordered tui handling
Diffstat (limited to 'src/frontend/tui.rs')
| -rw-r--r-- | src/frontend/tui.rs | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/src/frontend/tui.rs b/src/frontend/tui.rs index 477052f..add3e56 100644 --- a/src/frontend/tui.rs +++ b/src/frontend/tui.rs @@ -15,37 +15,50 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. ///// -use crate::frontend::app::{App, AppResult}; +use crate::frontend::app::App; use crate::frontend::event::EventHandler; +use color_eyre::eyre::Result; use crossterm::event::{DisableMouseCapture, EnableMouseCapture}; use crossterm::terminal::{self, EnterAlternateScreen, LeaveAlternateScreen}; -use ratatui::backend::Backend; +use ratatui::backend::{Backend, CrosstermBackend}; +// use ratatui::backend::CrosstermBackend as Backend; use ratatui::Terminal; -use std::io; +use std::io::{self, stdout, Stdout}; use std::panic; +use std::{ + ops::{Deref, DerefMut}, + time::Duration, +}; +// pub type IO = std::io::{{crossterm_io | title_case}}; +// pub fn io() -> IO { +// std::io::{{crossterm_io}}() +// } /// 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<B: Backend> { +pub struct Tui { /// Interface to the Terminal. - terminal: Terminal<B>, + terminal: ratatui::Terminal<CrosstermBackend<Stdout>>, /// Terminal event handler. pub events: EventHandler, } -impl<B: Backend> Tui<B> { +impl Tui { /// Constructs a new instance of [`Tui`]. - pub fn new(terminal: Terminal<B>, events: EventHandler) -> Self { - Self { terminal, events } + pub fn new() -> Result<Self> { + let backend = CrosstermBackend::new(stdout()); + let terminal = Terminal::new(backend)?; + let events = EventHandler::new(250); + Ok(Self { terminal, events }) } /// Initializes the terminal interface. /// /// It enables the raw mode and sets terminal properties. - pub fn init(&mut self) -> AppResult<()> { + pub fn init(&mut self) -> Result<()> { terminal::enable_raw_mode()?; crossterm::execute!(io::stdout(), EnterAlternateScreen, EnableMouseCapture)?; @@ -66,7 +79,7 @@ impl<B: Backend> Tui<B> { /// /// [`Draw`]: ratatui::Terminal::draw /// [`rendering`]: crate::ui::render - pub fn draw(&mut self, app: &mut App) -> AppResult<()> { + pub fn draw(&mut self, app: &mut App) -> Result<()> { // self.terminal.draw(|frame| ui::render(app, frame))?; self.terminal .draw(|frame| frame.render_widget(app, frame.area()))?; @@ -77,7 +90,7 @@ impl<B: Backend> Tui<B> { /// /// This function is also used for the panic hook to revert /// the terminal properties if unexpected errors occur. - fn reset() -> AppResult<()> { + fn reset() -> Result<()> { terminal::disable_raw_mode()?; crossterm::execute!(io::stdout(), LeaveAlternateScreen, DisableMouseCapture)?; Ok(()) @@ -86,7 +99,7 @@ impl<B: Backend> Tui<B> { /// Exits the terminal interface. /// /// It disables the raw mode and reverts back the terminal properties. - pub fn exit(&mut self) -> AppResult<()> { + pub fn exit(&mut self) -> Result<()> { Self::reset()?; self.terminal.show_cursor()?; Ok(()) |
