From 4452cdbaa0d84084d4e8f39c2c9c5e0dc249fce5 Mon Sep 17 00:00:00 2001 From: Mahesh Asolkar Date: Sat, 7 Jun 2025 20:42:23 -0700 Subject: [PATCH] Added date and time modules --- Cargo.toml | 1 + src/bar_modules/bar_module_date.rs | 47 ++++++++++++++++++++++++++++++ src/bar_modules/bar_module_time.rs | 47 ++++++++++++++++++++++++++++++ src/bar_modules/mod.rs | 2 ++ src/main.rs | 5 +++- 5 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/bar_modules/bar_module_date.rs create mode 100644 src/bar_modules/bar_module_time.rs diff --git a/Cargo.toml b/Cargo.toml index 535af57..5b96f34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ serde = "1.0.196" serde_json = "1.0.113" curl = "0.4.46" regex = "1.11.1" +chrono = "0.4.41" clap = { version = "4.5.1", features = ["derive"] } diff --git a/src/bar_modules/bar_module_date.rs b/src/bar_modules/bar_module_date.rs new file mode 100644 index 0000000..ebe8c20 --- /dev/null +++ b/src/bar_modules/bar_module_date.rs @@ -0,0 +1,47 @@ +use chrono::{DateTime, Local}; +use crate::common; +use crate::bar_modules; + +#[derive(Clone)] +pub struct UnibarModuleDate { + opts: common::AppOptions, + date_time: DateTime, +} + +impl UnibarModuleDate { + // -------------------- + pub fn new(o :common::AppOptions) -> Self { + UnibarModuleDate { + opts: o, + date_time: Local::now(), + } + } +} + +impl bar_modules::BarModuleActions for UnibarModuleDate { + + // -------------------- + fn generate_data(&mut self) { + self.date_time = Local::now(); + if self.opts.debug { + println!("-----> Date dump {:#?}", self.date_time); + } + } + + // -------------------- + fn get_content(&self) -> String { + return format!("{}", self.date_time.format("%Y %h %d")); + } + + // -------------------- + fn get_icon(&self) -> String { + return "📅".to_string(); + } +} + +impl bar_modules::BarModuleDebug for UnibarModuleDate { + + // -------------------- + fn post_debug(&self) { + } +} diff --git a/src/bar_modules/bar_module_time.rs b/src/bar_modules/bar_module_time.rs new file mode 100644 index 0000000..38f680e --- /dev/null +++ b/src/bar_modules/bar_module_time.rs @@ -0,0 +1,47 @@ +use chrono::{DateTime, Local}; +use crate::common; +use crate::bar_modules; + +#[derive(Clone)] +pub struct UnibarModuleTime { + opts: common::AppOptions, + date_time: DateTime, +} + +impl UnibarModuleTime { + // -------------------- + pub fn new(o :common::AppOptions) -> Self { + UnibarModuleTime { + opts: o, + date_time: Local::now(), + } + } +} + +impl bar_modules::BarModuleActions for UnibarModuleTime { + + // -------------------- + fn generate_data(&mut self) { + self.date_time = Local::now(); + if self.opts.debug { + println!("-----> Time dump {:#?}", self.date_time); + } + } + + // -------------------- + fn get_content(&self) -> String { + return format!("{}", self.date_time.format("%I:%M%p %Z")); + } + + // -------------------- + fn get_icon(&self) -> String { + return "🕑".to_string(); + } +} + +impl bar_modules::BarModuleDebug for UnibarModuleTime { + + // -------------------- + fn post_debug(&self) { + } +} diff --git a/src/bar_modules/mod.rs b/src/bar_modules/mod.rs index 0917787..5da2397 100644 --- a/src/bar_modules/mod.rs +++ b/src/bar_modules/mod.rs @@ -23,3 +23,5 @@ pub mod bar_module_network; pub mod bar_module_memory; pub mod bar_module_cpu; pub mod bar_module_power; +pub mod bar_module_date; +pub mod bar_module_time; diff --git a/src/main.rs b/src/main.rs index e9c7630..01dc06e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -66,7 +66,8 @@ impl Unibar { self.bar_modules_enabled.push(Box::new(bar_modules::bar_module_cpu::UnibarModuleCpu::new(self.opts.clone()))); self.bar_modules_enabled.push(Box::new(bar_modules::bar_module_power::UnibarModulePower::new(self.opts.clone()))); self.bar_modules_enabled.push(Box::new(bar_modules::bar_module_network::UnibarModuleNetwork::new(self.opts.clone()))); - self.bar_modules_enabled.push(Box::new(bar_modules::bar_module_weather::UnibarModuleWeather::new(self.opts.clone()))); + self.bar_modules_enabled.push(Box::new(bar_modules::bar_module_date::UnibarModuleDate::new(self.opts.clone()))); + self.bar_modules_enabled.push(Box::new(bar_modules::bar_module_time::UnibarModuleTime::new(self.opts.clone()))); // Run in a forever-loop ... loop { @@ -117,6 +118,8 @@ impl Unibar { Box::new(bar_modules::bar_module_network::UnibarModuleNetwork::new(self.opts.clone())), Box::new(bar_modules::bar_module_cpu::UnibarModuleCpu::new(self.opts.clone())), Box::new(bar_modules::bar_module_power::UnibarModulePower::new(self.opts.clone())), + Box::new(bar_modules::bar_module_date::UnibarModuleDate::new(self.opts.clone())), + Box::new(bar_modules::bar_module_time::UnibarModuleTime::new(self.opts.clone())), ]; for md in bar_modules_debugged { md.post_debug();