]> git.seodisparate.com - swaybar_info/commitdiff
Print to stderr on invalid arg, use f64 for memory
authorStephen Seo <seo.disparate@gmail.com>
Sun, 10 Jul 2022 08:02:54 +0000 (17:02 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Sun, 10 Jul 2022 08:02:54 +0000 (17:02 +0900)
src/args.rs
src/proc.rs

index 3248b0031264e8a593e1633db0fbf9b362aa946a..248803fb7d0e7612bde2752a843cd708030071cf 100644 (file)
@@ -5,8 +5,12 @@ use std::io::Write;
 pub fn get_args() -> HashMap<String, String> {
     let mut map = HashMap::new();
 
+    let mut first = true;
     for arg in std::env::args() {
-        if arg.starts_with("--netdev=") {
+        if first {
+            first = false;
+            continue;
+        } else if arg.starts_with("--netdev=") {
             let (_, back) = arg.split_at(9);
             map.insert("netdev".into(), back.into());
         } else if arg.starts_with("--interval-sec=") {
@@ -14,6 +18,11 @@ pub fn get_args() -> HashMap<String, String> {
             map.insert("interval-sec".into(), back.into());
         } else if arg.starts_with("--help") || arg.starts_with("-h") {
             map.insert("help".into(), "".into());
+        } else {
+            let mut stderr_handle = io::stderr().lock();
+            stderr_handle
+                .write_all(format!("WARNING: Got invalid arg \"{}\"!\n", arg).as_bytes())
+                .ok();
         }
     }
 
index e36ba49bb91472bf441deba9dc3929fae830a73b..b49df82ff402241f2de9f79347041b8832581025 100644 (file)
@@ -162,33 +162,38 @@ pub fn get_meminfo() -> Result<String, Error> {
         }
     }
 
-    let mut used = total - available;
+    let mut used: f64 = (total - available) as f64;
     let mut is_used_mega = false;
 
     if total == 0 {
         Ok("0".into())
     } else {
-        if total > 1024 {
-            total /= 1024;
+        let mut total = total as f64;
+
+        if total > 1024.0 {
+            total /= 1024.0;
             is_total_mega = true;
         }
 
-        if used > 1024 {
-            used /= 1024;
+        if used > 1024.0 {
+            used /= 1024.0;
             is_used_mega = true;
         }
 
-        let mut output = format!("{} ", used);
+        let mut output: String;
         if is_used_mega {
+            output = format!("{:.2} ", used);
             output.push_str("MiB / ");
         } else {
+            output = format!("{:.0} ", used);
             output.push_str("KiB / ");
         }
 
-        write!(&mut output, "{} ", total)?;
         if is_total_mega {
+            write!(&mut output, "{:.2} ", total)?;
             output.push_str("MiB");
         } else {
+            write!(&mut output, "{:.0} ", total)?;
             output.push_str("KiB");
         }