]> git.seodisparate.com - swaybar_info/commitdiff
Impl get info from /proc/net/dev
authorStephen Seo <seo.disparate@gmail.com>
Sat, 9 Jul 2022 08:33:19 +0000 (17:33 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Sat, 9 Jul 2022 08:33:19 +0000 (17:33 +0900)
src/args.rs [new file with mode: 0644]
src/main.rs
src/proc.rs

diff --git a/src/args.rs b/src/args.rs
new file mode 100644 (file)
index 0000000..0d839c6
--- /dev/null
@@ -0,0 +1,14 @@
+use std::collections::HashMap;
+
+pub fn get_args() -> HashMap<String, String> {
+    let mut map = HashMap::new();
+
+    for arg in std::env::args() {
+        if arg.starts_with("--netdev=") {
+            let (_, back) = arg.split_at(9);
+            map.insert("netdev".into(), back.into());
+        }
+    }
+
+    map
+}
index 3122c314f718632b61ccab7675aa1a0b96d03d42..28c6cf489987d5e314cf8d3432ee5fd019595586 100644 (file)
@@ -1,7 +1,17 @@
+mod args;
 mod proc;
 mod swaybar_object;
 
 fn main() {
+    let args_map = args::get_args();
+
+    let mut net_obj = None;
+    if args_map.contains_key("netdev") {
+        net_obj = Some(proc::NetInfo::new(
+            args_map.get("netdev").unwrap().to_owned(),
+        ));
+    }
+
     println!(
         "{}",
         serde_json::to_string(&swaybar_object::SwaybarHeader::new())
@@ -20,5 +30,15 @@ fn main() {
         let loadavg_object = swaybar_object::SwaybarObject::from_string(loadavg_string);
         array.push_object(loadavg_object);
     }
+
+    if let Some(mut netinfo) = net_obj {
+        for _i in 0..10 {
+            netinfo.update().expect("netinfo.update() shouldn't fail");
+            let netinfo_string = netinfo.get_netstring();
+            array.push_object(swaybar_object::SwaybarObject::from_string(netinfo_string));
+            std::thread::sleep(std::time::Duration::from_secs(1));
+        }
+    }
+
     println!("{}", array);
 }
index 07ca3bc4809d7362ab717e94f90cd72a9066d00f..90fc666d1bd7075197267fbdbe909858e5eebc7b 100644 (file)
@@ -2,6 +2,88 @@ use std::fs::File;
 use std::io;
 use std::io::prelude::*;
 
+pub struct NetInfo {
+    dev_name: String,
+    down: u64,
+    prev_down: u64,
+    up: u64,
+    prev_up: u64,
+}
+
+impl NetInfo {
+    pub fn new(dev_name: String) -> Self {
+        Self {
+            dev_name,
+            down: 0,
+            prev_down: 0,
+            up: 0,
+            prev_up: 0,
+        }
+    }
+
+    pub fn update(&mut self) -> io::Result<()> {
+        let mut netdev_string = String::new();
+        {
+            let mut netdev_file: File = File::open("/proc/net/dev")?;
+            netdev_file.read_to_string(&mut netdev_string)?;
+        }
+
+        let mut dev_line: Option<String> = None;
+        for line in netdev_string.lines() {
+            if line.starts_with(&self.dev_name) {
+                dev_line = Some(line.to_owned());
+                break;
+            }
+        }
+
+        if let Some(line) = dev_line {
+            let entries: Vec<&str> = line.split_whitespace().collect();
+            if entries.len() < 10 {
+                return Err(io::Error::new(io::ErrorKind::Other, format!("NetInfo::update: Failed to parse /proc/net/dev, \"{}\" device line is too short", self.dev_name)));
+            }
+
+            self.down = entries[1].parse().map_err(|_| io::Error::new(io::ErrorKind::Other, format!("NetInfo::update: Failed to parse recv bytes in /proc/net/dev for device \"{}\"", self.dev_name)))?;
+            self.up = entries[9].parse().map_err(|_| io::Error::new(io::ErrorKind::Other, format!("NetInfo::update: Failed to parse recv bytes in /proc/net/dev for device \"{}\"", self.dev_name)))?;
+        } else {
+            return Err(io::Error::new(
+                io::ErrorKind::Other,
+                format!(
+                    "NetInfo::update: Failed to parse /proc/net/dev, can't find net device \"{}\"",
+                    self.dev_name
+                ),
+            ));
+        }
+
+        Ok(())
+    }
+
+    pub fn get_netstring(&mut self) -> String {
+        let down_diff = self.down - self.prev_down;
+        self.prev_down = self.down;
+        let up_diff = self.up - self.prev_up;
+        self.prev_up = self.up;
+
+        let mut output = String::new();
+        if down_diff > 1024 * 1024 {
+            output.push_str(&format!("{} MiB ", down_diff / 1024 / 1024));
+        } else if down_diff > 1024 {
+            output.push_str(&format!("{} KiB ", down_diff / 1024));
+        } else {
+            output.push_str(&format!("{} B ", down_diff));
+        }
+
+        if up_diff > 1024 * 1024 {
+            output.push_str(&format!("{} MiB", up_diff / 1024 / 1024));
+        } else if up_diff > 1024 {
+            output.push_str(&format!("{} KiB", up_diff / 1024));
+        } else {
+            output.push_str(&format!("{} B", up_diff));
+        }
+
+        output
+    }
+}
+
 pub fn get_meminfo() -> io::Result<String> {
     let mut meminfo_string = String::new();
     {