New network module

This commit is contained in:
Mahesh Asolkar 2025-05-22 17:49:30 -07:00
parent a2d9c768f8
commit 2be0d56655
Signed by: asolkar
GPG Key ID: 371CA8164433BDCC
3 changed files with 90 additions and 1 deletions

View File

@ -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::<Value>(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) {
}
}

View File

@ -19,3 +19,4 @@ pub trait BarModuleDebug {
pub mod bar_module_weather;
pub mod bar_module_music;
pub mod bar_module_network;

View File

@ -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<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_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<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())),
Box::new(bar_modules::bar_module_network::UnibarModuleNetwork::new(self.opts.clone())),
];
for md in bar_modules_debugged {
md.post_debug();