Colorize net graph
This commit is contained in:
parent
31458ca38d
commit
4584dd018d
2 changed files with 177 additions and 49 deletions
62
src/main.rs
62
src/main.rs
|
@ -49,8 +49,8 @@ fn main() {
|
|||
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") {
|
||||
let mut net_graph_size: Option<usize> = None;
|
||||
if args_result.map.contains_key("netdev") {
|
||||
if let Some(size_str) = args_result.map.get("netgraph-size") {
|
||||
if let Ok(size) = size_str.parse::<usize>() {
|
||||
if size > 0 {
|
||||
|
@ -79,6 +79,10 @@ fn main() {
|
|||
net_graph_size,
|
||||
));
|
||||
}
|
||||
if net_graph_size.is_none() {
|
||||
net_graph_size = Some(10);
|
||||
}
|
||||
|
||||
if args_result.map.contains_key("netdevwidth") {
|
||||
let width_result: Result<u16, _> = args_result.map.get("netdevwidth").unwrap().parse();
|
||||
if let Ok(width) = width_result {
|
||||
|
@ -207,7 +211,8 @@ fn main() {
|
|||
array: &mut SwaybarArray|
|
||||
-> Result<(), proc::Error> {
|
||||
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();
|
||||
|
||||
if is_empty {
|
||||
|
@ -219,11 +224,32 @@ fn main() {
|
|||
}
|
||||
|
||||
if net_graph_max.is_some() || net_graph_is_dynamic {
|
||||
let mut graph_obj =
|
||||
SwaybarObject::from_string("net_graph".to_owned(), graph_string);
|
||||
for i in 0..net_graph_size.unwrap() {
|
||||
let mut graph_obj = SwaybarObject::from_string(
|
||||
"net_graph".to_owned() + &i.to_string(),
|
||||
" ".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;
|
||||
if let Some(width) = net_width {
|
||||
|
@ -259,12 +285,36 @@ fn main() {
|
|||
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) && !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 let Some(graph_obj) = array.get_by_name_mut("net_graph") {
|
||||
graph_obj.full_text = graph_string;
|
||||
for i in 0..net_graph_size.unwrap() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
160
src/proc.rs
160
src/proc.rs
|
@ -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 {
|
||||
dev_name: String,
|
||||
graph: String,
|
||||
graph_history: Vec<f64>,
|
||||
graph: Vec<GraphItem>,
|
||||
down: u64,
|
||||
prev_down: u64,
|
||||
up: u64,
|
||||
|
@ -72,8 +103,11 @@ impl NetInfo {
|
|||
pub fn new(dev_name: String, graph_size_opt: Option<usize>) -> Self {
|
||||
let mut s = Self {
|
||||
dev_name,
|
||||
graph: String::from(" "),
|
||||
graph_history: Vec::new(),
|
||||
graph: vec![GraphItem {
|
||||
value: ' ',
|
||||
num_value: 0.0,
|
||||
value_type: GraphItemType::BOTH,
|
||||
}],
|
||||
down: 0,
|
||||
prev_down: 0,
|
||||
up: 0,
|
||||
|
@ -83,7 +117,6 @@ impl NetInfo {
|
|||
|
||||
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();
|
||||
|
@ -93,11 +126,9 @@ impl NetInfo {
|
|||
.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);
|
||||
}
|
||||
|
||||
|
@ -145,11 +176,11 @@ impl NetInfo {
|
|||
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(
|
||||
&mut self,
|
||||
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 value = (self.down - self.prev_down) as f64;
|
||||
self.prev_down = self.down;
|
||||
|
@ -182,13 +213,19 @@ impl NetInfo {
|
|||
write!(&mut output, "{:.0} B", up_diff)?;
|
||||
}
|
||||
|
||||
let mut graph_type = GraphItemType::BOTH;
|
||||
let diff_max = if down_diff > up_diff {
|
||||
graph_type = GraphItemType::DOWNLOAD;
|
||||
down_diff
|
||||
} else {
|
||||
if down_diff < up_diff {
|
||||
graph_type = GraphItemType::UPLOAD;
|
||||
}
|
||||
up_diff
|
||||
};
|
||||
|
||||
let mut diff_max_string = String::new();
|
||||
let mut history_max_idx = 0;
|
||||
|
||||
if let Some(graph_max) = graph_max_opt {
|
||||
let graph_value: u8 = if diff_max > graph_max {
|
||||
|
@ -199,27 +236,73 @@ impl NetInfo {
|
|||
|
||||
self.graph.remove(0);
|
||||
match graph_value {
|
||||
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('█'),
|
||||
0 => self.graph.push(GraphItem {
|
||||
value: ' ',
|
||||
num_value: 0.0,
|
||||
value_type: graph_type,
|
||||
}),
|
||||
1 => self.graph.push(GraphItem {
|
||||
value: '▁',
|
||||
num_value: 0.0,
|
||||
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 {
|
||||
self.graph_history.rotate_left(1);
|
||||
self.graph.rotate_left(1);
|
||||
{
|
||||
let end_idx = self.graph_history.len() - 1;
|
||||
self.graph_history[end_idx] = diff_max;
|
||||
let end_idx = self.graph.len() - 1;
|
||||
self.graph[end_idx] = GraphItem {
|
||||
value: ' ',
|
||||
num_value: diff_max,
|
||||
value_type: graph_type,
|
||||
};
|
||||
}
|
||||
|
||||
let mut history_max: f64 = 0.0;
|
||||
for value in &self.graph_history {
|
||||
if history_max < *value {
|
||||
history_max = *value;
|
||||
for (idx, value) in self
|
||||
.graph
|
||||
.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)?;
|
||||
}
|
||||
|
||||
self.graph.clear();
|
||||
if history_max == 0.0 {
|
||||
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 {
|
||||
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('█'),
|
||||
}
|
||||
for item in self.graph.iter_mut() {
|
||||
match (8.0 * item.get_num_value() / history_max).round() as u8 {
|
||||
0 => item.set_value(' '),
|
||||
1 => item.set_value('▁'),
|
||||
2 => item.set_value('▂'),
|
||||
3 => item.set_value('▃'),
|
||||
4 => item.set_value('▄'),
|
||||
5 => item.set_value('▅'),
|
||||
6 => item.set_value('▆'),
|
||||
7 => item.set_value('▇'),
|
||||
_ => item.set_value('█'),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok((output, self.graph.clone(), diff_max_string))
|
||||
Ok((output, self.graph.clone(), history_max_idx, diff_max_string))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue