]> git.seodisparate.com - swaybar_info/commitdiff
Impl setting the history-graph size
authorStephen Seo <seo.disparate@gmail.com>
Tue, 13 Dec 2022 11:03:01 +0000 (20:03 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Tue, 13 Dec 2022 11:03:01 +0000 (20:03 +0900)
src/args.rs
src/main.rs
src/proc.rs

index c8323fe6576ae2be258b76511423bd486cd0c8f3..c4603227b689ff2d10bfb549dcffeb510e28369f 100644 (file)
@@ -27,6 +27,9 @@ pub fn get_args() -> ArgsResult {
             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());
@@ -69,6 +72,9 @@ pub fn print_usage() {
     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();
index 2423f0c1247a008ddf73ac7fd9bebf484ddcad81..7f4b346ca2f678f91691cf8b919d8234404d8add 100644 (file)
@@ -50,8 +50,33 @@ fn main() {
     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") {
index 8a4b002382271ba1db04792f37aefb5e4e03bfb3..42c7d118a3abcea2c305794fc198a5c625fdbd6c 100644 (file)
@@ -1,6 +1,7 @@
-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 {
@@ -59,7 +60,7 @@ impl std::error::Error for Error {
 pub struct NetInfo {
     dev_name: String,
     graph: String,
-    graph_history: [f64; 10],
+    graph_history: Vec<f64>,
     down: u64,
     prev_down: u64,
     up: u64,
@@ -68,17 +69,39 @@ pub struct NetInfo {
 }
 
 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> {
@@ -188,7 +211,10 @@ impl NetInfo {
             }
         } 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 {
@@ -211,7 +237,7 @@ impl NetInfo {
 
             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 {
@@ -227,7 +253,6 @@ impl NetInfo {
                     }
                 }
             }
-            assert_eq!(self.graph_history.len(), 10);
         }
 
         Ok((output, self.graph.clone(), diff_max_string))