Krolyxon
Commit 4194bda

rmptuisrcmain.rs

77 lines · 2.2 KB · Rust

1#![allow(unused_imports)]
2use clap::Parser;
3use rmptui::app;
4use rmptui::app::App;
5use rmptui::app::AppResult;
6use rmptui::cli::Args;
7use rmptui::cli::Command;
8use rmptui::connection::Connection;
9use rmptui::event::Event;
10use rmptui::event::EventHandler;
11use rmptui::handler;
12use rmptui::tui;
13use std::env;
14use std::io;
15
16use crossterm::event::{self, KeyCode, KeyEvent, KeyEventKind};
17use ratatui::{
18 prelude::*,
19 symbols::border,
20 widgets::{block::*, *},
21};
22
23pub type Result<T> = core::result::Result<T, Error>;
24pub type Error = Box<dyn std::error::Error>;
25
26fn main() -> AppResult<()> {
27 let args = Args::parse();
28 let env_host = env::var("MPD_HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
29 let env_port = env::var("MPD_PORT").unwrap_or_else(|_| "6600".to_string());
30 let mut app = App::builder(format!("{}:{}", env_host, env_port).as_str())?;
31
32 if !args.tui {
33 handle_tui(&mut app)?;
34 } else {
35 match args.command {
36 Some(Command::Dmenu) => app.conn.play_dmenu()?,
37 Some(Command::Fzf) => app.conn.play_fzf().unwrap(),
38 Some(Command::Status) => app.conn.status(),
39 Some(Command::Pause) => app.conn.pause(),
40 Some(Command::Toggle) => app.conn.toggle_pause(),
41 _ => {
42 // let mut vec: Vec<RSong> = Vec::new();
43 // for filename in app.conn.songs_filenames {
44 // let song = RSong::new(&mut app.conn.conn, filename);
45 // vec.push(song);
46 // }
47 // println!("{:#?}", vec);
48
49 }
50 }
51 }
52 Ok(())
53}
54
55pub fn handle_tui(app: &mut App) -> AppResult<()> {
56 let backend = CrosstermBackend::new(io::stderr());
57 let terminal = Terminal::new(backend)?;
58 let events = EventHandler::new(1000);
59
60 let mut tui = tui::Tui::new(terminal, events);
61 tui.init()?;
62
63 // update the directory
64 app.browser.update_directory(&mut app.conn).unwrap();
65
66 while app.running {
67 tui.draw(app)?;
68 match tui.events.next()? {
69 Event::Tick => app.tick(),
70 Event::Key(key_event) => handler::handle_key_events(key_event, app)?,
71 Event::Mouse(_) => {}
72 Event::Resize(_, _) => {}
73 }
74 }
75
76 Ok(())
77}