From b35626c9fb7972727f5a5009e9804681b7551e20 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 12 Dec 2022 13:24:22 +0900 Subject: [PATCH] Impl. showing the maximum value in a dyn. netgraph --- Changelog.md | 2 ++ src/args.rs | 5 +++++ src/main.rs | 32 +++++++++++++++++++++++++++++++- src/proc.rs | 22 ++++++++++++++++++++-- 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/Changelog.md b/Changelog.md index a48a67a..036344a 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,6 +2,8 @@ ## Upcoming Changes +Impl. showing the maximum value in a dynamic netgraph. + ## 0.1.7 When swaybar\_info starts, it no longer displays the traffic amount leading up diff --git a/src/args.rs b/src/args.rs index 96d4027..c8323fe 100644 --- a/src/args.rs +++ b/src/args.rs @@ -25,6 +25,8 @@ pub fn get_args() -> ArgsResult { } else if arg.starts_with("--netgraph_max_bytes=") { let (_, back) = arg.split_at(21); map.insert("netgraph".into(), back.into()); + } else if arg.starts_with("--netgraph_dyn_display") { + map.insert("netgraph-dyndisplay".into(), String::new()); } else if arg.starts_with("--interval-sec=") { let (_, back) = arg.split_at(15); map.insert("interval-sec".into(), back.into()); @@ -67,6 +69,9 @@ pub fn print_usage() { stderr_handle .write_all(b" \t\t\t\t (Set to \"dynamic\" instead of a byte count for dynamic sizing)\n") .ok(); + stderr_handle + .write_all(b" --netgraph_dyn_display\t\t\t\tEnable showing the current maximum value in the graph\n") + .ok(); stderr_handle .write_all( b" --interval-sec=\t\t\t\tOutput at intervals of (default 5)\n", diff --git a/src/main.rs b/src/main.rs index d0d0d58..2423f0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,6 +47,7 @@ fn main() { let mut net_width: Option = Some(11); let mut net_graph_max: Option = None; let mut net_graph_is_dynamic: bool = false; + let mut net_graph_show_dynamic_max: bool = false; let mut interval: Duration = Duration::from_secs(5); if args_result.map.contains_key("netdev") { net_obj = Some(proc::NetInfo::new( @@ -84,6 +85,9 @@ fn main() { } } } + if args_result.map.contains_key("netgraph-dyndisplay") { + net_graph_show_dynamic_max = true; + } if args_result.map.contains_key("interval-sec") { let seconds: Result = args_result.map.get("interval-sec").unwrap().parse(); if let Ok(seconds_value) = seconds { @@ -128,6 +132,13 @@ fn main() { let mut array = SwaybarArray::new(); let set_net_error = |is_empty: bool, array: &mut SwaybarArray, graph_max_opt: &Option| { if is_empty { + if net_graph_is_dynamic && net_graph_show_dynamic_max { + array.push_object(SwaybarObject::from_error_string( + "net_graph_dyn_max".to_owned(), + "net ERROR".into(), + )); + } + if graph_max_opt.is_some() || net_graph_is_dynamic { array.push_object(SwaybarObject::from_error_string( "net_graph".to_owned(), @@ -142,6 +153,12 @@ fn main() { let up_obj = SwaybarObject::from_error_string("net_up".to_owned(), "Net ERROR".into()); array.push_object(up_obj); } else { + if net_graph_is_dynamic && net_graph_show_dynamic_max { + if let Some(dyn_max) = array.get_by_name_mut("net_graph_dyn_max") { + dyn_max.update_as_error("Net ERROR".to_owned()); + } + } + if graph_max_opt.is_some() || net_graph_is_dynamic { if let Some(graph_ref) = array.get_by_name_mut("net_graph") { graph_ref.update_as_error("Net ERROR".to_owned()); @@ -165,10 +182,17 @@ fn main() { array: &mut SwaybarArray| -> Result<(), proc::Error> { net.update()?; - let (netinfo_string, graph_string) = net.get_netstring(net_graph_max)?; + let (netinfo_string, graph_string, history_max) = net.get_netstring(net_graph_max)?; let netinfo_parts: Vec<&str> = netinfo_string.split_whitespace().collect(); if is_empty { + if net_graph_is_dynamic && net_graph_show_dynamic_max { + let mut graph_obj = + SwaybarObject::from_string("net_graph_dyn_max".to_owned(), history_max); + graph_obj.color = Some("#ffff88".into()); + array.push_object(graph_obj); + } + if net_graph_max.is_some() || net_graph_is_dynamic { let mut graph_obj = SwaybarObject::from_string("net_graph".to_owned(), graph_string); @@ -207,6 +231,12 @@ fn main() { array.push_object(up_object); } } else { + if net_graph_is_dynamic && net_graph_show_dynamic_max { + if let Some(graph_obj) = array.get_by_name_mut("net_graph_dyn_max") { + graph_obj.full_text = history_max; + } + } + if net_graph_max.is_some() || net_graph_is_dynamic { if let Some(graph_obj) = array.get_by_name_mut("net_graph") { graph_obj.full_text = graph_string; diff --git a/src/proc.rs b/src/proc.rs index d5a4dfb..8a4b002 100644 --- a/src/proc.rs +++ b/src/proc.rs @@ -122,7 +122,11 @@ impl NetInfo { Ok(()) } - pub fn get_netstring(&mut self, graph_max_opt: Option) -> Result<(String, String), Error> { + // Returns netinfo down/up, graph, and history_max (if dynamic is enabled) + pub fn get_netstring( + &mut self, + graph_max_opt: Option, + ) -> Result<(String, String, String), Error> { let down_diff: f64 = if self.down > self.prev_down { let value = (self.down - self.prev_down) as f64; self.prev_down = self.down; @@ -161,6 +165,8 @@ impl NetInfo { up_diff }; + let mut diff_max_string = String::new(); + if let Some(graph_max) = graph_max_opt { let graph_value: u8 = if diff_max > graph_max { 8 @@ -191,6 +197,18 @@ impl NetInfo { } } + if history_max > 1024.0 * 1024.0 { + write!( + &mut diff_max_string, + "{:.2} MiB", + history_max / (1024.0 * 1024.0) + )?; + } else if history_max > 1024.0 { + write!(&mut diff_max_string, "{:.2} KiB", history_max / 1024.0)?; + } else { + write!(&mut diff_max_string, "{:.0} B", history_max)?; + } + self.graph.clear(); if history_max == 0.0 { self.graph = String::from(" "); @@ -212,7 +230,7 @@ impl NetInfo { assert_eq!(self.graph_history.len(), 10); } - Ok((output, self.graph.clone())) + Ok((output, self.graph.clone(), diff_max_string)) } }