diff --git a/src/common/mod.rs b/src/common/mod.rs index 2177f5f..bda0c82 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -12,8 +12,8 @@ pub enum TemperatureUnits { impl fmt::Display for TemperatureUnits { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - TemperatureUnits::Metric => write!(f, "Metric"), - TemperatureUnits::Imperial => write!(f, "Imperial"), + TemperatureUnits::Metric => write!(f, "Metric"), + TemperatureUnits::Imperial => write!(f, "Imperial"), } } } @@ -23,9 +23,10 @@ impl fmt::Display for TemperatureUnits { // -------------------- #[derive(Debug,Clone)] pub struct AppOptions { - pub weather_units: TemperatureUnits, - pub weather_station: String, - pub music_progress: bool, - pub debug: bool, - pub debug_modules: bool, + pub interval: u64, + pub weather_units: TemperatureUnits, + pub weather_station: String, + pub music_progress: bool, + pub debug: bool, + pub debug_modules: bool, } diff --git a/src/main.rs b/src/main.rs index bb8dc67..e9c7630 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ use std::str; +use std::thread; +use std::time::Duration; use clap::Parser; // Common utilities/types @@ -14,6 +16,10 @@ mod bar_modules; #[command(about, long_about = "A tool that returns variety of components in a string suitable to use in a status bar")] struct CommandlineArgs { + /// Update interval in seconds + #[arg(short = 'i', long, default_value = "5")] + interval: u64, + /// Name of the weather station #[arg(short = 's', long, default_value = "khio")] weather_station: String, @@ -39,32 +45,44 @@ struct CommandlineArgs { // // Application (Unibar) // -#[derive(Clone)] +//#[derive(Clone)] struct Unibar { - opts: common::AppOptions, + opts: common::AppOptions, + bar_modules_enabled: Vec>, + debug_modules_done: bool, } impl Unibar { // -------------------- - fn run(&self) { + fn run(&mut self) { if self.opts.debug { self.debug_msg("Debugging ..."); } self.check_options(); // Set up a list of all modules to be used - let bar_modules_enabled: Vec> = vec! [ - Box::new(bar_modules::bar_module_music::UnibarModuleMusic::new(self.opts.clone())), - Box::new(bar_modules::bar_module_memory::UnibarModuleMemory::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_network::UnibarModuleNetwork::new(self.opts.clone())), - Box::new(bar_modules::bar_module_weather::UnibarModuleWeather::new(self.opts.clone())), - ]; + self.bar_modules_enabled.push(Box::new(bar_modules::bar_module_music::UnibarModuleMusic::new(self.opts.clone()))); + self.bar_modules_enabled.push(Box::new(bar_modules::bar_module_memory::UnibarModuleMemory::new(self.opts.clone()))); + 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()))); + // Run in a forever-loop ... + loop { + // ... to update the bar contents ... + self.update_bar_contents(); + // ... at specfied interval + thread::sleep(Duration::from_millis(self.opts.interval * 1000)); + } + + } + + // -------------------- + fn update_bar_contents(&mut self) { // Get module's part to be displayed in the bar let mut parts = Vec::new(); - for mut md in bar_modules_enabled { + for md in &mut self.bar_modules_enabled { let mut mod_parts = Vec::new(); // Each bar module implements following 3 steps: @@ -81,23 +99,31 @@ impl Unibar { parts.push(mod_parts.join(" ")); } - // Show module debug information if enabled - if self.opts.debug_modules { - let bar_modules_debugged: Vec> = vec! [ - Box::new(bar_modules::bar_module_weather::UnibarModuleWeather::new(self.opts.clone())), - Box::new(bar_modules::bar_module_music::UnibarModuleMusic::new(self.opts.clone())), - Box::new(bar_modules::bar_module_memory::UnibarModuleMemory::new(self.opts.clone())), - 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())), - ]; - for md in bar_modules_debugged { - md.post_debug(); - } - } + self.show_module_debug(); // Print parts provided by each module - println!("{}", parts.join(" ")); + println!("status {}", parts.join(" ")); + } + + // -------------------- + fn show_module_debug(&mut self) { + // Show module debug information if enabled + if self.opts.debug_modules { + if !self.debug_modules_done { + let bar_modules_debugged: Vec> = vec! [ + Box::new(bar_modules::bar_module_weather::UnibarModuleWeather::new(self.opts.clone())), + Box::new(bar_modules::bar_module_music::UnibarModuleMusic::new(self.opts.clone())), + Box::new(bar_modules::bar_module_memory::UnibarModuleMemory::new(self.opts.clone())), + 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())), + ]; + for md in bar_modules_debugged { + md.post_debug(); + } + self.debug_modules_done = true; + } + } } // -------------------- @@ -121,15 +147,18 @@ impl Unibar { // fn main() { let cmd_args = CommandlineArgs::parse(); - let app = Unibar { + let mut app = Unibar { opts: common::AppOptions { + interval: cmd_args.interval as u64, weather_units: if cmd_args.weather_metric { common::TemperatureUnits::Metric } else { common::TemperatureUnits::Imperial }, weather_station: cmd_args.weather_station, music_progress: cmd_args.music_progress, debug: cmd_args.debug, - debug_modules: cmd_args.debug_modules + debug_modules: cmd_args.debug_modules, }, + bar_modules_enabled: Vec::new(), + debug_modules_done: false, }; app.run();