Compare commits

..

No commits in common. "9b79fcd904ea10b78a1349daef4847fe35dfbee4" and "2be0d5665508b27d1c129adba961ad67ae21d16e" have entirely different histories.

2 changed files with 18 additions and 33 deletions

View File

@ -38,21 +38,18 @@ impl bar_modules::BarModuleActions for UnibarModuleNetwork {
self.ip_addr_stdout = String::from_utf8(ip_addr_output.stdout).unwrap(); self.ip_addr_stdout = String::from_utf8(ip_addr_output.stdout).unwrap();
self.ip_addr_stdout.pop(); self.ip_addr_stdout.pop();
let network_data: Value = serde_json::from_str::<Value>(self.ip_addr_stdout.as_str()).unwrap(); self.network_info = serde_json::from_str::<Value>(self.ip_addr_stdout.as_str()).unwrap();
if self.opts.debug_json {
println!("-----> ip_addr - {:#?}", self.network_info);
}
}
if let Some(interfaces) = network_data.as_array() { // --------------------
if self.opts.debug_json { fn get_content(&self) -> String {
println!("-----> network_data - {:#?}", network_data); if let Some(interfaces) = self.network_info.as_array() {
} let up_intf :Vec<_> = interfaces.iter()
// Get all interfaces that are up
let mut up_intf :Vec<_> = interfaces.iter()
.filter(|el| el["operstate"].as_str().unwrap().contains("UP")) .filter(|el| el["operstate"].as_str().unwrap().contains("UP"))
.cloned().collect(); .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 { if up_intf.len() > 0 {
let inet_addr :Vec<_> = up_intf[0]["addr_info"].as_array().unwrap().iter() let inet_addr :Vec<_> = up_intf[0]["addr_info"].as_array().unwrap().iter()
.filter(|ai| ai["scope"].as_str().unwrap().contains("global")) .filter(|ai| ai["scope"].as_str().unwrap().contains("global"))
@ -61,36 +58,24 @@ impl bar_modules::BarModuleActions for UnibarModuleNetwork {
if self.opts.debug_json { if self.opts.debug_json {
println!("-----> Inet Addr - {:#?}", inet_addr); println!("-----> Inet Addr - {:#?}", inet_addr);
} }
self.network_info = inet_addr[0].clone(); return inet_addr[0]["local"].as_str().unwrap().to_string();
self.network_info["ifname"] = up_intf[0]["ifname"].clone();
} }
} }
} }
if self.opts.debug_json { return "None".to_string();
println!("-----> ip_addr - {:#?}", self.network_info);
}
}
// --------------------
fn get_content(&self) -> String {
if self.network_info != serde_json::Value::Null {
// If any interface was up, return the local IP address
return self.network_info["local"].as_str().unwrap().to_string();
}
return "Network down".to_string();
} }
// -------------------- // --------------------
fn get_icon(&self) -> String { fn get_icon(&self) -> String {
if self.network_info != serde_json::Value::Null { if let Some(interfaces) = self.network_info.as_array() {
// Select icon based on which interface is up (ethernet or wifi) let up_intf :Vec<_> = interfaces.iter()
if self.network_info["ifname"].as_str().unwrap().to_string().starts_with("w") { .filter(|el| el["operstate"].as_str().unwrap().contains("UP"))
.cloned().collect();
if up_intf.len() > 0 {
return "📶".to_string(); return "📶".to_string();
} else {
return "🌎".to_string();
} }
} }
return "📡".to_string(); return "🌎".to_string();
} }
} }

View File

@ -53,9 +53,9 @@ impl Unibar {
// Set up a list of all modules to be used // Set up a list of all modules to be used
let bar_modules_enabled: Vec<Box<dyn bar_modules::BarModuleActions>> = vec! [ let bar_modules_enabled: Vec<Box<dyn bar_modules::BarModuleActions>> = 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_music::UnibarModuleMusic::new(self.opts.clone())),
Box::new(bar_modules::bar_module_network::UnibarModuleNetwork::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())),
]; ];
// Get module's part to be displayed in the bar // Get module's part to be displayed in the bar