diff --git a/src/bar_modules/bar_module_network.rs b/src/bar_modules/bar_module_network.rs new file mode 100644 index 0000000..dab65da --- /dev/null +++ b/src/bar_modules/bar_module_network.rs @@ -0,0 +1,87 @@ +use serde_json::json; +use serde_json::Value; +use std::process::{Stdio, Command}; + +use crate::common; +use crate::bar_modules; + +#[derive(Clone)] +pub struct UnibarModuleNetwork { + opts: common::AppOptions, + ip_addr_stdout: String, + network_info: Value, +} + +impl UnibarModuleNetwork { + + // -------------------- + pub fn new(o :common::AppOptions) -> Self { + UnibarModuleNetwork { + opts: o, + ip_addr_stdout: "".to_string(), + network_info: json!(serde_json::Value::Null), + } + } +} + +impl bar_modules::BarModuleActions for UnibarModuleNetwork { + + // -------------------- + fn generate_data(&mut self) { + // Output of 'ip -j address' command has network information + let ip_addr_output = Command::new("ip") + .arg("-j") // Output in json format + .arg("address") + .stdout(Stdio::piped()) + .output() + .unwrap(); + self.ip_addr_stdout = String::from_utf8(ip_addr_output.stdout).unwrap(); + self.ip_addr_stdout.pop(); + + self.network_info = serde_json::from_str::(self.ip_addr_stdout.as_str()).unwrap(); + if self.opts.debug_json { + println!("-----> ip_addr - {:#?}", self.network_info); + } + } + + // -------------------- + fn get_content(&self) -> String { + if let Some(interfaces) = self.network_info.as_array() { + let up_intf :Vec<_> = interfaces.iter() + .filter(|el| el["operstate"].as_str().unwrap().contains("UP")) + .cloned().collect(); + 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_json { + println!("-----> Inet Addr - {:#?}", inet_addr); + } + return inet_addr[0]["local"].as_str().unwrap().to_string(); + } + } + } + return "None".to_string(); + } + + // -------------------- + fn get_icon(&self) -> String { + if let Some(interfaces) = self.network_info.as_array() { + let up_intf :Vec<_> = interfaces.iter() + .filter(|el| el["operstate"].as_str().unwrap().contains("UP")) + .cloned().collect(); + if up_intf.len() > 0 { + return "📶".to_string(); + } + } + return "🌎".to_string(); + } +} + +impl bar_modules::BarModuleDebug for UnibarModuleNetwork { + + // -------------------- + fn post_debug(&self) { + } +} diff --git a/src/bar_modules/mod.rs b/src/bar_modules/mod.rs index 96e934e..facfcf6 100644 --- a/src/bar_modules/mod.rs +++ b/src/bar_modules/mod.rs @@ -19,3 +19,4 @@ pub trait BarModuleDebug { pub mod bar_module_weather; pub mod bar_module_music; +pub mod bar_module_network; diff --git a/src/main.rs b/src/main.rs index 3fee6f3..ef80219 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,7 +40,6 @@ struct CommandlineArgs { // #[derive(Clone)] struct Unibar { - // acts :UnibarActions opts: common::AppOptions, } @@ -56,6 +55,7 @@ impl Unibar { let bar_modules_enabled: Vec> = 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_network::UnibarModuleNetwork::new(self.opts.clone())), ]; // Get module's part to be displayed in the bar @@ -82,6 +82,7 @@ impl Unibar { let bar_modules_debugged: Vec> = 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_network::UnibarModuleNetwork::new(self.opts.clone())), ]; for md in bar_modules_debugged { md.post_debug();