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 { 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) { fn generate_data(&mut self) {
self.cpu_times_sample_1 = self.read_cpu_times().expect("Trouble getting CPU times sample 1"); 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 { impl bar_modules::BarModuleActions for UnibarModuleDate {
// -------------------- // --------------------
fn generate_data(&mut self) { fn clear(&mut self) {
self.date_time = Local::now(); self.date_time = Local::now();
}
// --------------------
fn generate_data(&mut self) {
if self.opts.debug { if self.opts.debug {
println!("-----> Date dump {:#?}", self.date_time); println!("-----> Date dump {:#?}", self.date_time);
} }

View File

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

View File

@ -5,26 +5,33 @@ use crate::bar_modules;
#[derive(Clone)] #[derive(Clone)]
pub struct UnibarModuleMusic { pub struct UnibarModuleMusic {
opts: common::AppOptions, opts: common::AppOptions,
current_stdout :String, current_stdout: String,
progress_stdout :String, progress_stdout: String,
state_stdout: String, state_stdout: String,
} }
impl UnibarModuleMusic { impl UnibarModuleMusic {
// -------------------- // --------------------
pub fn new(o :common::AppOptions) -> Self { pub fn new(o :common::AppOptions) -> Self {
UnibarModuleMusic { UnibarModuleMusic {
opts: o, opts: o,
current_stdout: "".to_string(), current_stdout: "".to_string(),
progress_stdout: "".to_string(), progress_stdout: "".to_string(),
state_stdout: "stopped".to_string(), state_stdout: "stopped".to_string(),
} }
} }
} }
impl bar_modules::BarModuleActions for 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) { fn generate_data(&mut self) {
// MPD format options here: // MPD format options here:

View File

@ -26,43 +26,54 @@ impl UnibarModuleNetwork {
impl bar_modules::BarModuleActions for UnibarModuleNetwork { impl bar_modules::BarModuleActions for UnibarModuleNetwork {
// --------------------
fn clear(&mut self) {
self.network_info = json!(serde_json::Value::Null);
}
// -------------------- // --------------------
fn generate_data(&mut self) { fn generate_data(&mut self) {
// Output of 'ip -j address' command has network information // 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("-j") // Output in json format
.arg("address") .arg("address")
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.output() .output() {
.unwrap(); Err(e) => {
self.ip_addr_stdout = String::from_utf8(ip_addr_output.stdout).unwrap(); eprintln!("Error getting network information {e}");
self.ip_addr_stdout.pop(); self.network_info = json!(serde_json::Value::Null);
let network_data: Value = serde_json::from_str::<Value>(self.ip_addr_stdout.as_str()).unwrap();
if let Some(interfaces) = network_data.as_array() {
if self.opts.debug {
println!("-----> network_data - {:#?}", network_data);
} }
// Get all interfaces that are up Ok(ip_addr_output) => {
let mut up_intf :Vec<_> = interfaces.iter() self.ip_addr_stdout = String::from_utf8(ip_addr_output.stdout).unwrap();
.filter(|el| el["operstate"].as_str().unwrap().contains("UP")) self.ip_addr_stdout.pop();
.cloned().collect();
// Sort by 'ifname'. This is an unreliable way to proiritize ethernet over wifi. let network_data: Value = serde_json::from_str::<Value>(self.ip_addr_stdout.as_str()).unwrap();
// Ethenet network interface names normally start with 'e' and wifi interface names
// start with 'w' if let Some(interfaces) = network_data.as_array() {
up_intf.sort_by(|a, b| a["ifname"].as_str().unwrap().cmp(b["ifname"].as_str().unwrap()));
if up_intf.len() > 0 {
let inet_addr :Vec<_> = up_intf[0]["addr_info"].as_array().unwrap().iter()
.filter(|ai| ai["scope"].as_str().unwrap().contains("global"))
.cloned().collect();
if inet_addr.len() > 0 {
if self.opts.debug { if self.opts.debug {
println!("-----> Inet Addr - {:#?}", inet_addr); println!("-----> network_data - {:#?}", network_data);
}
// Get all interfaces that are up
let mut up_intf :Vec<_> = interfaces.iter()
.filter(|el| el["operstate"].as_str().unwrap().contains("UP"))
.cloned().collect();
// Sort by 'ifname'. This is an unreliable way to proiritize ethernet over wifi.
// Ethenet network interface names normally start with 'e' and wifi interface names
// start with 'w'
up_intf.sort_by(|a, b| a["ifname"].as_str().unwrap().cmp(b["ifname"].as_str().unwrap()));
if up_intf.len() > 0 {
let inet_addr :Vec<_> = up_intf[0]["addr_info"].as_array().unwrap().iter()
.filter(|ai| ai["scope"].as_str().unwrap().contains("global"))
.cloned().collect();
if inet_addr.len() > 0 {
if self.opts.debug {
println!("-----> Inet Addr - {:#?}", inet_addr);
}
self.network_info = inet_addr[0].clone();
self.network_info["ifname"] = up_intf[0]["ifname"].clone();
}
} }
self.network_info = inet_addr[0].clone();
self.network_info["ifname"] = up_intf[0]["ifname"].clone();
} }
} }
} }

View File

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

View File

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

View File

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

View File

@ -1,6 +1,9 @@
// -------------------- // --------------------
/// All Bar modules must implement the actions /// All Bar modules must implement the actions
pub trait BarModuleActions { 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 /// Do necessary processing to generate data for this bar module
fn generate_data(&mut self); fn generate_data(&mut self);

View File

@ -86,13 +86,15 @@ impl Unibar {
for md in &mut self.bar_modules_enabled { for md in &mut self.bar_modules_enabled {
let mut mod_parts = Vec::new(); 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 // * Generate raw data with pertinent information
// * Return a unicode icon to be displayed // * Return a unicode icon to be displayed
// * Return a String content to be displayed after the icon // * Return a String content to be displayed after the icon
// //
// Following generates ICON+CONTENT string for a module to be displayed // Following generates ICON+CONTENT string for a module to be displayed
// in the bar // in the bar
md.clear();
md.generate_data(); md.generate_data();
mod_parts.push(md.get_icon()); mod_parts.push(md.get_icon());
mod_parts.push(md.get_content()); mod_parts.push(md.get_content());