Print to stderr on invalid arg, use f64 for memory

This commit is contained in:
Stephen Seo 2022-07-10 17:02:54 +09:00
parent ad1f0cd871
commit 251125366c
2 changed files with 22 additions and 8 deletions

View 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();
}
}

View 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");
}