Impl dynamic netgraph display

The netgraph can now dynamically "resize" to always have the maximum
bytes transferred in an interval be the max value.
This commit is contained in:
Stephen Seo 2022-12-11 21:49:51 +09:00
parent 7c38f9cf32
commit 3889eb6dcc
2 changed files with 85 additions and 28 deletions

View file

@ -46,6 +46,7 @@ fn main() {
let mut net_obj: Option<proc::NetInfo> = None; let mut net_obj: Option<proc::NetInfo> = None;
let mut net_width: Option<u16> = Some(10); let mut net_width: Option<u16> = Some(10);
let mut net_graph_max: Option<f64> = None; let mut net_graph_max: Option<f64> = None;
let mut net_graph_is_dynamic: bool = false;
let mut interval: Duration = Duration::from_secs(5); let mut interval: Duration = Duration::from_secs(5);
if args_result.map.contains_key("netdev") { if args_result.map.contains_key("netdev") {
net_obj = Some(proc::NetInfo::new( net_obj = Some(proc::NetInfo::new(
@ -67,19 +68,23 @@ fn main() {
} }
} }
if args_result.map.contains_key("netgraph") { if args_result.map.contains_key("netgraph") {
let graph_max_result: Result<f64, _> = args_result.map.get("netgraph").unwrap().parse(); if args_result.map.get("netgraph").as_ref().unwrap() == &"dynamic" {
if let Ok(graph_max) = graph_max_result { net_graph_is_dynamic = true;
net_graph_max = Some(graph_max);
} else { } else {
let mut stderr_handle = io::stderr().lock(); let graph_max_result: Result<f64, _> = args_result.map.get("netgraph").unwrap().parse();
stderr_handle if let Ok(graph_max) = graph_max_result {
.write_all( net_graph_max = Some(graph_max);
format!( } else {
"WARNING: Invalid value passed to --netgraph_max_bytes=..., ignoring...\n" let mut stderr_handle = io::stderr().lock();
stderr_handle
.write_all(
format!(
"WARNING: Invalid value passed to --netgraph_max_bytes=..., ignoring...\n"
)
.as_bytes(),
) )
.as_bytes(), .ok();
) }
.ok();
} }
} }
if args_result.map.contains_key("interval-sec") { if args_result.map.contains_key("interval-sec") {
@ -126,7 +131,7 @@ fn main() {
let mut array = SwaybarArray::new(); let mut array = SwaybarArray::new();
let set_net_error = |is_empty: bool, array: &mut SwaybarArray, graph_max_opt: &Option<f64>| { let set_net_error = |is_empty: bool, array: &mut SwaybarArray, graph_max_opt: &Option<f64>| {
if is_empty { if is_empty {
if graph_max_opt.is_some() { if graph_max_opt.is_some() || net_graph_is_dynamic {
array.push_object(SwaybarObject::from_error_string( array.push_object(SwaybarObject::from_error_string(
"net_graph".to_owned(), "net_graph".to_owned(),
"net ERROR".into(), "net ERROR".into(),
@ -140,7 +145,7 @@ fn main() {
let up_obj = SwaybarObject::from_error_string("net_up".to_owned(), "Net ERROR".into()); let up_obj = SwaybarObject::from_error_string("net_up".to_owned(), "Net ERROR".into());
array.push_object(up_obj); array.push_object(up_obj);
} else { } else {
if graph_max_opt.is_some() { if graph_max_opt.is_some() || net_graph_is_dynamic {
if let Some(graph_ref) = array.get_by_name_mut("net_graph") { if let Some(graph_ref) = array.get_by_name_mut("net_graph") {
graph_ref.update_as_error("Net ERROR".to_owned()); graph_ref.update_as_error("Net ERROR".to_owned());
} }
@ -167,7 +172,7 @@ fn main() {
let netinfo_parts: Vec<&str> = netinfo_string.split_whitespace().collect(); let netinfo_parts: Vec<&str> = netinfo_string.split_whitespace().collect();
if is_empty { if is_empty {
if net_graph_max.is_some() { if net_graph_max.is_some() || net_graph_is_dynamic {
let mut graph_obj = let mut graph_obj =
SwaybarObject::from_string("net_graph".to_owned(), graph_string); SwaybarObject::from_string("net_graph".to_owned(), graph_string);
graph_obj.color = Some("#ffff88".into()); graph_obj.color = Some("#ffff88".into());
@ -205,7 +210,7 @@ fn main() {
array.push_object(up_object); array.push_object(up_object);
} }
} else { } else {
if net_graph_max.is_some() { if net_graph_max.is_some() || net_graph_is_dynamic {
if let Some(graph_obj) = array.get_by_name_mut("net_graph") { if let Some(graph_obj) = array.get_by_name_mut("net_graph") {
graph_obj.full_text = graph_string; graph_obj.full_text = graph_string;
} }

View file

@ -59,10 +59,12 @@ impl std::error::Error for Error {
pub struct NetInfo { pub struct NetInfo {
dev_name: String, dev_name: String,
graph: String, graph: String,
graph_history: [f64; 10],
down: u64, down: u64,
prev_down: u64, prev_down: u64,
up: u64, up: u64,
prev_up: u64, prev_up: u64,
first_iteration: bool,
} }
impl NetInfo { impl NetInfo {
@ -70,10 +72,12 @@ impl NetInfo {
Self { Self {
dev_name, dev_name,
graph: String::from(" "), graph: String::from(" "),
graph_history: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
down: 0, down: 0,
prev_down: 0, prev_down: 0,
up: 0, up: 0,
prev_up: 0, prev_up: 0,
first_iteration: true,
} }
} }
@ -98,8 +102,13 @@ impl NetInfo {
return Err(format!("NetInfo::update: Failed to parse /proc/net/dev, \"{}\" device line is too short", self.dev_name).into()); return Err(format!("NetInfo::update: Failed to parse /proc/net/dev, \"{}\" device line is too short", self.dev_name).into());
} }
self.down = entries[1].parse()?; if !self.first_iteration {
self.up = entries[9].parse()?; self.down = entries[1].parse()?;
self.up = entries[9].parse()?;
} else {
self.prev_down = entries[1].parse()?;
self.prev_up = entries[9].parse()?;
}
} else { } else {
return Err(format!( return Err(format!(
"NetInfo::update: Failed to parse /proc/net/dev, can't find net device \"{}\"", "NetInfo::update: Failed to parse /proc/net/dev, can't find net device \"{}\"",
@ -108,14 +117,26 @@ impl NetInfo {
.into()); .into());
} }
self.first_iteration = false;
Ok(()) Ok(())
} }
pub fn get_netstring(&mut self, graph_max: Option<f64>) -> Result<(String, String), Error> { pub fn get_netstring(&mut self, graph_max_opt: Option<f64>) -> Result<(String, String), Error> {
let down_diff: f64 = (self.down - self.prev_down) as f64; let down_diff: f64 = if self.down > self.prev_down {
self.prev_down = self.down; let value = (self.down - self.prev_down) as f64;
let up_diff: f64 = (self.up - self.prev_up) as f64; self.prev_down = self.down;
self.prev_up = self.up; value
} else {
0.0
};
let up_diff: f64 = if self.up > self.prev_up {
let value = (self.up - self.prev_up) as f64;
self.prev_up = self.up;
value
} else {
0.0
};
let mut output = String::new(); let mut output = String::new();
if down_diff > 1024.0 * 1024.0 { if down_diff > 1024.0 * 1024.0 {
@ -134,12 +155,13 @@ impl NetInfo {
write!(&mut output, "{:.0} B", up_diff)?; write!(&mut output, "{:.0} B", up_diff)?;
} }
if let Some(graph_max) = graph_max { let diff_max = if down_diff > up_diff {
let diff_max = if down_diff > up_diff { down_diff
down_diff } else {
} else { up_diff
up_diff };
};
if let Some(graph_max) = graph_max_opt {
let graph_value: u8 = if diff_max > graph_max { let graph_value: u8 = if diff_max > graph_max {
8 8
} else { } else {
@ -158,6 +180,36 @@ impl NetInfo {
7 => self.graph.push('▇'), 7 => self.graph.push('▇'),
_ => self.graph.push('█'), _ => self.graph.push('█'),
} }
} else {
self.graph_history.rotate_left(1);
self.graph_history[9] = diff_max;
let mut history_max: f64 = 0.0;
for value in &self.graph_history {
if history_max < *value {
history_max = *value;
}
}
self.graph.clear();
if history_max == 0.0 {
self.graph = String::from(" ");
} else {
for value in &self.graph_history {
match (8.0 * value / history_max).round() as u8 {
0 => self.graph.push(' '),
1 => self.graph.push('▁'),
2 => self.graph.push('▂'),
3 => self.graph.push('▃'),
4 => self.graph.push('▄'),
5 => self.graph.push('▅'),
6 => self.graph.push('▆'),
7 => self.graph.push('▇'),
_ => self.graph.push('█'),
}
}
}
assert_eq!(self.graph_history.len(), 10);
} }
Ok((output, self.graph.clone())) Ok((output, self.graph.clone()))