Skip to content

Commit

Permalink
refactor: rename the modules
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed May 15, 2024
1 parent 54dbaca commit 5c4b834
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 360 deletions.
3 changes: 0 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ impl App {
}
}

/// Handles the tick event of the terminal.
pub fn tick(&self) {}

/// Set running to false to quit the application.
pub fn quit(&mut self) {
self.running = false;
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
/// Application.
pub mod app;

/// Terminal events handler.
/// Event handler.
pub mod event;

/// Widget renderer.
pub mod ui;

/// Terminal user interface.
/// TUI renderer.
pub mod tui;

/// Terminal handler
pub mod terminal;

/// Application theme.
pub mod theme;

Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use flawz::cve::Cve;
use flawz::error::Error;
use flawz::event::{Event, EventHandler};
use flawz::handler::handle_key_events;
use flawz::tui::Tui;
use flawz::terminal::Tui;
use flawz::widgets::SelectableList;
use ratatui::backend::CrosstermBackend;
use ratatui::Terminal;
Expand Down Expand Up @@ -59,7 +59,7 @@ fn main() -> AppResult<()> {
tui.draw(&mut app)?;
// Handle events.
match tui.events.next()? {
Event::Tick => app.tick(),
Event::Tick => {}
Event::Key(key_event) => handle_key_events(key_event, &mut app, &tui.events.sender)?,
Event::Mouse(_) => {}
Event::Resize(_, _) => {}
Expand Down
76 changes: 76 additions & 0 deletions src/terminal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use crate::app::{App, AppResult};
use crate::event::EventHandler;
use crate::tui;
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<B: Backend> {
/// Interface to the Terminal.
terminal: Terminal<B>,
/// Terminal event handler.
pub events: EventHandler,
}

impl<B: Backend> Tui<B> {
/// Constructs a new instance of [`Tui`].
pub fn new(terminal: Terminal<B>, events: EventHandler) -> Self {
Self { terminal, events }
}

/// Initializes the terminal interface.
///
/// It enables the raw mode and sets terminal properties.
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::tui::render
pub fn draw(&mut self, app: &mut App) -> AppResult<()> {
self.terminal.draw(|frame| tui::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.
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.
pub fn exit(&mut self) -> AppResult<()> {
Self::reset()?;
self.terminal.show_cursor()?;
Ok(())
}
}
Loading

0 comments on commit 5c4b834

Please sign in to comment.