2023-04-11 09:04:00 +02:00
|
|
|
use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
|
|
|
|
use crossterm::terminal::{self, EnterAlternateScreen, LeaveAlternateScreen};
|
2023-04-11 13:52:49 +02:00
|
|
|
use musichoard::collection;
|
2023-04-11 09:04:00 +02:00
|
|
|
use ratatui::backend::Backend;
|
|
|
|
use ratatui::Terminal;
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
pub mod app;
|
|
|
|
pub mod event;
|
|
|
|
pub mod handler;
|
|
|
|
|
|
|
|
mod ui;
|
|
|
|
|
|
|
|
use event::EventHandler;
|
|
|
|
|
|
|
|
use self::app::App;
|
|
|
|
|
2023-04-11 13:52:49 +02:00
|
|
|
/// Error type for the TUI.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
/// The collection manager failed.
|
|
|
|
CollectionError(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<collection::Error> for Error {
|
|
|
|
fn from(err: collection::Error) -> Error {
|
|
|
|
Error::CollectionError(err.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 09:04:00 +02:00
|
|
|
/// 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) {
|
|
|
|
terminal::enable_raw_mode().unwrap();
|
|
|
|
crossterm::execute!(io::stdout(), EnterAlternateScreen, EnableMouseCapture).unwrap();
|
|
|
|
self.terminal.hide_cursor().unwrap();
|
|
|
|
self.terminal.clear().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// [`Draw`] the terminal interface by [`rendering`] the widgets.
|
|
|
|
///
|
|
|
|
/// [`Draw`]: tui::Terminal::draw
|
|
|
|
/// [`rendering`]: crate::ui:render
|
|
|
|
pub fn draw(&mut self, app: &mut App) {
|
|
|
|
self.terminal.draw(|frame| ui::render(app, frame)).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Exits the terminal interface.
|
|
|
|
///
|
|
|
|
/// It disables the raw mode and reverts back the terminal properties.
|
|
|
|
pub fn exit(&mut self) {
|
|
|
|
terminal::disable_raw_mode().unwrap();
|
|
|
|
crossterm::execute!(io::stdout(), LeaveAlternateScreen, DisableMouseCapture).unwrap();
|
|
|
|
self.terminal.show_cursor().unwrap();
|
|
|
|
}
|
|
|
|
}
|