From 35ff17ef0f0eaee8b8ff47123e7f4f8cf062ea30 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sun, 10 Jul 2022 17:47:51 +0900 Subject: [PATCH] Refactor main.rs handling of network stats --- src/args.rs | 6 ++-- src/main.rs | 90 +++++++++++++++++++++++++---------------------------- 2 files changed, 46 insertions(+), 50 deletions(-) diff --git a/src/args.rs b/src/args.rs index 248803f..fa843ac 100644 --- a/src/args.rs +++ b/src/args.rs @@ -16,7 +16,7 @@ pub fn get_args() -> HashMap { } 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 { 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=\tCheck network traffic on specified device\n") .ok(); diff --git a/src/main.rs b/src/main.rs index 6b839cd..ea7b0c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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] - )); - } - } - } } }