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;
|
2023-04-12 10:20:20 +02:00
|
|
|
pub mod ui;
|
2023-04-11 09:04:00 +02:00
|
|
|
|
|
|
|
use self::app::App;
|
2023-04-12 15:41:57 +02:00
|
|
|
use self::event::{EventError, EventListener};
|
2023-04-12 10:20:20 +02:00
|
|
|
use self::handler::EventHandler;
|
|
|
|
use self::ui::Ui;
|
2023-04-11 09:04:00 +02:00
|
|
|
|
2023-04-11 13:52:49 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
CollectionError(String),
|
2023-04-12 15:41:57 +02:00
|
|
|
IoError(String),
|
|
|
|
EventError(String),
|
|
|
|
ListenerPanic,
|
2023-04-11 13:52:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<collection::Error> for Error {
|
|
|
|
fn from(err: collection::Error) -> Error {
|
|
|
|
Error::CollectionError(err.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 15:41:57 +02:00
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(err: io::Error) -> Error {
|
|
|
|
Error::IoError(err.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<EventError> for Error {
|
|
|
|
fn from(err: EventError) -> Error {
|
|
|
|
Error::EventError(err.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 09:04:00 +02:00
|
|
|
pub struct Tui<B: Backend> {
|
|
|
|
terminal: Terminal<B>,
|
2023-04-12 15:41:57 +02:00
|
|
|
listener: Option<EventListener>,
|
2023-04-12 10:20:20 +02:00
|
|
|
handler: EventHandler,
|
|
|
|
ui: Ui,
|
|
|
|
app: App,
|
2023-04-11 09:04:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<B: Backend> Tui<B> {
|
2023-04-12 10:20:20 +02:00
|
|
|
pub fn new(
|
|
|
|
terminal: Terminal<B>,
|
|
|
|
listener: EventListener,
|
|
|
|
handler: EventHandler,
|
|
|
|
ui: Ui,
|
|
|
|
app: App,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
terminal,
|
2023-04-12 15:41:57 +02:00
|
|
|
listener: Some(listener),
|
2023-04-12 10:20:20 +02:00
|
|
|
handler,
|
|
|
|
ui,
|
|
|
|
app,
|
|
|
|
}
|
2023-04-11 09:04:00 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 15:41:57 +02:00
|
|
|
fn init(&mut self) -> Result<(), Error> {
|
|
|
|
terminal::enable_raw_mode()?;
|
|
|
|
crossterm::execute!(io::stdout(), EnterAlternateScreen, EnableMouseCapture)?;
|
|
|
|
self.terminal.hide_cursor()?;
|
|
|
|
self.terminal.clear()?;
|
|
|
|
Ok(())
|
2023-04-11 09:04:00 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 15:41:57 +02:00
|
|
|
fn run_loop(&mut self) -> Result<(), Error> {
|
2023-04-12 10:20:20 +02:00
|
|
|
while self.app.is_running() {
|
|
|
|
self.terminal
|
2023-04-12 15:41:57 +02:00
|
|
|
.draw(|frame| self.ui.render(&mut self.app, frame))?;
|
|
|
|
self.handler.handle_next_event(&mut self.app)?;
|
2023-04-12 10:20:20 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 15:41:57 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(mut self) -> Result<(), Error> {
|
|
|
|
self.init()?;
|
|
|
|
|
|
|
|
let listener_handle = self.listener.take().unwrap().spawn();
|
|
|
|
let result = self.run_loop();
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(_) => {
|
|
|
|
self.exit()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
// We want to call exit before handling the run_loop result to reset the terminal.
|
|
|
|
// Therefore, we suppress exit errors (if any) to not mask the original error.
|
|
|
|
self.exit_suppress_errors();
|
|
|
|
|
|
|
|
if listener_handle.is_finished() {
|
|
|
|
match listener_handle.join() {
|
|
|
|
Ok(err) => return Err(err.into()),
|
|
|
|
// Calling std::panic::resume_unwind(err) as recommended by the Rust docs
|
|
|
|
// will not produce an error message. The panic error message is printed at
|
|
|
|
// the location of the panic which at the time is hidden by the TUI.
|
|
|
|
Err(_) => return Err(Error::ListenerPanic),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exit(&mut self) -> Result<(), Error> {
|
|
|
|
terminal::disable_raw_mode()?;
|
|
|
|
crossterm::execute!(io::stdout(), LeaveAlternateScreen, DisableMouseCapture)?;
|
|
|
|
self.terminal.show_cursor()?;
|
|
|
|
Ok(())
|
2023-04-11 09:04:00 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 15:41:57 +02:00
|
|
|
#[allow(unused_must_use)]
|
|
|
|
fn exit_suppress_errors(&mut self) {
|
|
|
|
self.exit();
|
2023-04-11 09:04:00 +02:00
|
|
|
}
|
|
|
|
}
|