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("--netgraph_size=") {
+ let (_, back) = arg.split_at(16);
+ map.insert("netgraph-size".into(), back.into());
} else if arg.starts_with("--interval-sec=") {
let (_, back) = arg.split_at(15);
map.insert("interval-sec".into(), back.into());
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_size=<size>\t\t\t\tSet the number of characters displayed in the net-graph (size of graph)\n")
+ .ok();
stderr_handle
.write_all(b" --netgraph_dyn_display\t\t\t\tEnable showing the current maximum value in the graph\n")
.ok();
let mut net_graph_show_dynamic_max: bool = false;
let mut interval: Duration = Duration::from_secs(5);
if args_result.map.contains_key("netdev") {
+ let mut net_graph_size: Option<usize> = None;
+ if let Some(size_str) = args_result.map.get("netgraph-size") {
+ if let Ok(size) = size_str.parse::<usize>() {
+ if size > 0 {
+ net_graph_size = Some(size);
+ } else {
+ let mut stderr_handle = io::stderr().lock();
+ stderr_handle
+ .write_all(
+ "WARNING: Invalid value passed to --netgraph_size=..., ignoring...\n"
+ .as_bytes(),
+ )
+ .ok();
+ }
+ } else {
+ let mut stderr_handle = io::stderr().lock();
+ stderr_handle
+ .write_all(
+ "WARNING: Invalid value passed to --netgraph_size=..., ignoring...\n"
+ .as_bytes(),
+ )
+ .ok();
+ }
+ }
net_obj = Some(proc::NetInfo::new(
args_result.map.get("netdev").unwrap().to_owned(),
+ net_graph_size,
));
}
if args_result.map.contains_key("netdevwidth") {
-use std::fmt::Write;
+use std::fmt::Write as FMTWrite;
use std::fs::File;
use std::io::prelude::*;
+use std::io::Write as IOWrite;
#[derive(Debug)]
pub enum Error {
pub struct NetInfo {
dev_name: String,
graph: String,
- graph_history: [f64; 10],
+ graph_history: Vec<f64>,
down: u64,
prev_down: u64,
up: u64,
}
impl NetInfo {
- pub fn new(dev_name: String) -> Self {
- Self {
+ pub fn new(dev_name: String, graph_size_opt: Option<usize>) -> Self {
+ let mut s = Self {
dev_name,
- 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],
+ graph: String::from(" "),
+ graph_history: Vec::new(),
down: 0,
prev_down: 0,
up: 0,
prev_up: 0,
first_iteration: true,
+ };
+
+ if let Some(graph_size) = graph_size_opt {
+ if graph_size > 0 {
+ s.graph_history.resize(graph_size, 0.0);
+ s.graph = s.graph.repeat(graph_size);
+ } else {
+ let mut stderr_handle = std::io::stderr().lock();
+ stderr_handle
+ .write_all(
+ "WARNING: Invalid graph_size value passed to NetInfo, ignoring...\n"
+ .as_bytes(),
+ )
+ .ok();
+ s.graph_history.resize(10, 0.0);
+ s.graph = s.graph.repeat(10);
+ }
+ } else {
+ s.graph_history.resize(10, 0.0);
+ s.graph = s.graph.repeat(10);
}
+
+ s
}
pub fn update(&mut self) -> Result<(), Error> {
}
} else {
self.graph_history.rotate_left(1);
- self.graph_history[9] = diff_max;
+ {
+ let end_idx = self.graph_history.len() - 1;
+ self.graph_history[end_idx] = diff_max;
+ }
let mut history_max: f64 = 0.0;
for value in &self.graph_history {
self.graph.clear();
if history_max == 0.0 {
- self.graph = String::from(" ");
+ self.graph = String::from(" ").repeat(self.graph_history.len());
} else {
for value in &self.graph_history {
match (8.0 * value / history_max).round() as u8 {
}
}
}
- assert_eq!(self.graph_history.len(), 10);
}
Ok((output, self.graph.clone(), diff_max_string))