Refactoring of bar modules

* Moved debug functions to separate trait
* Broke up module actions and moved assembling of icon and content
  in main instead of module itself. This will enable disabling of
  icon or content via options in the future
This commit is contained in:
2025-05-11 16:02:14 -07:00
parent ade570b8af
commit b7f83b4810
5 changed files with 140 additions and 52 deletions

View File

@@ -32,7 +32,7 @@ struct CommandlineArgs {
/// Show ICON debug information
#[arg(short = 'I', long)]
debug_icons: bool,
debug_modules: bool,
}
//
@@ -51,24 +51,45 @@ impl Unibar {
self.debug_msg("Debugging ...");
}
self.check_options();
let mut parts = Vec::new();
// 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_weather::UnibarModuleWeather { opts: self.opts.clone() }),
Box::new(bar_modules::bar_module_music::UnibarModuleMusic { opts: self.opts.clone() }),
Box::new(bar_modules::bar_module_weather::UnibarModuleWeather::new(self.opts.clone())),
Box::new(bar_modules::bar_module_music::UnibarModuleMusic::new(self.opts.clone())),
];
for md in bar_modules_enabled {
parts.push(md.get());
// Get module's part to be displayed in the bar
let mut parts = Vec::new();
for mut md in bar_modules_enabled {
let mut mod_parts = Vec::new();
// Each bar module implements following 3 steps:
// * Generate raw data with pertinent information
// * Return a unicode icon to be displayed
// * Return a String content to be displayed after the icon
//
// Following generates ICON+CONTENT string for a module to be displayed
// in the bar
md.generate_data();
mod_parts.push(md.get_icon());
mod_parts.push(md.get_content());
parts.push(mod_parts.join(" "));
}
// Show module debug information if enabled
if self.opts.debug_modules {
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())),
];
for md in bar_modules_debugged {
md.post_debug();
}
}
// Print parts provided by each module
println!("{}", parts.join(" | "));
if self.opts.debug_icons {
let w = bar_modules::bar_module_weather::UnibarModuleWeather { opts: self.opts.clone() };
w.get_icons();
w.show_icons();
}
}
// --------------------
@@ -99,7 +120,7 @@ fn main() {
weather_station: cmd_args.weather_station,
music_progress: cmd_args.music_progress,
debug_json: cmd_args.debug_json,
debug_icons: cmd_args.debug_icons
debug_modules: cmd_args.debug_modules
},
};