From bbc46eb13792b88ac51f9a2fabed163f188feacf Mon Sep 17 00:00:00 2001 From: Jacob Casper Date: Fri, 6 Nov 2020 15:32:43 -0600 Subject: [PATCH] Put date and time in xsetroot name --- Cargo.lock | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 35 +++++++++++++++++++++++- 3 files changed, 113 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index fd6c2ab..270245e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,24 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "time", + "winapi", +] + [[package]] name = "dbus" version = "0.9.0" @@ -25,15 +44,74 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + [[package]] name = "pkg-config" version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi", + "winapi", +] + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "xsetrootd" version = "0.1.0" dependencies = [ + "chrono", "dbus", ] diff --git a/Cargo.toml b/Cargo.toml index 9f14b5c..fff6099 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ edition = "2018" [dependencies] dbus = "0.9.0" +chrono = "0.4" diff --git a/src/main.rs b/src/main.rs index a5b3c32..4aec86b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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>>, 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>>, 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> { }, ); + 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))?; } -- 2.20.1