Clear data before new bar display iteration

This makes sure that data displayed is always fresh
This commit is contained in:
Mahesh Asolkar 2025-06-07 21:19:02 -07:00
parent 4452cdbaa0
commit 79e1a5956e
Signed by: asolkar
GPG Key ID: 371CA8164433BDCC
10 changed files with 93 additions and 42 deletions

View File

@ -116,6 +116,12 @@ impl UnibarModuleCpu {
impl bar_modules::BarModuleActions for UnibarModuleCpu {
// --------------------
fn clear(&mut self) {
self.cpu_times_sample_1 = CpuTimes::new();
self.cpu_times_sample_2 = CpuTimes::new();
}
// --------------------
fn generate_data(&mut self) {
self.cpu_times_sample_1 = self.read_cpu_times().expect("Trouble getting CPU times sample 1");

View File

@ -21,8 +21,12 @@ impl UnibarModuleDate {
impl bar_modules::BarModuleActions for UnibarModuleDate {
// --------------------
fn generate_data(&mut self) {
fn clear(&mut self) {
self.date_time = Local::now();
}
// --------------------
fn generate_data(&mut self) {
if self.opts.debug {
println!("-----> Date dump {:#?}", self.date_time);
}

View File

@ -25,6 +25,11 @@ impl UnibarModuleMemory {
impl bar_modules::BarModuleActions for UnibarModuleMemory {
// --------------------
fn clear(&mut self) {
self.meminfo_str = "".to_string();
}
// --------------------
fn generate_data(&mut self) {
let path = Path::new("/proc/meminfo");
@ -42,7 +47,6 @@ impl bar_modules::BarModuleActions for UnibarModuleMemory {
},
};
self.meminfo_str.pop();
}
// --------------------

View File

@ -25,6 +25,13 @@ impl UnibarModuleMusic {
impl bar_modules::BarModuleActions for UnibarModuleMusic {
// --------------------
fn clear(&mut self) {
self.current_stdout = "".to_string();
self.progress_stdout = "".to_string();
self.state_stdout = "stopped".to_string();
}
// --------------------
fn generate_data(&mut self) {
// MPD format options here:

View File

@ -26,15 +26,24 @@ impl UnibarModuleNetwork {
impl bar_modules::BarModuleActions for UnibarModuleNetwork {
// --------------------
fn clear(&mut self) {
self.network_info = json!(serde_json::Value::Null);
}
// --------------------
fn generate_data(&mut self) {
// Output of 'ip -j address' command has network information
let ip_addr_output = Command::new("ip")
match Command::new("ip")
.arg("-j") // Output in json format
.arg("address")
.stdout(Stdio::piped())
.output()
.unwrap();
.output() {
Err(e) => {
eprintln!("Error getting network information {e}");
self.network_info = json!(serde_json::Value::Null);
}
Ok(ip_addr_output) => {
self.ip_addr_stdout = String::from_utf8(ip_addr_output.stdout).unwrap();
self.ip_addr_stdout.pop();
@ -66,6 +75,8 @@ impl bar_modules::BarModuleActions for UnibarModuleNetwork {
}
}
}
}
}
if self.opts.debug {
println!("-----> ip_addr - {:#?}", self.network_info);
}

View File

@ -38,6 +38,11 @@ impl UnibarModulePower {
impl bar_modules::BarModuleActions for UnibarModulePower {
// --------------------
fn clear(&mut self) {
self.power_info = PowerData::new();
}
// --------------------
fn generate_data(&mut self) {
// Following command is used to get power data:

View File

@ -21,8 +21,12 @@ impl UnibarModuleTime {
impl bar_modules::BarModuleActions for UnibarModuleTime {
// --------------------
fn generate_data(&mut self) {
fn clear(&mut self) {
self.date_time = Local::now();
}
// --------------------
fn generate_data(&mut self) {
if self.opts.debug {
println!("-----> Time dump {:#?}", self.date_time);
}

View File

@ -150,6 +150,11 @@ impl UnibarModuleWeather {
impl bar_modules::BarModuleActions for UnibarModuleWeather {
// --------------------
fn clear(&mut self) {
self.weather_info = json!(serde_json::Value::Null);
}
// --------------------
fn generate_data(&mut self) {
// Print a web page onto stdout

View File

@ -1,6 +1,9 @@
// --------------------
/// All Bar modules must implement the actions
pub trait BarModuleActions {
/// Do necessary clean up before starting new fetch
fn clear(&mut self);
/// Do necessary processing to generate data for this bar module
fn generate_data(&mut self);

View File

@ -86,13 +86,15 @@ impl Unibar {
for md in &mut self.bar_modules_enabled {
let mut mod_parts = Vec::new();
// Each bar module implements following 3 steps:
// Each bar module implements following 4 steps:
// * Clear data from previous iteration
// * 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.clear();
md.generate_data();
mod_parts.push(md.get_icon());
mod_parts.push(md.get_content());