Krolyxon
Commit fd371e8

rmptuisrcmain.rs

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