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 10:20:20 +02:00
|
|
|
use self::event::EventListener;
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
pub struct Tui<B: Backend> {
|
|
|
|
terminal: Terminal<B>,
|
2023-04-12 10:20:20 +02:00
|
|
|
listener: EventListener,
|
|
|
|
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,
|
|
|
|
listener,
|
|
|
|
handler,
|
|
|
|
ui,
|
|
|
|
app,
|
|
|
|
}
|
2023-04-11 09:04:00 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 10:20:20 +02:00
|
|
|
fn init(&mut self) {
|
2023-04-11 09:04:00 +02:00
|
|
|
terminal::enable_raw_mode().unwrap();
|
|
|
|
crossterm::execute!(io::stdout(), EnterAlternateScreen, EnableMouseCapture).unwrap();
|
|
|
|
self.terminal.hide_cursor().unwrap();
|
|
|
|
self.terminal.clear().unwrap();
|
|
|
|
}
|
|
|
|
|
2023-04-12 10:20:20 +02:00
|
|
|
pub fn run(&mut self) {
|
|
|
|
self.init();
|
|
|
|
|
|
|
|
self.listener.spawn();
|
|
|
|
while self.app.is_running() {
|
|
|
|
self.terminal
|
|
|
|
.draw(|frame| self.ui.render(&mut self.app, frame))
|
|
|
|
.unwrap();
|
|
|
|
self.handler.handle_next_event(&mut self.app);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.exit();
|
2023-04-11 09:04:00 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 10:20:20 +02:00
|
|
|
fn exit(&mut self) {
|
2023-04-11 09:04:00 +02:00
|
|
|
terminal::disable_raw_mode().unwrap();
|
|
|
|
crossterm::execute!(io::stdout(), LeaveAlternateScreen, DisableMouseCapture).unwrap();
|
|
|
|
self.terminal.show_cursor().unwrap();
|
|
|
|
}
|
|
|
|
}
|