Static TUI mock-up
This commit is contained in:
parent
0ca084cacc
commit
147e663450
@ -1,4 +1,10 @@
|
|||||||
use crate::{database::{Database, self}, library::{Library, Query, self}, Artist};
|
use std::fmt;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
database::{self, Database},
|
||||||
|
library::{self, Library, Query},
|
||||||
|
Artist,
|
||||||
|
};
|
||||||
|
|
||||||
/// The collection type.
|
/// The collection type.
|
||||||
pub type Collection = Vec<Artist>;
|
pub type Collection = Vec<Artist>;
|
||||||
@ -12,6 +18,17 @@ pub enum Error {
|
|||||||
DatabaseError(String),
|
DatabaseError(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Error {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match *self {
|
||||||
|
Self::LibraryError(ref s) => write!(f, "failed to read/write from/to the library: {s}"),
|
||||||
|
Self::DatabaseError(ref s) => {
|
||||||
|
write!(f, "failed to read/write from/to the database: {s}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<library::Error> for Error {
|
impl From<library::Error> for Error {
|
||||||
fn from(err: library::Error) -> Error {
|
fn from(err: library::Error) -> Error {
|
||||||
Error::LibraryError(err.to_string())
|
Error::LibraryError(err.to_string())
|
||||||
@ -51,4 +68,8 @@ impl CollectionManager {
|
|||||||
self.database.write(&self.collection)?;
|
self.database.write(&self.collection)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_collection(&self) -> &Collection {
|
||||||
|
&self.collection
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
34
src/main.rs
34
src/main.rs
@ -13,7 +13,7 @@ use musichoard::{
|
|||||||
library::{
|
library::{
|
||||||
beets::{BeetsLibrary, BeetsLibraryCommandExecutor},
|
beets::{BeetsLibrary, BeetsLibraryCommandExecutor},
|
||||||
Library, Query,
|
Library, Query,
|
||||||
},
|
}, collection::CollectionManager,
|
||||||
};
|
};
|
||||||
|
|
||||||
mod tui;
|
mod tui;
|
||||||
@ -46,17 +46,19 @@ struct Opt {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Create the application.
|
// Create the application.
|
||||||
let mut app = App::new();
|
let opt = Opt::from_args();
|
||||||
|
|
||||||
// let opt = Opt::from_args();
|
let beets = BeetsLibrary::new(Box::new(
|
||||||
|
BeetsLibraryCommandExecutor::default().config(opt.beets_config_file_path.as_deref()),
|
||||||
|
));
|
||||||
|
|
||||||
// let mut beets = BeetsLibrary::new(Box::new(
|
let database = JsonDatabase::new(Box::new(JsonDatabaseFileBackend::new(
|
||||||
// BeetsLibraryCommandExecutor::default().config(opt.beets_config_file_path.as_deref()),
|
&opt.database_file_path,
|
||||||
// ));
|
)));
|
||||||
|
|
||||||
// let collection = beets
|
let collection_manager = CollectionManager::new(Box::new(beets), Box::new(database));
|
||||||
// .list(&Query::new())
|
|
||||||
// .expect("failed to query the library");
|
let mut app = App::new(collection_manager).expect("failed to initialise app");
|
||||||
|
|
||||||
// Initialize the terminal user interface.
|
// Initialize the terminal user interface.
|
||||||
let backend = CrosstermBackend::new(io::stdout());
|
let backend = CrosstermBackend::new(io::stdout());
|
||||||
@ -65,11 +67,9 @@ fn main() {
|
|||||||
let mut tui = Tui::new(terminal, events);
|
let mut tui = Tui::new(terminal, events);
|
||||||
tui.init();
|
tui.init();
|
||||||
|
|
||||||
// Start the main loop.
|
// Main loop.
|
||||||
while app.running {
|
while app.is_running() {
|
||||||
// Render the user interface.
|
|
||||||
tui.draw(&mut app);
|
tui.draw(&mut app);
|
||||||
// Handle events.
|
|
||||||
match tui.events.next_event() {
|
match tui.events.next_event() {
|
||||||
Event::Key(key_event) => handle_key_events(key_event, &mut app),
|
Event::Key(key_event) => handle_key_events(key_event, &mut app),
|
||||||
Event::Mouse(_) => {}
|
Event::Mouse(_) => {}
|
||||||
@ -79,12 +79,4 @@ fn main() {
|
|||||||
|
|
||||||
// Exit the user interface.
|
// Exit the user interface.
|
||||||
tui.exit();
|
tui.exit();
|
||||||
|
|
||||||
// let mut database = JsonDatabase::new(Box::new(JsonDatabaseFileBackend::new(
|
|
||||||
// &opt.database_file_path,
|
|
||||||
// )));
|
|
||||||
|
|
||||||
// database
|
|
||||||
// .write(&collection)
|
|
||||||
// .expect("failed to write to the database");
|
|
||||||
}
|
}
|
||||||
|
@ -1,44 +1,32 @@
|
|||||||
/// Application.
|
use musichoard::collection::{CollectionManager, Collection};
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct App {
|
|
||||||
/// Is the application running?
|
|
||||||
pub running: bool,
|
|
||||||
/// counter
|
|
||||||
pub counter: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for App {
|
use super::Error;
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
/// Application.
|
||||||
running: true,
|
pub struct App {
|
||||||
counter: 0,
|
collection_manager: CollectionManager,
|
||||||
}
|
running: bool,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
/// Constructs a new instance of [`App`].
|
/// Constructs a new instance of [`App`].
|
||||||
pub fn new() -> Self {
|
pub fn new(mut collection_manager: CollectionManager) -> Result<Self, Error> {
|
||||||
Self::default()
|
collection_manager.rescan_library()?;
|
||||||
|
Ok(App { collection_manager, running: true })
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handles the tick event of the terminal.
|
/// Whether the app is running.
|
||||||
pub fn tick(&self) {}
|
pub fn is_running(&self) -> bool {
|
||||||
|
self.running
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the list of artists.
|
||||||
|
pub fn get_collection(&self) -> &Collection {
|
||||||
|
self.collection_manager.get_collection()
|
||||||
|
}
|
||||||
|
|
||||||
/// Set running to false to quit the application.
|
/// Set running to false to quit the application.
|
||||||
pub fn quit(&mut self) {
|
pub fn quit(&mut self) {
|
||||||
self.running = false;
|
self.running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn increment_counter(&mut self) {
|
|
||||||
if let Some(res) = self.counter.checked_add(1) {
|
|
||||||
self.counter = res;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn decrement_counter(&mut self) {
|
|
||||||
if let Some(res) = self.counter.checked_sub(1) {
|
|
||||||
self.counter = res;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -15,13 +15,6 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) {
|
|||||||
app.quit();
|
app.quit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Counter handlers
|
|
||||||
KeyCode::Right => {
|
|
||||||
app.increment_counter();
|
|
||||||
}
|
|
||||||
KeyCode::Left => {
|
|
||||||
app.decrement_counter();
|
|
||||||
}
|
|
||||||
// Other handlers you could add here.
|
// Other handlers you could add here.
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
|
use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
|
||||||
use crossterm::terminal::{self, EnterAlternateScreen, LeaveAlternateScreen};
|
use crossterm::terminal::{self, EnterAlternateScreen, LeaveAlternateScreen};
|
||||||
|
use musichoard::collection;
|
||||||
use ratatui::backend::Backend;
|
use ratatui::backend::Backend;
|
||||||
use ratatui::Terminal;
|
use ratatui::Terminal;
|
||||||
use std::io;
|
use std::io;
|
||||||
@ -14,6 +15,19 @@ use event::EventHandler;
|
|||||||
|
|
||||||
use self::app::App;
|
use self::app::App;
|
||||||
|
|
||||||
|
/// 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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Representation of a terminal user interface.
|
/// Representation of a terminal user interface.
|
||||||
///
|
///
|
||||||
/// It is responsible for setting up the terminal,
|
/// It is responsible for setting up the terminal,
|
||||||
|
157
src/tui/ui.rs
157
src/tui/ui.rs
@ -1,8 +1,9 @@
|
|||||||
|
use musichoard::{collection::Collection, TrackFormat};
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
backend::Backend,
|
backend::Backend,
|
||||||
layout::Alignment,
|
layout::{Alignment, Rect},
|
||||||
style::{Color, Style},
|
style::{Color, Modifier, Style},
|
||||||
widgets::{Block, BorderType, Borders, Paragraph},
|
widgets::{Block, BorderType, Borders, List, ListItem, Paragraph},
|
||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -14,23 +15,149 @@ pub fn render<B: Backend>(app: &mut App, frame: &mut Frame<'_, B>) {
|
|||||||
// See the following resources:
|
// See the following resources:
|
||||||
// - https://docs.rs/ratatui/latest/ratatui/widgets/index.html
|
// - https://docs.rs/ratatui/latest/ratatui/widgets/index.html
|
||||||
// - https://github.com/tui-rs-revival/ratatui/tree/master/examples
|
// - https://github.com/tui-rs-revival/ratatui/tree/master/examples
|
||||||
|
|
||||||
|
let collection: &Collection = app.get_collection();
|
||||||
|
|
||||||
|
let artists: Vec<ListItem> = collection
|
||||||
|
.iter()
|
||||||
|
.map(|a| ListItem::new(a.id.name.as_str()))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let frame_rect = frame.size();
|
||||||
|
let width_over_three = frame_rect.width / 3;
|
||||||
|
let height_over_three = frame_rect.height / 3;
|
||||||
|
|
||||||
|
let artists_rect = Rect {
|
||||||
|
x: frame_rect.x,
|
||||||
|
y: frame_rect.y,
|
||||||
|
width: width_over_three,
|
||||||
|
height: frame_rect.height,
|
||||||
|
};
|
||||||
|
|
||||||
frame.render_widget(
|
frame.render_widget(
|
||||||
Paragraph::new(format!(
|
List::new(artists)
|
||||||
"This is a tui template.\n\
|
.highlight_style(Style::default().add_modifier(Modifier::ITALIC))
|
||||||
Press `Esc`, `Ctrl-C` or `q` to stop running.\n\
|
.highlight_symbol(">>")
|
||||||
Press left and right to increment and decrement the counter respectively.\n\
|
.style(Style::default().fg(Color::Cyan).bg(Color::Black))
|
||||||
Counter: {}",
|
|
||||||
app.counter
|
|
||||||
))
|
|
||||||
.block(
|
.block(
|
||||||
Block::default()
|
Block::default()
|
||||||
.title("Template")
|
.title("Artists")
|
||||||
.title_alignment(Alignment::Center)
|
.title_alignment(Alignment::Center)
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
.border_type(BorderType::Rounded),
|
.border_type(BorderType::Rounded),
|
||||||
)
|
),
|
||||||
|
artists_rect,
|
||||||
|
);
|
||||||
|
|
||||||
|
let albums: Vec<ListItem> = collection[1]
|
||||||
|
.albums
|
||||||
|
.iter()
|
||||||
|
.map(|a| ListItem::new(a.id.title.as_str()))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let albums_rect = Rect {
|
||||||
|
x: artists_rect.x + artists_rect.width,
|
||||||
|
y: frame_rect.y,
|
||||||
|
width: width_over_three,
|
||||||
|
height: frame_rect.height - height_over_three,
|
||||||
|
};
|
||||||
|
|
||||||
|
frame.render_widget(
|
||||||
|
List::new(albums)
|
||||||
|
.highlight_style(Style::default().add_modifier(Modifier::ITALIC))
|
||||||
|
.highlight_symbol(">>")
|
||||||
.style(Style::default().fg(Color::Cyan).bg(Color::Black))
|
.style(Style::default().fg(Color::Cyan).bg(Color::Black))
|
||||||
.alignment(Alignment::Center),
|
.block(
|
||||||
frame.size(),
|
Block::default()
|
||||||
)
|
.title("Albums")
|
||||||
|
.title_alignment(Alignment::Center)
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_type(BorderType::Rounded),
|
||||||
|
),
|
||||||
|
albums_rect,
|
||||||
|
);
|
||||||
|
|
||||||
|
let albums_info_rect = Rect {
|
||||||
|
x: albums_rect.x,
|
||||||
|
y: albums_rect.y + albums_rect.height,
|
||||||
|
width: albums_rect.width,
|
||||||
|
height: height_over_three,
|
||||||
|
};
|
||||||
|
|
||||||
|
frame.render_widget(
|
||||||
|
Paragraph::new(format!(
|
||||||
|
"Title: {}\n\
|
||||||
|
Year: {}",
|
||||||
|
collection[1].albums[1].id.title, collection[1].albums[1].id.year,
|
||||||
|
))
|
||||||
|
.style(Style::default().fg(Color::Cyan).bg(Color::Black))
|
||||||
|
.block(
|
||||||
|
Block::default()
|
||||||
|
.title("Album info")
|
||||||
|
.title_alignment(Alignment::Center)
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_type(BorderType::Rounded),
|
||||||
|
),
|
||||||
|
albums_info_rect,
|
||||||
|
);
|
||||||
|
|
||||||
|
let tracks: Vec<ListItem> = collection[1].albums[1]
|
||||||
|
.tracks
|
||||||
|
.iter()
|
||||||
|
.map(|t| ListItem::new(t.title.as_str()))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let tracks_rect = Rect {
|
||||||
|
x: albums_rect.x + albums_rect.width,
|
||||||
|
y: frame_rect.y,
|
||||||
|
width: frame_rect.width - 2 * width_over_three,
|
||||||
|
height: frame_rect.height - height_over_three,
|
||||||
|
};
|
||||||
|
|
||||||
|
frame.render_widget(
|
||||||
|
List::new(tracks)
|
||||||
|
.highlight_style(Style::default().add_modifier(Modifier::ITALIC))
|
||||||
|
.highlight_symbol(">>")
|
||||||
|
.style(Style::default().fg(Color::Cyan).bg(Color::Black))
|
||||||
|
.block(
|
||||||
|
Block::default()
|
||||||
|
.title("Tracks")
|
||||||
|
.title_alignment(Alignment::Center)
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_type(BorderType::Rounded),
|
||||||
|
),
|
||||||
|
tracks_rect,
|
||||||
|
);
|
||||||
|
|
||||||
|
let track_info_rect = Rect {
|
||||||
|
x: tracks_rect.x,
|
||||||
|
y: tracks_rect.y + tracks_rect.height,
|
||||||
|
width: tracks_rect.width,
|
||||||
|
height: height_over_three,
|
||||||
|
};
|
||||||
|
|
||||||
|
frame.render_widget(
|
||||||
|
Paragraph::new(format!(
|
||||||
|
"Track: {}\n\
|
||||||
|
Title: {}\n\
|
||||||
|
Artist: {}\n\
|
||||||
|
Format: {}",
|
||||||
|
collection[1].albums[1].tracks[1].number,
|
||||||
|
collection[1].albums[1].tracks[1].title,
|
||||||
|
collection[1].albums[1].tracks[1].artist.join("; "),
|
||||||
|
match collection[1].albums[1].tracks[1].format {
|
||||||
|
TrackFormat::Flac => "FLAC",
|
||||||
|
TrackFormat::Mp3 => "MP3",
|
||||||
|
},
|
||||||
|
))
|
||||||
|
.style(Style::default().fg(Color::Cyan).bg(Color::Black))
|
||||||
|
.block(
|
||||||
|
Block::default()
|
||||||
|
.title("Track info")
|
||||||
|
.title_alignment(Alignment::Center)
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_type(BorderType::Rounded),
|
||||||
|
),
|
||||||
|
track_info_rect,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user