let mut net_obj: Option<proc::NetInfo> = None;
let mut net_width: Option<u16> = Some(10);
let mut net_graph_max: Option<f64> = None;
+ let mut net_graph_is_dynamic: bool = false;
let mut interval: Duration = Duration::from_secs(5);
if args_result.map.contains_key("netdev") {
net_obj = Some(proc::NetInfo::new(
}
}
if args_result.map.contains_key("netgraph") {
- let graph_max_result: Result<f64, _> = args_result.map.get("netgraph").unwrap().parse();
- if let Ok(graph_max) = graph_max_result {
- net_graph_max = Some(graph_max);
+ if args_result.map.get("netgraph").as_ref().unwrap() == &"dynamic" {
+ net_graph_is_dynamic = true;
} else {
- let mut stderr_handle = io::stderr().lock();
- stderr_handle
- .write_all(
- format!(
- "WARNING: Invalid value passed to --netgraph_max_bytes=..., ignoring...\n"
+ let graph_max_result: Result<f64, _> = args_result.map.get("netgraph").unwrap().parse();
+ if let Ok(graph_max) = graph_max_result {
+ net_graph_max = Some(graph_max);
+ } else {
+ 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") {
let mut array = SwaybarArray::new();
let set_net_error = |is_empty: bool, array: &mut SwaybarArray, graph_max_opt: &Option<f64>| {
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(
"net_graph".to_owned(),
"net ERROR".into(),
let up_obj = SwaybarObject::from_error_string("net_up".to_owned(), "Net ERROR".into());
array.push_object(up_obj);
} 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") {
graph_ref.update_as_error("Net ERROR".to_owned());
}
let netinfo_parts: Vec<&str> = netinfo_string.split_whitespace().collect();
if is_empty {
- if net_graph_max.is_some() {
+ if net_graph_max.is_some() || net_graph_is_dynamic {
let mut graph_obj =
SwaybarObject::from_string("net_graph".to_owned(), graph_string);
graph_obj.color = Some("#ffff88".into());
array.push_object(up_object);
}
} 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") {
graph_obj.full_text = graph_string;
}
pub struct NetInfo {
dev_name: String,
graph: String,
+ graph_history: [f64; 10],
down: u64,
prev_down: u64,
up: u64,
prev_up: u64,
+ first_iteration: bool,
}
impl NetInfo {
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],
down: 0,
prev_down: 0,
up: 0,
prev_up: 0,
+ first_iteration: true,
}
}
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()?;
- self.up = entries[9].parse()?;
+ if !self.first_iteration {
+ self.down = entries[1].parse()?;
+ self.up = entries[9].parse()?;
+ } else {
+ self.prev_down = entries[1].parse()?;
+ self.prev_up = entries[9].parse()?;
+ }
} else {
return Err(format!(
"NetInfo::update: Failed to parse /proc/net/dev, can't find net device \"{}\"",
.into());
}
+ self.first_iteration = false;
+
Ok(())
}
- pub fn get_netstring(&mut self, graph_max: Option<f64>) -> Result<(String, String), Error> {
- let down_diff: f64 = (self.down - self.prev_down) as f64;
- self.prev_down = self.down;
- let up_diff: f64 = (self.up - self.prev_up) as f64;
- self.prev_up = self.up;
+ pub fn get_netstring(&mut self, graph_max_opt: Option<f64>) -> Result<(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;
+ 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();
if down_diff > 1024.0 * 1024.0 {
write!(&mut output, "{:.0} B", up_diff)?;
}
- if let Some(graph_max) = graph_max {
- let diff_max = if down_diff > up_diff {
- down_diff
- } else {
- up_diff
- };
+ let diff_max = if down_diff > up_diff {
+ down_diff
+ } else {
+ up_diff
+ };
+
+ if let Some(graph_max) = graph_max_opt {
let graph_value: u8 = if diff_max > graph_max {
8
} else {
7 => 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()))