Fix output of memory info

It displayed "GiB" where it should have displayed "MiB"
This commit is contained in:
Stephen Seo 2022-07-09 18:21:28 +09:00
parent 313bb30794
commit be4421630e

View file

@ -91,7 +91,7 @@ pub fn get_meminfo() -> io::Result<String> {
meminfo.read_to_string(&mut meminfo_string)?; meminfo.read_to_string(&mut meminfo_string)?;
} }
let mut is_total_giga = false; let mut is_total_mega = false;
let mut total: u32 = 0; let mut total: u32 = 0;
let mut available: u32 = 0; let mut available: u32 = 0;
for line in meminfo_string.lines() { for line in meminfo_string.lines() {
@ -115,31 +115,31 @@ pub fn get_meminfo() -> io::Result<String> {
} }
let mut used = total - available; let mut used = total - available;
let mut is_used_giga = false; let mut is_used_mega = false;
if total == 0 { if total == 0 {
Ok("0".into()) Ok("0".into())
} else { } else {
if total > 1024 { if total > 1024 {
total /= 1024; total /= 1024;
is_total_giga = true; is_total_mega = true;
} }
if used > 1024 { if used > 1024 {
used /= 1024; used /= 1024;
is_used_giga = true; is_used_mega = true;
} }
let mut output = format!("{} ", used); let mut output = format!("{} ", used);
if is_used_giga { if is_used_mega {
output.push_str("GiB / "); output.push_str("MiB / ");
} else { } else {
output.push_str("KiB / "); output.push_str("KiB / ");
} }
output.push_str(&format!("{} ", total)); output.push_str(&format!("{} ", total));
if is_total_giga { if is_total_mega {
output.push_str("GiB"); output.push_str("MiB");
} else { } else {
output.push_str("KiB"); output.push_str("KiB");
} }