Colorize net graph

This commit is contained in:
Stephen Seo 2023-05-21 14:23:35 +09:00
parent 31458ca38d
commit 4584dd018d
2 changed files with 177 additions and 49 deletions

View file

@ -49,8 +49,8 @@ fn main() {
let mut net_graph_is_dynamic: bool = false; let mut net_graph_is_dynamic: bool = false;
let mut net_graph_show_dynamic_max: bool = false; let mut net_graph_show_dynamic_max: bool = false;
let mut interval: Duration = Duration::from_secs(5); let mut interval: Duration = Duration::from_secs(5);
let mut net_graph_size: Option<usize> = None;
if args_result.map.contains_key("netdev") { 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 Some(size_str) = args_result.map.get("netgraph-size") {
if let Ok(size) = size_str.parse::<usize>() { if let Ok(size) = size_str.parse::<usize>() {
if size > 0 { if size > 0 {
@ -79,6 +79,10 @@ fn main() {
net_graph_size, net_graph_size,
)); ));
} }
if net_graph_size.is_none() {
net_graph_size = Some(10);
}
if args_result.map.contains_key("netdevwidth") { if args_result.map.contains_key("netdevwidth") {
let width_result: Result<u16, _> = args_result.map.get("netdevwidth").unwrap().parse(); let width_result: Result<u16, _> = args_result.map.get("netdevwidth").unwrap().parse();
if let Ok(width) = width_result { if let Ok(width) = width_result {
@ -207,7 +211,8 @@ fn main() {
array: &mut SwaybarArray| array: &mut SwaybarArray|
-> Result<(), proc::Error> { -> Result<(), proc::Error> {
net.update()?; net.update()?;
let (netinfo_string, graph_string, history_max) = net.get_netstring(net_graph_max)?; let (netinfo_string, graph_items, max_idx, history_max) =
net.get_netstring(net_graph_max)?;
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 {
@ -219,10 +224,31 @@ fn main() {
} }
if net_graph_max.is_some() || net_graph_is_dynamic { if net_graph_max.is_some() || net_graph_is_dynamic {
let mut graph_obj = for i in 0..net_graph_size.unwrap() {
SwaybarObject::from_string("net_graph".to_owned(), graph_string); let mut graph_obj = SwaybarObject::from_string(
graph_obj.color = Some("#ffff88".into()); "net_graph".to_owned() + &i.to_string(),
array.push_object(graph_obj); " ".to_owned(),
);
if i == 0 {
graph_obj.border_left = Some(1);
} else {
graph_obj.border_left = Some(0);
}
if i == net_graph_size.unwrap() - 1 {
graph_obj.border_right = Some(1);
graph_obj.separator_block_width = Some(12);
} else {
graph_obj.border_right = Some(0);
graph_obj.separator_block_width = Some(0);
}
graph_obj.border_top = Some(1);
graph_obj.border_bottom = Some(1);
graph_obj.color = Some("#ffff88".into());
graph_obj.separator = Some(false);
array.push_object(graph_obj);
}
} }
let mut width_string: Option<String> = None; let mut width_string: Option<String> = None;
@ -259,12 +285,36 @@ fn main() {
if net_graph_is_dynamic && net_graph_show_dynamic_max { if net_graph_is_dynamic && net_graph_show_dynamic_max {
if let Some(graph_obj) = array.get_by_name_mut("net_graph_dyn_max") { if let Some(graph_obj) = array.get_by_name_mut("net_graph_dyn_max") {
graph_obj.full_text = history_max; graph_obj.full_text = history_max;
if (net_graph_max.is_some() || net_graph_is_dynamic) && !graph_items.is_empty()
{
match graph_items[max_idx].get_value_type() {
proc::GraphItemType::DOWNLOAD => {
graph_obj.color = Some("#ff8888ff".into())
}
proc::GraphItemType::UPLOAD => {
graph_obj.color = Some("#88ff88ff".into())
}
proc::GraphItemType::BOTH => graph_obj.color = Some("#ffff88ff".into()),
}
}
} }
} }
if net_graph_max.is_some() || net_graph_is_dynamic { if net_graph_max.is_some() || net_graph_is_dynamic {
if let Some(graph_obj) = array.get_by_name_mut("net_graph") { for i in 0..net_graph_size.unwrap() {
graph_obj.full_text = graph_string; let name = "net_graph".to_owned() + &i.to_string();
if let Some(graph_obj) = array.get_by_name_mut(&name) {
match graph_items[i].get_value_type() {
proc::GraphItemType::DOWNLOAD => {
graph_obj.color = Some("#ff8888ff".into())
}
proc::GraphItemType::UPLOAD => {
graph_obj.color = Some("#88ff88ff".into())
}
proc::GraphItemType::BOTH => graph_obj.color = Some("#ffff88ff".into()),
}
graph_obj.full_text = graph_items[i].get_value().into();
}
} }
} }

View file

@ -57,10 +57,41 @@ impl std::error::Error for Error {
} }
} }
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum GraphItemType {
DOWNLOAD,
UPLOAD,
BOTH,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct GraphItem {
value: char,
num_value: f64,
value_type: GraphItemType,
}
impl GraphItem {
pub fn get_value(&self) -> char {
self.value
}
pub fn set_value(&mut self, c: char) {
self.value = c;
}
pub fn get_num_value(&self) -> f64 {
self.num_value
}
pub fn get_value_type(&self) -> GraphItemType {
self.value_type
}
}
pub struct NetInfo { pub struct NetInfo {
dev_name: String, dev_name: String,
graph: String, graph: Vec<GraphItem>,
graph_history: Vec<f64>,
down: u64, down: u64,
prev_down: u64, prev_down: u64,
up: u64, up: u64,
@ -72,8 +103,11 @@ impl NetInfo {
pub fn new(dev_name: String, graph_size_opt: Option<usize>) -> Self { pub fn new(dev_name: String, graph_size_opt: Option<usize>) -> Self {
let mut s = Self { let mut s = Self {
dev_name, dev_name,
graph: String::from(" "), graph: vec![GraphItem {
graph_history: Vec::new(), value: ' ',
num_value: 0.0,
value_type: GraphItemType::BOTH,
}],
down: 0, down: 0,
prev_down: 0, prev_down: 0,
up: 0, up: 0,
@ -83,7 +117,6 @@ impl NetInfo {
if let Some(graph_size) = graph_size_opt { if let Some(graph_size) = graph_size_opt {
if graph_size > 0 { if graph_size > 0 {
s.graph_history.resize(graph_size, 0.0);
s.graph = s.graph.repeat(graph_size); s.graph = s.graph.repeat(graph_size);
} else { } else {
let mut stderr_handle = std::io::stderr().lock(); let mut stderr_handle = std::io::stderr().lock();
@ -93,11 +126,9 @@ impl NetInfo {
.as_bytes(), .as_bytes(),
) )
.ok(); .ok();
s.graph_history.resize(10, 0.0);
s.graph = s.graph.repeat(10); s.graph = s.graph.repeat(10);
} }
} else { } else {
s.graph_history.resize(10, 0.0);
s.graph = s.graph.repeat(10); s.graph = s.graph.repeat(10);
} }
@ -145,11 +176,11 @@ impl NetInfo {
Ok(()) Ok(())
} }
// Returns netinfo down/up, graph, and history_max (if dynamic is enabled) // Returns netinfo down/up, graph, max idx, and history_max (if dynamic is enabled)
pub fn get_netstring( pub fn get_netstring(
&mut self, &mut self,
graph_max_opt: Option<f64>, graph_max_opt: Option<f64>,
) -> Result<(String, String, String), Error> { ) -> Result<(String, Vec<GraphItem>, usize, String), Error> {
let down_diff: f64 = if self.down > self.prev_down { let down_diff: f64 = if self.down > self.prev_down {
let value = (self.down - self.prev_down) as f64; let value = (self.down - self.prev_down) as f64;
self.prev_down = self.down; self.prev_down = self.down;
@ -182,13 +213,19 @@ impl NetInfo {
write!(&mut output, "{:.0} B", up_diff)?; write!(&mut output, "{:.0} B", up_diff)?;
} }
let mut graph_type = GraphItemType::BOTH;
let diff_max = if down_diff > up_diff { let diff_max = if down_diff > up_diff {
graph_type = GraphItemType::DOWNLOAD;
down_diff down_diff
} else { } else {
if down_diff < up_diff {
graph_type = GraphItemType::UPLOAD;
}
up_diff up_diff
}; };
let mut diff_max_string = String::new(); let mut diff_max_string = String::new();
let mut history_max_idx = 0;
if let Some(graph_max) = graph_max_opt { 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 {
@ -199,27 +236,73 @@ impl NetInfo {
self.graph.remove(0); self.graph.remove(0);
match graph_value { match graph_value {
0 => self.graph.push(' '), 0 => self.graph.push(GraphItem {
1 => self.graph.push('▁'), value: ' ',
2 => self.graph.push('▂'), num_value: 0.0,
3 => self.graph.push('▃'), value_type: graph_type,
4 => self.graph.push('▄'), }),
5 => self.graph.push('▅'), 1 => self.graph.push(GraphItem {
6 => self.graph.push('▆'), value: '',
7 => self.graph.push('▇'), num_value: 0.0,
_ => self.graph.push('█'), value_type: graph_type,
}),
2 => self.graph.push(GraphItem {
value: '',
num_value: 0.0,
value_type: graph_type,
}),
3 => self.graph.push(GraphItem {
value: '',
num_value: 0.0,
value_type: graph_type,
}),
4 => self.graph.push(GraphItem {
value: '',
num_value: 0.0,
value_type: graph_type,
}),
5 => self.graph.push(GraphItem {
value: '',
num_value: 0.0,
value_type: graph_type,
}),
6 => self.graph.push(GraphItem {
value: '',
num_value: 0.0,
value_type: graph_type,
}),
7 => self.graph.push(GraphItem {
value: '',
num_value: 0.0,
value_type: graph_type,
}),
_ => self.graph.push(GraphItem {
value: '',
num_value: 0.0,
value_type: graph_type,
}),
} }
} else { } else {
self.graph_history.rotate_left(1); self.graph.rotate_left(1);
{ {
let end_idx = self.graph_history.len() - 1; let end_idx = self.graph.len() - 1;
self.graph_history[end_idx] = diff_max; self.graph[end_idx] = GraphItem {
value: ' ',
num_value: diff_max,
value_type: graph_type,
};
} }
let mut history_max: f64 = 0.0; let mut history_max: f64 = 0.0;
for value in &self.graph_history { for (idx, value) in self
if history_max < *value { .graph
history_max = *value; .iter()
.map(|item| item.get_num_value())
.enumerate()
{
if history_max < value {
history_max = value;
history_max_idx = idx;
} }
} }
@ -235,27 +318,22 @@ impl NetInfo {
write!(&mut diff_max_string, "{:.0} B", history_max)?; write!(&mut diff_max_string, "{:.0} B", history_max)?;
} }
self.graph.clear(); for item in self.graph.iter_mut() {
if history_max == 0.0 { match (8.0 * item.get_num_value() / history_max).round() as u8 {
self.graph = String::from(" ").repeat(self.graph_history.len()); 0 => item.set_value(' '),
} else { 1 => item.set_value('▁'),
for value in &self.graph_history { 2 => item.set_value('▂'),
match (8.0 * value / history_max).round() as u8 { 3 => item.set_value('▃'),
0 => self.graph.push(' '), 4 => item.set_value('▄'),
1 => self.graph.push('▁'), 5 => item.set_value('▅'),
2 => self.graph.push('▂'), 6 => item.set_value('▆'),
3 => self.graph.push('▃'), 7 => item.set_value('▇'),
4 => self.graph.push('▄'), _ => item.set_value('█'),
5 => self.graph.push('▅'),
6 => self.graph.push('▆'),
7 => self.graph.push('▇'),
_ => self.graph.push('█'),
}
} }
} }
} }
Ok((output, self.graph.clone(), diff_max_string)) Ok((output, self.graph.clone(), history_max_idx, diff_max_string))
} }
} }