Print to stderr on invalid arg, use f64 for memory
This commit is contained in:
parent
ad1f0cd871
commit
251125366c
2 changed files with 22 additions and 8 deletions
11
src/args.rs
11
src/args.rs
|
@ -5,8 +5,12 @@ use std::io::Write;
|
||||||
pub fn get_args() -> HashMap<String, String> {
|
pub fn get_args() -> HashMap<String, String> {
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
|
|
||||||
|
let mut first = true;
|
||||||
for arg in std::env::args() {
|
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);
|
let (_, back) = arg.split_at(9);
|
||||||
map.insert("netdev".into(), back.into());
|
map.insert("netdev".into(), back.into());
|
||||||
} else if arg.starts_with("--interval-sec=") {
|
} 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());
|
map.insert("interval-sec".into(), back.into());
|
||||||
} else if arg.starts_with("--help") || arg.starts_with("-h") {
|
} else if arg.starts_with("--help") || arg.starts_with("-h") {
|
||||||
map.insert("help".into(), "".into());
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
19
src/proc.rs
19
src/proc.rs
|
@ -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;
|
let mut is_used_mega = false;
|
||||||
|
|
||||||
if total == 0 {
|
if total == 0 {
|
||||||
Ok("0".into())
|
Ok("0".into())
|
||||||
} else {
|
} else {
|
||||||
if total > 1024 {
|
let mut total = total as f64;
|
||||||
total /= 1024;
|
|
||||||
|
if total > 1024.0 {
|
||||||
|
total /= 1024.0;
|
||||||
is_total_mega = true;
|
is_total_mega = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if used > 1024 {
|
if used > 1024.0 {
|
||||||
used /= 1024;
|
used /= 1024.0;
|
||||||
is_used_mega = true;
|
is_used_mega = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut output = format!("{} ", used);
|
let mut output: String;
|
||||||
if is_used_mega {
|
if is_used_mega {
|
||||||
|
output = format!("{:.2} ", used);
|
||||||
output.push_str("MiB / ");
|
output.push_str("MiB / ");
|
||||||
} else {
|
} else {
|
||||||
|
output = format!("{:.0} ", used);
|
||||||
output.push_str("KiB / ");
|
output.push_str("KiB / ");
|
||||||
}
|
}
|
||||||
|
|
||||||
write!(&mut output, "{} ", total)?;
|
|
||||||
if is_total_mega {
|
if is_total_mega {
|
||||||
|
write!(&mut output, "{:.2} ", total)?;
|
||||||
output.push_str("MiB");
|
output.push_str("MiB");
|
||||||
} else {
|
} else {
|
||||||
|
write!(&mut output, "{:.0} ", total)?;
|
||||||
output.push_str("KiB");
|
output.push_str("KiB");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue