aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 9984c066b3ffd3e5d4eed3393f592beb940b9f4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#![allow(unused_imports)]
use clap::Parser;
use rmptui::app;
use rmptui::app::App;
use rmptui::app::AppResult;
use rmptui::cli::Args;
use rmptui::cli::Command;
use rmptui::connection::Connection;
use rmptui::event::Event;
use rmptui::event::EventHandler;
use rmptui::handler;
use rmptui::song::RSong;
use rmptui::tui;
use std::env;
use std::io;

use crossterm::event::{self, KeyCode, KeyEvent, KeyEventKind};
use ratatui::{
    prelude::*,
    symbols::border,
    widgets::{block::*, *},
};

pub type Result<T> = core::result::Result<T, Error>;
pub type Error = Box<dyn std::error::Error>;

fn main() -> AppResult<()> {
    let args = Args::parse();
    let env_host = env::var("MPD_HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
    let env_port  = env::var("MPD_PORT").unwrap_or_else(|_| "6600".to_string());
    let mut app = App::builder(format!("{}:{}", env_host, env_port).as_str())?;

    if !args.tui {
        handle_tui(&mut app)?;
    } else {
        match args.command {
            Some(Command::Dmenu) => app.conn.play_dmenu()?,
            Some(Command::Fzf) => app.conn.play_fzf().unwrap(),
            Some(Command::Status) => app.conn.status(),
            Some(Command::Pause) => app.conn.pause(),
            Some(Command::Toggle) => app.conn.toggle_pause(),
            _ => {
                // let mut vec: Vec<RSong> = Vec::new();
                // for filename in app.conn.songs_filenames {
                //     let song = RSong::new(&mut app.conn.conn, filename);
                //     vec.push(song);
                // }
                // println!("{:#?}", vec);

            }
        }
    }
    Ok(())
}

pub fn handle_tui(app: &mut App) -> AppResult<()> {
    let backend = CrosstermBackend::new(io::stderr());
    let terminal = Terminal::new(backend)?;
    let events = EventHandler::new(1000);

    let mut tui = tui::Tui::new(terminal, events);
    tui.init()?;

    // update the directory
    app.browser.update_directory(&mut app.conn).unwrap();

    while app.running {
        tui.draw(app)?;
        match tui.events.next()? {
            Event::Tick => app.tick(),
            Event::Key(key_event) => handler::handle_key_events(key_event, app)?,
            Event::Mouse(_) => {}
            Event::Resize(_, _) => {}
        }
    }

    Ok(())
}