~phate/mpc-status

mpc-status/src/main.rs -rw-r--r-- 1.5 KiB
7959c553ValleyKnight reduce code by a single line 2 years ago
                                                                                
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
use mpd::{Client, Song, State, Status};
use sedregex::find_and_replace;
use std::fmt;

struct PlayState {
    sta: State,
}

impl fmt::Display for PlayState {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self.sta)
    }
}

fn format_time(time: i64) -> String {
    let minutes = (time / 60) % 60;
    let seconds = time % 60;

    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;
    match stat {
        State::Play | State::Pause => output(&mut c, status, stat),
        State::Stop => println!("[mpd is stopped]")
    }
}