]> git.seodisparate.com - swaybar_info/commitdiff
Refactor main.rs handling of network stats
authorStephen Seo <seo.disparate@gmail.com>
Sun, 10 Jul 2022 08:47:51 +0000 (17:47 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Sun, 10 Jul 2022 08:47:51 +0000 (17:47 +0900)
src/args.rs
src/main.rs

index 248803fb7d0e7612bde2752a843cd708030071cf..fa843ac5060d851b45598b4b40ac7efaa5168bfe 100644 (file)
@@ -16,7 +16,7 @@ pub fn get_args() -> HashMap<String, String> {
         } else if arg.starts_with("--interval-sec=") {
             let (_, back) = arg.split_at(15);
             map.insert("interval-sec".into(), back.into());
-        } else if arg.starts_with("--help") || arg.starts_with("-h") {
+        } else if arg == "--help" || arg == "-h" {
             map.insert("help".into(), "".into());
         } else {
             let mut stderr_handle = io::stderr().lock();
@@ -32,7 +32,9 @@ pub fn get_args() -> HashMap<String, String> {
 pub fn print_usage() {
     let mut stderr_handle = io::stderr().lock();
     stderr_handle.write_all(b"Usage:\n").ok();
-    stderr_handle.write_all(b"  --help\t\t\tPrints help\n").ok();
+    stderr_handle
+        .write_all(b"  -h | --help\t\t\tPrints help\n")
+        .ok();
     stderr_handle
         .write_all(b"  --netdev=<device_name>\tCheck network traffic on specified device\n")
         .ok();
index 6b839cdcaeaa5380e1041bd9d4baa41c7af79f3f..ea7b0c80aa4789be18e571d4c2db8acd5b07928a 100644 (file)
@@ -73,62 +73,56 @@ fn main() {
         }
     };
 
+    let handle_net = |is_empty: bool,
+                      net: &mut proc::NetInfo,
+                      array: &mut SwaybarArray|
+     -> Result<(), proc::Error> {
+        net.update()?;
+        let netinfo_string = net.get_netstring()?;
+        let netinfo_parts: Vec<&str> = netinfo_string.split_whitespace().collect();
+
+        if is_empty {
+            {
+                let mut down_object = SwaybarObject::from_string(
+                    "net_down".to_owned(),
+                    format!("{} {}", netinfo_parts[0], netinfo_parts[1]),
+                );
+                down_object.color = Some("#ff8888ff".into());
+                array.push_object(down_object);
+            }
+
+            {
+                let mut up_object = SwaybarObject::from_string(
+                    "net_up".to_owned(),
+                    format!("{} {}", netinfo_parts[2], netinfo_parts[3]),
+                );
+                up_object.color = Some("#88ff88ff".into());
+                array.push_object(up_object);
+            }
+        } else {
+            if let Some(down_object) = array.get_by_name_mut("net_down") {
+                down_object
+                    .update_as_net_down(format!("{} {}", netinfo_parts[0], netinfo_parts[1]));
+            }
+
+            if let Some(up_object) = array.get_by_name_mut("net_up") {
+                up_object.update_as_net_up(format!("{} {}", netinfo_parts[2], netinfo_parts[3]));
+            }
+        }
+
+        Ok(())
+    };
+
     loop {
         let is_empty = array.is_empty();
 
         // network traffic
-        if let Some(net) = &mut net_obj {
-            if let Err(e) = net.update() {
+        if let Some(net) = net_obj.as_mut() {
+            if let Err(e) = handle_net(is_empty, net, &mut array) {
                 let mut stderr_handle = io::stderr().lock();
                 stderr_handle.write_all(e.to_string().as_bytes()).ok();
                 net_obj = None;
                 set_net_error(is_empty, &mut array);
-            } else {
-                let netinfo_result = net.get_netstring();
-                if let Err(e) = netinfo_result {
-                    {
-                        let mut stderr_handle = io::stderr().lock();
-                        stderr_handle.write_all(e.to_string().as_bytes()).ok();
-                    }
-                    set_net_error(is_empty, &mut array);
-                } else {
-                    let netinfo_string = netinfo_result.unwrap();
-                    let netinfo_parts: Vec<&str> = netinfo_string.split_whitespace().collect();
-
-                    if is_empty {
-                        {
-                            let mut down_object = SwaybarObject::from_string(
-                                "net_down".to_owned(),
-                                format!("{} {}", netinfo_parts[0], netinfo_parts[1]),
-                            );
-                            down_object.color = Some("#ff8888ff".into());
-                            array.push_object(down_object);
-                        }
-
-                        {
-                            let mut up_object = SwaybarObject::from_string(
-                                "net_up".to_owned(),
-                                format!("{} {}", netinfo_parts[2], netinfo_parts[3]),
-                            );
-                            up_object.color = Some("#88ff88ff".into());
-                            array.push_object(up_object);
-                        }
-                    } else {
-                        if let Some(down_object) = array.get_by_name_mut("net_down") {
-                            down_object.update_as_net_down(format!(
-                                "{} {}",
-                                netinfo_parts[0], netinfo_parts[1]
-                            ));
-                        }
-
-                        if let Some(up_object) = array.get_by_name_mut("net_up") {
-                            up_object.update_as_net_up(format!(
-                                "{} {}",
-                                netinfo_parts[2], netinfo_parts[3]
-                            ));
-                        }
-                    }
-                }
             }
         }