Impl. showing the maximum value in a dyn. netgraph

This commit is contained in:
Stephen Seo 2022-12-12 13:24:22 +09:00
parent 276ae6b94a
commit b35626c9fb
4 changed files with 58 additions and 3 deletions

View file

@ -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

View file

@ -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=<seconds>\t\t\t\tOutput at intervals of <seconds> (default 5)\n",

View file

@ -47,6 +47,7 @@ fn main() {
let mut net_width: Option<u16> = Some(11);
let mut net_graph_max: Option<f64> = 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<i64, _> = 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<f64>| {
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;

View file

@ -122,7 +122,11 @@ impl NetInfo {
Ok(())
}
pub fn get_netstring(&mut self, graph_max_opt: Option<f64>) -> 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<f64>,
) -> 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))
}
}