From 251125366cebe441193e59407861d20319bb5951 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sun, 10 Jul 2022 17:02:54 +0900 Subject: [PATCH] Print to stderr on invalid arg, use f64 for memory --- src/args.rs | 11 ++++++++++- src/proc.rs | 19 ++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/args.rs b/src/args.rs index 3248b00..248803f 100644 --- a/src/args.rs +++ b/src/args.rs @@ -5,8 +5,12 @@ use std::io::Write; pub fn get_args() -> HashMap { 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 { 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(); } } diff --git a/src/proc.rs b/src/proc.rs index e36ba49..b49df82 100644 --- a/src/proc.rs +++ b/src/proc.rs @@ -162,33 +162,38 @@ pub fn get_meminfo() -> Result { } } - 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"); }