Port the tui-rs template code
This commit is contained in:
parent
4a71015aaf
commit
1c29c68321
@ -10,7 +10,7 @@ crossterm = "0.26.1"
|
||||
serde = { version = "1.0.159", features = ["derive"] }
|
||||
serde_json = "1.0.95"
|
||||
structopt = "0.3.26"
|
||||
tui = { package = "ratatui", version = "0.20.1" }
|
||||
ratatui = "0.20.1"
|
||||
uuid = { version = "1.3.0", features = ["serde"] }
|
||||
|
||||
[dev-dependencies]
|
||||
|
62
src/main.rs
62
src/main.rs
@ -1,3 +1,6 @@
|
||||
use ratatui::backend::CrosstermBackend;
|
||||
use ratatui::Terminal;
|
||||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use structopt::StructOpt;
|
||||
@ -13,6 +16,14 @@ use musichoard::{
|
||||
},
|
||||
};
|
||||
|
||||
mod tui;
|
||||
use tui::{
|
||||
app::App,
|
||||
event::{Event, EventHandler},
|
||||
handler::handle_key_events,
|
||||
Tui,
|
||||
};
|
||||
|
||||
#[derive(StructOpt)]
|
||||
struct Opt {
|
||||
#[structopt(
|
||||
@ -34,21 +45,46 @@ struct Opt {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let opt = Opt::from_args();
|
||||
// Create the application.
|
||||
let mut app = App::new();
|
||||
|
||||
let mut beets = BeetsLibrary::new(Box::new(
|
||||
BeetsLibraryCommandExecutor::default().config(opt.beets_config_file_path.as_deref()),
|
||||
));
|
||||
// let opt = Opt::from_args();
|
||||
|
||||
let collection = beets
|
||||
.list(&Query::new())
|
||||
.expect("failed to query the library");
|
||||
// let mut beets = BeetsLibrary::new(Box::new(
|
||||
// BeetsLibraryCommandExecutor::default().config(opt.beets_config_file_path.as_deref()),
|
||||
// ));
|
||||
|
||||
let mut database = JsonDatabase::new(Box::new(JsonDatabaseFileBackend::new(
|
||||
&opt.database_file_path,
|
||||
)));
|
||||
// let collection = beets
|
||||
// .list(&Query::new())
|
||||
// .expect("failed to query the library");
|
||||
|
||||
database
|
||||
.write(&collection)
|
||||
.expect("failed to write to the database");
|
||||
// Initialize the terminal user interface.
|
||||
let backend = CrosstermBackend::new(io::stdout());
|
||||
let terminal = Terminal::new(backend).expect("failed to initialise terminal");
|
||||
let events = EventHandler::new();
|
||||
let mut tui = Tui::new(terminal, events);
|
||||
tui.init();
|
||||
|
||||
// Start the main loop.
|
||||
while app.running {
|
||||
// Render the user interface.
|
||||
tui.draw(&mut app);
|
||||
// Handle events.
|
||||
match tui.events.next_event() {
|
||||
Event::Key(key_event) => handle_key_events(key_event, &mut app),
|
||||
Event::Mouse(_) => {}
|
||||
Event::Resize(_, _) => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Exit the user interface.
|
||||
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");
|
||||
}
|
||||
|
44
src/tui/app.rs
Normal file
44
src/tui/app.rs
Normal file
@ -0,0 +1,44 @@
|
||||
/// Application.
|
||||
#[derive(Debug)]
|
||||
pub struct App {
|
||||
/// Is the application running?
|
||||
pub running: bool,
|
||||
/// counter
|
||||
pub counter: u8,
|
||||
}
|
||||
|
||||
impl Default for App {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
running: true,
|
||||
counter: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl App {
|
||||
/// Constructs a new instance of [`App`].
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Handles the tick event of the terminal.
|
||||
pub fn tick(&self) {}
|
||||
|
||||
/// Set running to false to quit the application.
|
||||
pub fn quit(&mut self) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
60
src/tui/event.rs
Normal file
60
src/tui/event.rs
Normal file
@ -0,0 +1,60 @@
|
||||
use crossterm::event::{self, Event as CrosstermEvent, KeyEvent, MouseEvent};
|
||||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
|
||||
/// Terminal events.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum Event {
|
||||
/// Key press.
|
||||
Key(KeyEvent),
|
||||
/// Mouse click/scroll.
|
||||
Mouse(MouseEvent),
|
||||
/// Terminal resize.
|
||||
Resize(u16, u16),
|
||||
}
|
||||
|
||||
/// Terminal event handler.
|
||||
#[derive(Debug)]
|
||||
pub struct EventHandler {
|
||||
/// Event sender channel.
|
||||
sender: mpsc::Sender<Event>,
|
||||
/// Event receiver channel.
|
||||
receiver: mpsc::Receiver<Event>,
|
||||
/// Event handler thread.
|
||||
handler: thread::JoinHandle<()>,
|
||||
}
|
||||
|
||||
impl EventHandler {
|
||||
/// Constructs a new instance of [`EventHandler`].
|
||||
pub fn new() -> Self {
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
let handler = {
|
||||
let sender = sender.clone();
|
||||
thread::spawn(move || loop {
|
||||
// Put this inside an if event::poll {...} if the display needs to be refreshed on a
|
||||
// periodic basis. See
|
||||
// https://github.com/tui-rs-revival/rust-tui-template/blob/master/src/event.rs.
|
||||
match event::read().expect("unable to read event") {
|
||||
CrosstermEvent::Key(e) => sender.send(Event::Key(e)),
|
||||
CrosstermEvent::Mouse(e) => sender.send(Event::Mouse(e)),
|
||||
CrosstermEvent::Resize(w, h) => sender.send(Event::Resize(w, h)),
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
.expect("failed to send terminal event")
|
||||
})
|
||||
};
|
||||
Self {
|
||||
sender,
|
||||
receiver,
|
||||
handler,
|
||||
}
|
||||
}
|
||||
|
||||
/// Receive the next event from the handler thread.
|
||||
///
|
||||
/// This function will always block the current thread if
|
||||
/// there is no data available and it's possible for more data to be sent.
|
||||
pub fn next_event(&self) -> Event {
|
||||
self.receiver.recv().expect("failed to receive terminal event")
|
||||
}
|
||||
}
|
28
src/tui/handler.rs
Normal file
28
src/tui/handler.rs
Normal file
@ -0,0 +1,28 @@
|
||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||
|
||||
use super::app::App;
|
||||
|
||||
/// Handles the key events and updates the state of [`App`].
|
||||
pub fn handle_key_events(key_event: KeyEvent, app: &mut App) {
|
||||
match key_event.code {
|
||||
// Exit application on `ESC` or `q`
|
||||
KeyCode::Esc | KeyCode::Char('q') => {
|
||||
app.quit();
|
||||
}
|
||||
// Exit application on `Ctrl-C`
|
||||
KeyCode::Char('c') | KeyCode::Char('C') => {
|
||||
if key_event.modifiers == KeyModifiers::CONTROL {
|
||||
app.quit();
|
||||
}
|
||||
}
|
||||
// Counter handlers
|
||||
KeyCode::Right => {
|
||||
app.increment_counter();
|
||||
}
|
||||
KeyCode::Left => {
|
||||
app.decrement_counter();
|
||||
}
|
||||
// Other handlers you could add here.
|
||||
_ => {}
|
||||
}
|
||||
}
|
61
src/tui/mod.rs
Normal file
61
src/tui/mod.rs
Normal file
@ -0,0 +1,61 @@
|
||||
use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
|
||||
use crossterm::terminal::{self, EnterAlternateScreen, LeaveAlternateScreen};
|
||||
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;
|
||||
|
||||
/// 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();
|
||||
}
|
||||
}
|
36
src/tui/ui.rs
Normal file
36
src/tui/ui.rs
Normal file
@ -0,0 +1,36 @@
|
||||
use ratatui::{
|
||||
backend::Backend,
|
||||
layout::Alignment,
|
||||
style::{Color, Style},
|
||||
widgets::{Block, BorderType, Borders, Paragraph},
|
||||
Frame,
|
||||
};
|
||||
|
||||
use super::app::App;
|
||||
|
||||
/// Renders the user interface widgets.
|
||||
pub fn render<B: Backend>(app: &mut App, frame: &mut Frame<'_, B>) {
|
||||
// This is where you add new widgets.
|
||||
// See the following resources:
|
||||
// - https://docs.rs/ratatui/latest/ratatui/widgets/index.html
|
||||
// - https://github.com/tui-rs-revival/ratatui/tree/master/examples
|
||||
frame.render_widget(
|
||||
Paragraph::new(format!(
|
||||
"This is a tui template.\n\
|
||||
Press `Esc`, `Ctrl-C` or `q` to stop running.\n\
|
||||
Press left and right to increment and decrement the counter respectively.\n\
|
||||
Counter: {}",
|
||||
app.counter
|
||||
))
|
||||
.block(
|
||||
Block::default()
|
||||
.title("Template")
|
||||
.title_alignment(Alignment::Center)
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded),
|
||||
)
|
||||
.style(Style::default().fg(Color::Cyan).bg(Color::Black))
|
||||
.alignment(Alignment::Center),
|
||||
frame.size(),
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue
Block a user