Put date and time in xsetroot name
[xsetrootd.git] / src / main.rs
index a5b3c32..4aec86b 100644 (file)
@@ -5,6 +5,7 @@ use dbus::message::Message;
 use std::collections::HashMap;
 use std::process::Command;
 use std::sync::{Arc, Mutex};
+use std::thread;
 use std::time::Duration;
 
 // Custom signal type impl
@@ -33,6 +34,19 @@ impl dbus::message::SignalArgs for ComJacobCasperMailUnreadCount {
     const INTERFACE: &'static str = "com.jacobcasper.Mail";
 }
 
+fn get_local_time_string() -> std::string::String {
+    use chrono::prelude::*;
+    let local_time = chrono::prelude::Local::now();
+    return format!(
+        "{}-{}-{:02} {}:{}",
+        local_time.year(),
+        local_time.month(),
+        local_time.day(),
+        local_time.hour(),
+        local_time.minute(),
+    );
+}
+
 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();
@@ -40,10 +54,11 @@ fn update_map(arc_map: Arc<Mutex<HashMap<String, String>>>, key: &str, val: &str
     let _ = Command::new("xsetroot")
         .arg("-name")
         .arg(format!(
-            "[🔊 {title} - {artist}] | âœ‰ {unread_count}",
+            "[🔊 {title} - {artist}] | âœ‰ {unread_count} | {date_time}",
             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("?")),
+            date_time = map.get("date_time").unwrap_or(&get_local_time_string()),
         ))
         .spawn();
 }
@@ -109,6 +124,24 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
         },
     );
 
+    let date_time_map = arc_locked_xset_map.clone();
+    let _ = thread::spawn(move || -> Result<(), std::time::SystemTimeError> {
+        let mut last_run_at = std::time::SystemTime::now();
+        loop {
+            let sys_time = std::time::SystemTime::now();
+            let elapsed_time = sys_time.duration_since(last_run_at)?;
+            if elapsed_time.as_secs() > 60 {
+                last_run_at = sys_time;
+                update_map(
+                    date_time_map.clone(),
+                    "date_time",
+                    get_local_time_string().as_str(),
+                );
+            }
+            thread::sleep(Duration::from_millis(5000));
+        }
+    });
+
     loop {
         conn.process(Duration::from_millis(1000))?;
     }