Added forever-loop to update bar contents at interval

* New argument --interval to specify update interval
This commit is contained in:
Mahesh Asolkar 2025-06-07 20:14:34 -07:00
parent 51253240b3
commit 0b7ff61da0
Signed by: asolkar
GPG Key ID: 371CA8164433BDCC
2 changed files with 66 additions and 36 deletions

View File

@ -23,6 +23,7 @@ impl fmt::Display for TemperatureUnits {
// --------------------
#[derive(Debug,Clone)]
pub struct AppOptions {
pub interval: u64,
pub weather_units: TemperatureUnits,
pub weather_station: String,
pub music_progress: bool,

View File

@ -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,
bar_modules_enabled: Vec<Box<dyn bar_modules::BarModuleActions>>,
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<Box<dyn bar_modules::BarModuleActions>> = 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,8 +99,17 @@ impl Unibar {
parts.push(mod_parts.join(" "));
}
self.show_module_debug();
// Print parts provided by each module
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<Box<dyn bar_modules::BarModuleDebug>> = 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())),
@ -94,10 +121,9 @@ impl Unibar {
for md in bar_modules_debugged {
md.post_debug();
}
self.debug_modules_done = true;
}
}
// Print parts provided by each module
println!("{}", parts.join(" "));
}
// --------------------
@ -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();