From 7245914da85295648a5e1cea62574a19748479b9 Mon Sep 17 00:00:00 2001 From: Mahesh Asolkar Date: Sat, 14 Jun 2025 17:33:24 -0700 Subject: [PATCH] Added mechanism to skip updates in a module * This is useful in modules where HTTP API calls are made. Updating them every second is expensive, network intensive and can cause rate limits to be hit * Added get_name() function to modules for easier debug statements --- src/bar_modules/bar_module_cpu.rs | 5 +++++ src/bar_modules/bar_module_date.rs | 5 +++++ src/bar_modules/bar_module_memory.rs | 5 +++++ src/bar_modules/bar_module_music.rs | 5 +++++ src/bar_modules/bar_module_network.rs | 5 +++++ src/bar_modules/bar_module_power.rs | 5 +++++ src/bar_modules/bar_module_time.rs | 5 +++++ src/bar_modules/bar_module_weather.rs | 20 ++++++++++++++++++++ src/bar_modules/mod.rs | 11 +++++++++++ src/common/mod.rs | 7 +++++++ src/main.rs | 22 +++++++++++++++++++--- 11 files changed, 92 insertions(+), 3 deletions(-) diff --git a/src/bar_modules/bar_module_cpu.rs b/src/bar_modules/bar_module_cpu.rs index 42d98fa..c165450 100644 --- a/src/bar_modules/bar_module_cpu.rs +++ b/src/bar_modules/bar_module_cpu.rs @@ -116,6 +116,11 @@ impl UnibarModuleCpu { impl bar_modules::BarModuleActions for UnibarModuleCpu { + // -------------------- + fn get_name(&self) -> String { + return "UnibarModuleCpu".to_string(); + } + // -------------------- fn clear(&mut self) { self.cpu_times_sample_1 = CpuTimes::new(); diff --git a/src/bar_modules/bar_module_date.rs b/src/bar_modules/bar_module_date.rs index bfd4f88..a30dc78 100644 --- a/src/bar_modules/bar_module_date.rs +++ b/src/bar_modules/bar_module_date.rs @@ -20,6 +20,11 @@ impl UnibarModuleDate { impl bar_modules::BarModuleActions for UnibarModuleDate { + // -------------------- + fn get_name(&self) -> String { + return "UnibarModuleDate".to_string(); + } + // -------------------- fn clear(&mut self) { self.date_time = Local::now(); diff --git a/src/bar_modules/bar_module_memory.rs b/src/bar_modules/bar_module_memory.rs index 6b998ba..6c22ad1 100644 --- a/src/bar_modules/bar_module_memory.rs +++ b/src/bar_modules/bar_module_memory.rs @@ -25,6 +25,11 @@ impl UnibarModuleMemory { impl bar_modules::BarModuleActions for UnibarModuleMemory { + // -------------------- + fn get_name(&self) -> String { + return "UnibarModuleMemory".to_string(); + } + // -------------------- fn clear(&mut self) { self.meminfo_str = "".to_string(); diff --git a/src/bar_modules/bar_module_music.rs b/src/bar_modules/bar_module_music.rs index 3a1c406..e1520d8 100644 --- a/src/bar_modules/bar_module_music.rs +++ b/src/bar_modules/bar_module_music.rs @@ -25,6 +25,11 @@ impl UnibarModuleMusic { impl bar_modules::BarModuleActions for UnibarModuleMusic { + // -------------------- + fn get_name(&self) -> String { + return "UnibarModuleMusic".to_string(); + } + // -------------------- fn clear(&mut self) { self.current_stdout = "".to_string(); diff --git a/src/bar_modules/bar_module_network.rs b/src/bar_modules/bar_module_network.rs index 36242ad..25161e7 100644 --- a/src/bar_modules/bar_module_network.rs +++ b/src/bar_modules/bar_module_network.rs @@ -26,6 +26,11 @@ impl UnibarModuleNetwork { impl bar_modules::BarModuleActions for UnibarModuleNetwork { + // -------------------- + fn get_name(&self) -> String { + return "UnibarModuleNetwork".to_string(); + } + // -------------------- fn clear(&mut self) { self.network_info = json!(serde_json::Value::Null); diff --git a/src/bar_modules/bar_module_power.rs b/src/bar_modules/bar_module_power.rs index d9b2be2..dde138d 100644 --- a/src/bar_modules/bar_module_power.rs +++ b/src/bar_modules/bar_module_power.rs @@ -38,6 +38,11 @@ impl UnibarModulePower { impl bar_modules::BarModuleActions for UnibarModulePower { + // -------------------- + fn get_name(&self) -> String { + return "UnibarModulePower".to_string(); + } + // -------------------- fn clear(&mut self) { self.power_info = PowerData::new(); diff --git a/src/bar_modules/bar_module_time.rs b/src/bar_modules/bar_module_time.rs index 2e5a47a..4664f68 100644 --- a/src/bar_modules/bar_module_time.rs +++ b/src/bar_modules/bar_module_time.rs @@ -23,6 +23,11 @@ impl UnibarModuleTime { impl bar_modules::BarModuleActions for UnibarModuleTime { + // -------------------- + fn get_name(&self) -> String { + return "UnibarModuleTime".to_string(); + } + // -------------------- fn clear(&mut self) { self.date_time = Local::now(); diff --git a/src/bar_modules/bar_module_weather.rs b/src/bar_modules/bar_module_weather.rs index bf31ba1..1572bb0 100644 --- a/src/bar_modules/bar_module_weather.rs +++ b/src/bar_modules/bar_module_weather.rs @@ -5,12 +5,14 @@ use serde_json::Value; use regex::Regex; use crate::common; +use crate::common::UpdateResult; use crate::bar_modules; #[derive(Clone)] pub struct UnibarModuleWeather { opts: common::AppOptions, weather_info: Value, + update_cnt: u32, } impl UnibarModuleWeather { @@ -20,6 +22,7 @@ impl UnibarModuleWeather { UnibarModuleWeather { opts: o, weather_info: json!(serde_json::Value::Null), + update_cnt: 0, } } @@ -159,6 +162,23 @@ impl UnibarModuleWeather { impl bar_modules::BarModuleActions for UnibarModuleWeather { + // -------------------- + fn get_name(&self) -> String { + return "UnibarModuleWeather".to_string(); + } + + // -------------------- + // Weather update every 60 cycles + fn should_update(&mut self) -> UpdateResult { + if self.update_cnt == 0 { + self.update_cnt = 300; + return UpdateResult::Update; + } else { + self.update_cnt -= 1; + return UpdateResult::Skip; + } + } + // -------------------- fn clear(&mut self) { self.weather_info = json!(serde_json::Value::Null); diff --git a/src/bar_modules/mod.rs b/src/bar_modules/mod.rs index a128b80..f9ab7c6 100644 --- a/src/bar_modules/mod.rs +++ b/src/bar_modules/mod.rs @@ -1,6 +1,11 @@ +use crate::common::UpdateResult; + // -------------------- /// All Bar modules must implement the actions pub trait BarModuleActions { + /// Return String with name of the module + fn get_name(&self) -> String; + /// Do necessary clean up before starting new fetch fn clear(&mut self); @@ -13,6 +18,12 @@ pub trait BarModuleActions { /// Return a Unicode icon to display before content in the bar. /// This icon may differ based on content of the data fn get_icon(&self) -> String; + + /// Returns UpdateResult depending on whether the module + /// should be updated during the current update cycle + fn should_update(&mut self) -> UpdateResult { + return UpdateResult::Update; + } } pub trait BarModuleDebug { diff --git a/src/common/mod.rs b/src/common/mod.rs index bda0c82..36e2353 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -18,6 +18,12 @@ impl fmt::Display for TemperatureUnits { } } +#[derive(Debug,PartialEq,Eq,Copy,Clone)] +pub enum UpdateResult { + Update, + Skip +} + // -------------------- // Application options // -------------------- @@ -29,4 +35,5 @@ pub struct AppOptions { pub music_progress: bool, pub debug: bool, pub debug_modules: bool, + pub debug_update: bool, } diff --git a/src/main.rs b/src/main.rs index 8e167d1..68ef735 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use std::str; use std::thread; use std::time::Duration; use clap::Parser; +use crate::common::UpdateResult; // Common utilities/types mod common; @@ -17,7 +18,7 @@ mod bar_modules; suitable to use in a status bar")] struct CommandlineArgs { /// Update interval in seconds - #[arg(short = 'i', long, default_value = "5")] + #[arg(short = 'i', long, default_value = "1")] interval: u64, /// Name of the weather station @@ -40,6 +41,11 @@ struct CommandlineArgs { /// but before output is printed #[arg(short = 'M', long)] debug_modules: bool, + + /// Show module debug information after all modules are evaluated + /// but before output is printed + #[arg(short = 'U', long)] + debug_update: bool, } // @@ -95,8 +101,17 @@ impl Unibar { // // Following generates ICON+CONTENT string for a module to be displayed // in the bar - md.clear(); - md.generate_data(); + match md.should_update() { + UpdateResult::Update => { + md.clear(); + md.generate_data(); + } + UpdateResult::Skip => { + if self.opts.debug_update { + println!("Skipping module {}", md.get_name()); + } + } + } mod_parts.push(md.get_icon()); mod_parts.push(md.get_content()); @@ -162,6 +177,7 @@ fn main() { music_progress: cmd_args.music_progress, debug: cmd_args.debug, debug_modules: cmd_args.debug_modules, + debug_update: cmd_args.debug_update, }, bar_modules_enabled: Vec::new(), debug_modules_done: false,