~phate/mpc-status

b9b93f669f4cd8e1515f46f336b8865c23d5e7df — ValleyKnight 1 year, 7 months ago b35af41
fix program breaking when paused... oops
1 files changed, 21 insertions(+), 17 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +21 -17
@@ 19,26 19,30 @@ fn format_time(time: i64) -> String {
    format!("{:0>2}:{:0>2}", minutes, seconds)
}

fn output(c: &mut Client, status: Status, stat: State) {
    let song: Song = c.currentsong().unwrap().unwrap();
    let na = "N/A".to_string();
    let gen = song.tags.get("Genre").unwrap_or(&na);
    let art = song.tags.get("Artist").unwrap_or(&na);
    let alb = song.tags.get("Album").unwrap_or(&na);
    let tit = song.title.as_ref().unwrap();
    let dat = song.tags.get("Date").unwrap_or(&na);
    let elap = status.elapsed.unwrap().num_seconds();
    let elapsed = format_time(elap);
    let dur = status.duration.unwrap().num_seconds();
    let duration = format_time(dur);
    let state = PlayState { sta: stat }.to_string();
    let state = find_and_replace(&state, &["s/Play/Playing/g", "s/Pause/Paused/g"]).unwrap();
    println!("{} - {} - {} ({}) - {} -- {} -- [{}/{}]", gen, art, alb, dat, tit, state, elapsed, duration);
}

fn main() {
    let mut c = Client::connect("127.0.0.1:6600").unwrap();
    let status: Status = c.status().unwrap();
    let stat = status.state;
    if stat == State::Play {
        let song: Song = c.currentsong().unwrap().unwrap();
        let na = "N/A".to_string();
        let gen = song.tags.get("Genre").unwrap_or(&na);
        let art = song.tags.get("Artist").unwrap_or(&na);
        let alb = song.tags.get("Album").unwrap_or(&na);
        let tit = song.title.as_ref().unwrap();
        let dat = song.tags.get("Date").unwrap_or(&na);
        let elap = status.elapsed.unwrap().num_seconds();
        let elapsed = format_time(elap);
        let dur = status.duration.unwrap().num_seconds();
        let duration = format_time(dur);
        let state = PlayState { sta: stat }.to_string();
        let state = find_and_replace(&state, &["s/Play/Playing/g", "s/Pause/Paused/g"]).unwrap();
        println!("{} - {} - {} ({}) - {} -- {} -- [{}/{}]", gen, art, alb, dat, tit, state, elapsed, duration);
    } else if stat == State::Stop {
        println!("[mpd is stopped]");
    match stat {
        State::Play => output(&mut c, status, stat),
        State::Pause => output(&mut c, status, stat),
        State::Stop => println!("[mpd is stopped]")
    }
}