Thomas Dy 9 жил өмнө
parent
commit
2844b2b0c1
4 өөрчлөгдсөн 44 нэмэгдсэн , 1 устгасан
  1. 1 0
      Cargo.toml
  2. 5 0
      src/bar.rs
  3. 3 1
      src/main.rs
  4. 35 0
      src/music.rs

+ 1 - 0
Cargo.toml

@@ -6,3 +6,4 @@ authors = ["Thomas Dy <thatsmydoing@gmail.com>"]
 [dependencies]
 time = "0.1"
 toml = "0.1"
+mpd = { git = "https://github.com/kstep/rust-mpd.git", rev = "d4738503" }

+ 5 - 0
src/bar.rs

@@ -62,6 +62,11 @@ impl Bar {
                         .output()
                         .ok(),
 
+                    'm' => Command::new("mpc")
+                        .arg("toggle")
+                        .output()
+                        .ok(),
+
                     _ => None
                 };
             }

+ 3 - 1
src/main.rs

@@ -4,6 +4,7 @@ mod config;
 mod external;
 mod bar;
 mod store;
+mod music;
 
 use comm::{Channel, Message};
 use config::Config;
@@ -23,6 +24,7 @@ fn main() {
     let (tx, rx) = mpsc::channel::<Message>();
     make_thread(&tx, &cfg, external::external);
     make_thread(&tx, &cfg, sensors::sensors);
+    make_thread(&tx, &cfg, music::music);
 
     let mut data = store::Store::new();
 
@@ -35,7 +37,7 @@ fn main() {
             data.get("title"),
             data.get("sensors")
         ));
-        botbar.send(&format!("{}", data.get("cmus")));
+        botbar.send(&format!("{}", data.get("mpd")));
     }
 }
 

+ 35 - 0
src/music.rs

@@ -0,0 +1,35 @@
+extern crate mpd;
+
+use comm;
+use comm::Channel;
+use config::Config;
+use self::mpd::client::Client;
+use self::mpd::song::Song;
+use self::mpd::idle;
+use self::mpd::idle::Idle;
+use self::mpd::status::State;
+
+pub fn music(tx: &Channel, _cfg: &Config) {
+    let mut conn = Client::connect("127.0.0.1:6600").unwrap();
+    loop {
+        let state = match conn.status().unwrap().state {
+            State::Play => "",
+            State::Pause => "",
+            State::Stop => ""
+        };
+
+        let song = conn.currentsong().unwrap();
+        let stopped = "Stopped".to_string();
+        let display = song.as_ref().map(get_display).unwrap_or(stopped);
+        comm::send(tx, "mpd", &format!("%{{A:mpd:}}{} {}%{{A}}", state, display));
+        conn.wait(&[idle::Subsystem::Player]).ok();
+    }
+}
+
+fn get_display(song: &Song) -> String {
+    let unknown = "Unknown".to_string();
+    let artist = song.tags.get("Artist").unwrap_or(&unknown);
+    let title = song.tags.get("Title").unwrap_or(&unknown);
+
+    format!("{} - {}", artist, title)
+}