Prevent small race when both track and artist change
[xsetrootd.git] / src / main.rs
index ca15fa2..d2d1df2 100644 (file)
@@ -51,11 +51,16 @@ fn update_map(arc_map: Arc<Mutex<HashMap<String, String>>>, key: &str, val: &str
     let clone_arc = arc_map.clone();
     let mut map = clone_arc.lock().unwrap();
     map.insert(String::from(key), String::from(val));
+}
+
+fn update_xsetroot(arc_map: Arc<Mutex<HashMap<String, String>>>) {
+    let clone_arc = arc_map.clone();
+    let map = clone_arc.lock().unwrap();
     let _ = Command::new("xsetroot")
         .arg("-name")
         .arg(format!(
             "[{playback_status} {title} - {artist}] | ✉ {unread_count} | {date_time}",
-            playback_status = map.get("playback-status").unwrap_or(&String::from("🔈")),
+            playback_status = map.get("playback_status").unwrap_or(&String::from("🔈")),
             title = map.get("title").unwrap_or(&String::from("")),
             artist = map.get("artist").unwrap_or(&String::from("")),
             unread_count = map.get("unread_count").unwrap_or(&String::from("?")),
@@ -82,6 +87,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
                 "unread_count",
                 m.count.to_string().as_str(),
             );
+            update_xsetroot(mail_match_map.clone());
             true
         },
     );
@@ -95,41 +101,51 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
     let spotify_match_map = arc_locked_xset_map.clone();
     let _ = spotify_proxy.match_signal(
         move |pc: PropertiesPropertiesChanged, _: &Connection, _: &Message| {
-            pc.changed_properties["Metadata"]
-                .0
-                .as_iter()
-                .and_then(|mut iter| {
-                    // Iterating over a RefArg goes key, value, key, value... Insane honestly.
-                    while let Some(key) = iter.next() {
-                        let key_str = key.as_str()?;
-                        let value = iter.next();
+            match pc.changed_properties.get("Metadata") {
+                Some(variant) => {
+                    variant.0.as_iter().and_then(|mut iter| {
+                        // Iterating over a RefArg goes key, value, key, value... Insane honestly.
+                        while let Some(key) = iter.next() {
+                            let key_str = key.as_str()?;
+                            let value = iter.next();
 
-                        match key_str {
-                            "xesam:artist" => {
-                                // Variant holding a variant that should just be a Vec<&str> I
-                                // believe. This is _the recommended_ way to do this by the author.
-                                let inner_value = value?.as_iter()?.next()?.as_iter()?.next()?;
-                                let artist = inner_value.as_str()?;
-                                update_map(spotify_match_map.clone(), "artist", artist);
-                            }
-                            "xesam:title" => {
-                                let title = value?.as_iter()?.next()?.as_str()?;
-                                update_map(spotify_match_map.clone(), "title", title);
+                            match key_str {
+                                "xesam:artist" => {
+                                    // Variant holding a variant that should just be a Vec<&str> I
+                                    // believe. This is _the recommended_ way to do this by the author.
+                                    let inner_value =
+                                        value?.as_iter()?.next()?.as_iter()?.next()?;
+                                    let artist = inner_value.as_str()?;
+                                    update_map(spotify_match_map.clone(), "artist", artist);
+                                }
+                                "xesam:title" => {
+                                    let title = value?.as_iter()?.next()?.as_str()?;
+                                    update_map(spotify_match_map.clone(), "title", title);
+                                }
+                                _ => (),
                             }
-                            _ => (),
                         }
-                    }
-                    Some(())
-                });
-            let playback_status = &pc.changed_properties["PlaybackStatus"].0;
-            update_map(
-                spotify_match_map.clone(),
-                "playback_status",
-                match &*playback_status.as_str().unwrap_or("🔈") {
-                    "Playing" => "🔊",
-                    _ => "🔈",
-                },
-            );
+                        update_xsetroot(spotify_match_map.clone());
+                        Some(())
+                    });
+                }
+                None => (),
+            }
+            match &pc.changed_properties.get("PlaybackStatus") {
+                Some(variant) => {
+                    let playback_status = &variant.0;
+                    update_map(
+                        spotify_match_map.clone(),
+                        "playback_status",
+                        match &*playback_status.as_str().unwrap_or("") {
+                            "Playing" => "🔊",
+                            _ => "🔈",
+                        },
+                    );
+                    update_xsetroot(spotify_match_map.clone());
+                }
+                None => (),
+            }
             true
         },
     );
@@ -147,6 +163,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
                     "date_time",
                     get_local_time_string().as_str(),
                 );
+                update_xsetroot(date_time_map.clone());
             }
             thread::sleep(Duration::from_millis(5000));
         }