music.rs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. extern crate mpd;
  2. use comm;
  3. use comm::Channel;
  4. use config::Config;
  5. use self::mpd::client::Client;
  6. use self::mpd::song::Song;
  7. use self::mpd::idle;
  8. use self::mpd::idle::Idle;
  9. use self::mpd::status::State;
  10. pub fn music(tx: &Channel, _cfg: &Config) {
  11. let mut conn = Client::connect("127.0.0.1:6600").unwrap();
  12. loop {
  13. let state = match conn.status().unwrap().state {
  14. State::Play => "",
  15. State::Pause => "",
  16. State::Stop => ""
  17. };
  18. let song = conn.currentsong().unwrap();
  19. let stopped = "Stopped".to_string();
  20. let display = song.as_ref().map(get_display).unwrap_or(stopped);
  21. comm::send(tx, "mpd", &format!("{{\"name\":\"mpd\",\"separator\":false,\"markup\":\"pango\",\"align\":\"left\",\"full_text\":\"<span face=\\\"Ionicons\\\">{}</span> {}\"}}", state, display));
  22. conn.wait(&[idle::Subsystem::Player]).ok();
  23. }
  24. }
  25. fn get_display(song: &Song) -> String {
  26. let unknown = "Unknown".to_string();
  27. let artist = song.tags.get("Artist").unwrap_or(&unknown);
  28. let title = song.title.as_ref().unwrap_or(&unknown);
  29. format!("{} - {}", artist, title)
  30. }