2023-05-10 22:52:03 +02:00
|
|
|
pub mod event;
|
|
|
|
pub mod handler;
|
|
|
|
pub mod listener;
|
|
|
|
pub mod ui;
|
|
|
|
|
|
|
|
mod lib;
|
|
|
|
|
2023-04-13 14:09:59 +02:00
|
|
|
use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
|
|
|
|
use crossterm::terminal::{self, EnterAlternateScreen, LeaveAlternateScreen};
|
|
|
|
use ratatui::backend::Backend;
|
|
|
|
use ratatui::Terminal;
|
|
|
|
use std::io;
|
2023-04-13 15:29:14 +02:00
|
|
|
use std::marker::PhantomData;
|
2023-04-13 14:09:59 +02:00
|
|
|
|
|
|
|
use self::event::EventError;
|
2023-05-10 22:52:03 +02:00
|
|
|
use self::handler::IEventHandler;
|
|
|
|
use self::listener::IEventListener;
|
|
|
|
use self::ui::IUi;
|
2023-04-13 14:09:59 +02:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub enum Error {
|
2023-05-10 22:52:03 +02:00
|
|
|
Lib(String),
|
2023-04-13 14:09:59 +02:00
|
|
|
Io(String),
|
|
|
|
Event(String),
|
|
|
|
ListenerPanic,
|
|
|
|
}
|
|
|
|
|
2023-05-20 00:02:39 +02:00
|
|
|
impl From<musichoard::Error> for Error {
|
|
|
|
fn from(err: musichoard::Error) -> Error {
|
|
|
|
Error::Lib(err.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 14:09:59 +02:00
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(err: io::Error) -> Error {
|
|
|
|
Error::Io(err.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<EventError> for Error {
|
|
|
|
fn from(err: EventError) -> Error {
|
|
|
|
Error::Event(err.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 19:05:37 +02:00
|
|
|
pub struct Tui<B: Backend, UI> {
|
2023-04-13 14:09:59 +02:00
|
|
|
terminal: Terminal<B>,
|
2023-04-27 19:05:37 +02:00
|
|
|
_phantom: PhantomData<UI>,
|
2023-04-13 14:09:59 +02:00
|
|
|
}
|
|
|
|
|
2023-05-10 22:52:03 +02:00
|
|
|
impl<B: Backend, UI: IUi> Tui<B, UI> {
|
2023-04-13 14:09:59 +02:00
|
|
|
fn init(&mut self) -> Result<(), Error> {
|
|
|
|
self.terminal.hide_cursor()?;
|
|
|
|
self.terminal.clear()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exit(&mut self) -> Result<(), Error> {
|
|
|
|
self.terminal.show_cursor()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused_must_use)]
|
|
|
|
fn exit_suppress_errors(&mut self) {
|
|
|
|
self.exit();
|
|
|
|
}
|
|
|
|
|
2023-05-10 22:52:03 +02:00
|
|
|
fn main_loop(&mut self, mut ui: UI, handler: impl IEventHandler<UI>) -> Result<(), Error> {
|
2023-04-27 19:05:37 +02:00
|
|
|
while ui.is_running() {
|
|
|
|
self.terminal.draw(|frame| ui.render(frame))?;
|
|
|
|
handler.handle_next_event(&mut ui)?;
|
2023-04-13 14:09:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main(
|
|
|
|
term: Terminal<B>,
|
2023-04-27 19:05:37 +02:00
|
|
|
ui: UI,
|
2023-05-10 22:52:03 +02:00
|
|
|
handler: impl IEventHandler<UI>,
|
|
|
|
listener: impl IEventListener,
|
2023-04-13 14:09:59 +02:00
|
|
|
) -> Result<(), Error> {
|
2023-04-13 15:29:14 +02:00
|
|
|
let mut tui = Tui {
|
|
|
|
terminal: term,
|
|
|
|
_phantom: PhantomData,
|
|
|
|
};
|
2023-04-13 14:09:59 +02:00
|
|
|
|
|
|
|
tui.init()?;
|
|
|
|
|
|
|
|
let listener_handle = listener.spawn();
|
2023-04-27 19:05:37 +02:00
|
|
|
let result = tui.main_loop(ui, handler);
|
2023-04-13 14:09:59 +02:00
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(_) => {
|
|
|
|
tui.exit()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
// We want to call exit before handling the result to reset the terminal. Therefore,
|
|
|
|
// we suppress exit errors (if any) to not mask the original error.
|
|
|
|
tui.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GRCOV_EXCL_START
|
|
|
|
fn enable() -> Result<(), Error> {
|
|
|
|
terminal::enable_raw_mode()?;
|
|
|
|
crossterm::execute!(io::stdout(), EnterAlternateScreen, EnableMouseCapture)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn disable() -> Result<(), Error> {
|
|
|
|
terminal::disable_raw_mode()?;
|
|
|
|
crossterm::execute!(io::stdout(), LeaveAlternateScreen, DisableMouseCapture)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused_must_use)]
|
|
|
|
fn disable_suppress_errors() {
|
|
|
|
Self::disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(
|
|
|
|
term: Terminal<B>,
|
2023-04-27 19:05:37 +02:00
|
|
|
ui: UI,
|
2023-05-10 22:52:03 +02:00
|
|
|
handler: impl IEventHandler<UI>,
|
|
|
|
listener: impl IEventListener,
|
2023-04-13 14:09:59 +02:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
Self::enable()?;
|
2023-04-27 19:05:37 +02:00
|
|
|
let result = Self::main(term, ui, handler, listener);
|
2023-04-13 14:09:59 +02:00
|
|
|
match result {
|
|
|
|
Ok(_) => {
|
|
|
|
Self::disable()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
// We want to call disable before handling the result to reset the terminal.
|
|
|
|
// Therefore, we suppress disable errors (if any) to not mask the original error.
|
|
|
|
Self::disable_suppress_errors();
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// GRCOV_EXCL_STOP
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::{io, thread};
|
|
|
|
|
2023-05-10 22:52:03 +02:00
|
|
|
use musichoard::Collection;
|
2023-04-13 14:09:59 +02:00
|
|
|
use ratatui::{backend::TestBackend, Terminal};
|
|
|
|
|
2023-05-10 22:52:03 +02:00
|
|
|
use crate::tests::COLLECTION;
|
2023-04-13 14:09:59 +02:00
|
|
|
|
|
|
|
use super::{
|
2023-04-13 15:29:14 +02:00
|
|
|
event::EventError,
|
2023-05-10 22:52:03 +02:00
|
|
|
handler::MockIEventHandler,
|
|
|
|
lib::MockIMusicHoard,
|
|
|
|
listener::MockIEventListener,
|
|
|
|
ui::{IUi, Ui},
|
2023-04-13 15:29:14 +02:00
|
|
|
Error, Tui,
|
2023-04-13 14:09:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
pub fn terminal() -> Terminal<TestBackend> {
|
|
|
|
let backend = TestBackend::new(150, 30);
|
|
|
|
Terminal::new(backend).unwrap()
|
|
|
|
}
|
|
|
|
|
2023-05-20 00:02:39 +02:00
|
|
|
pub fn music_hoard(collection: Collection) -> MockIMusicHoard {
|
2023-05-10 22:52:03 +02:00
|
|
|
let mut music_hoard = MockIMusicHoard::new();
|
2023-04-13 14:09:59 +02:00
|
|
|
|
2023-05-20 00:02:39 +02:00
|
|
|
music_hoard.expect_load_from_database().returning(|| Ok(()));
|
2023-05-10 22:52:03 +02:00
|
|
|
music_hoard.expect_rescan_library().returning(|| Ok(()));
|
|
|
|
music_hoard.expect_get_collection().return_const(collection);
|
2023-04-13 14:09:59 +02:00
|
|
|
|
2023-05-20 00:02:39 +02:00
|
|
|
music_hoard
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ui(collection: Collection) -> Ui<MockIMusicHoard> {
|
|
|
|
Ui::new(music_hoard(collection)).unwrap()
|
2023-04-13 14:09:59 +02:00
|
|
|
}
|
|
|
|
|
2023-05-10 22:52:03 +02:00
|
|
|
fn listener() -> MockIEventListener {
|
|
|
|
let mut listener = MockIEventListener::new();
|
2023-04-13 14:09:59 +02:00
|
|
|
listener.expect_spawn().return_once(|| {
|
|
|
|
thread::spawn(|| {
|
|
|
|
thread::park();
|
2024-01-06 11:14:30 +01:00
|
|
|
EventError::Io(io::Error::new(io::ErrorKind::Interrupted, "unparked"))
|
2023-04-13 14:09:59 +02:00
|
|
|
})
|
|
|
|
});
|
|
|
|
listener
|
|
|
|
}
|
|
|
|
|
2023-05-10 22:52:03 +02:00
|
|
|
fn handler() -> MockIEventHandler<Ui<MockIMusicHoard>> {
|
|
|
|
let mut handler = MockIEventHandler::new();
|
2023-04-27 19:05:37 +02:00
|
|
|
handler
|
|
|
|
.expect_handle_next_event()
|
2023-05-10 22:52:03 +02:00
|
|
|
.return_once(|ui: &mut Ui<MockIMusicHoard>| {
|
2023-04-27 19:05:37 +02:00
|
|
|
ui.quit();
|
2023-04-13 15:29:14 +02:00
|
|
|
Ok(())
|
2023-04-27 19:05:37 +02:00
|
|
|
});
|
2023-04-13 14:09:59 +02:00
|
|
|
handler
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run() {
|
|
|
|
let terminal = terminal();
|
2023-04-27 19:05:37 +02:00
|
|
|
let ui = ui(COLLECTION.to_owned());
|
2023-04-13 14:09:59 +02:00
|
|
|
|
|
|
|
let listener = listener();
|
|
|
|
let handler = handler();
|
|
|
|
|
2023-04-27 19:05:37 +02:00
|
|
|
let result = Tui::main(terminal, ui, handler, listener);
|
2023-04-13 14:09:59 +02:00
|
|
|
assert!(result.is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn event_error() {
|
|
|
|
let terminal = terminal();
|
2023-04-27 19:05:37 +02:00
|
|
|
let ui = ui(COLLECTION.to_owned());
|
2023-04-13 14:09:59 +02:00
|
|
|
|
|
|
|
let listener = listener();
|
|
|
|
|
2023-05-10 22:52:03 +02:00
|
|
|
let mut handler = MockIEventHandler::new();
|
2023-04-13 14:09:59 +02:00
|
|
|
handler
|
|
|
|
.expect_handle_next_event()
|
|
|
|
.return_once(|_| Err(EventError::Recv));
|
|
|
|
|
2023-04-27 19:05:37 +02:00
|
|
|
let result = Tui::main(terminal, ui, handler, listener);
|
2023-04-13 14:09:59 +02:00
|
|
|
assert!(result.is_err());
|
|
|
|
assert_eq!(
|
|
|
|
result.unwrap_err(),
|
|
|
|
Error::Event(EventError::Recv.to_string())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn listener_error() {
|
|
|
|
let terminal = terminal();
|
2023-04-27 19:05:37 +02:00
|
|
|
let ui = ui(COLLECTION.to_owned());
|
2023-04-13 14:09:59 +02:00
|
|
|
|
|
|
|
let error = EventError::Io(io::Error::new(io::ErrorKind::Interrupted, "error"));
|
|
|
|
let listener_handle: thread::JoinHandle<EventError> = thread::spawn(|| error);
|
|
|
|
while !listener_handle.is_finished() {}
|
|
|
|
|
2023-05-10 22:52:03 +02:00
|
|
|
let mut listener = MockIEventListener::new();
|
2023-04-13 14:09:59 +02:00
|
|
|
listener.expect_spawn().return_once(|| listener_handle);
|
|
|
|
|
2023-05-10 22:52:03 +02:00
|
|
|
let mut handler = MockIEventHandler::new();
|
2023-04-13 14:09:59 +02:00
|
|
|
handler
|
|
|
|
.expect_handle_next_event()
|
|
|
|
.return_once(|_| Err(EventError::Recv));
|
|
|
|
|
2023-04-27 19:05:37 +02:00
|
|
|
let result = Tui::main(terminal, ui, handler, listener);
|
2023-04-13 14:09:59 +02:00
|
|
|
assert!(result.is_err());
|
|
|
|
|
|
|
|
let error = EventError::Io(io::Error::new(io::ErrorKind::Interrupted, "error"));
|
|
|
|
assert_eq!(result.unwrap_err(), Error::Event(error.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn listener_panic() {
|
|
|
|
let terminal = terminal();
|
2023-04-27 19:05:37 +02:00
|
|
|
let ui = ui(COLLECTION.to_owned());
|
2023-04-13 14:09:59 +02:00
|
|
|
|
|
|
|
let listener_handle: thread::JoinHandle<EventError> = thread::spawn(|| panic!());
|
|
|
|
while !listener_handle.is_finished() {}
|
|
|
|
|
2023-05-10 22:52:03 +02:00
|
|
|
let mut listener = MockIEventListener::new();
|
2023-04-13 14:09:59 +02:00
|
|
|
listener.expect_spawn().return_once(|| listener_handle);
|
|
|
|
|
2023-05-10 22:52:03 +02:00
|
|
|
let mut handler = MockIEventHandler::new();
|
2023-04-13 14:09:59 +02:00
|
|
|
handler
|
|
|
|
.expect_handle_next_event()
|
|
|
|
.return_once(|_| Err(EventError::Recv));
|
|
|
|
|
2023-04-27 19:05:37 +02:00
|
|
|
let result = Tui::main(terminal, ui, handler, listener);
|
2023-04-13 14:09:59 +02:00
|
|
|
assert!(result.is_err());
|
|
|
|
assert_eq!(result.unwrap_err(), Error::ListenerPanic);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn errors() {
|
2023-05-10 22:52:03 +02:00
|
|
|
let lib_err: Error = musichoard::Error::DatabaseError(String::from("")).into();
|
2023-04-13 14:09:59 +02:00
|
|
|
let io_err: Error = io::Error::new(io::ErrorKind::Interrupted, "error").into();
|
|
|
|
let event_err: Error = EventError::Recv.into();
|
|
|
|
let listener_err = Error::ListenerPanic;
|
|
|
|
|
2023-05-10 22:52:03 +02:00
|
|
|
assert!(!format!("{:?}", lib_err).is_empty());
|
2023-04-13 14:09:59 +02:00
|
|
|
assert!(!format!("{:?}", io_err).is_empty());
|
|
|
|
assert!(!format!("{:?}", event_err).is_empty());
|
|
|
|
assert!(!format!("{:?}", listener_err).is_empty());
|
|
|
|
}
|
|
|
|
}
|