1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- use crate::app::{App, AppResult};
- use crate::event::EventHandler;
- use crate::ui;
- use crossterm::event::DisableMouseCapture;
- use crossterm::terminal::{self, EnterAlternateScreen, LeaveAlternateScreen};
- use std::io;
- use std::panic;
- use ratatui::backend::Backend;
- use ratatui::Terminal;
- #[derive(Debug)]
- pub struct Tui<B: Backend> {
-
- terminal: Terminal<B>,
-
- pub events: EventHandler,
- }
- impl<B: Backend> Tui<B> {
-
- pub fn new(terminal: Terminal<B>, events: EventHandler) -> Self {
- Self { terminal, events }
- }
-
-
-
- pub fn init(&mut self) -> AppResult<()> {
- terminal::enable_raw_mode()?;
- crossterm::execute!(io::stderr(), EnterAlternateScreen, DisableMouseCapture)?;
-
-
- 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) -> AppResult<()> {
- self.terminal.draw(|frame| ui::render(app, frame))?;
- Ok(())
- }
-
-
-
-
- fn reset() -> AppResult<()> {
- terminal::disable_raw_mode()?;
- crossterm::execute!(io::stdout(), LeaveAlternateScreen, DisableMouseCapture)?;
- Ok(())
- }
-
-
-
- pub fn exit(&mut self) -> AppResult<()> {
- Self::reset()?;
- self.terminal.show_cursor()?;
- Ok(())
- }
- }
|