2019-09-29 05:55:49 +00:00
|
|
|
use byte_unit::{Byte, ByteUnit};
|
|
|
|
|
2022-03-26 09:42:19 +00:00
|
|
|
use super::{Context, Module, ModuleConfig};
|
2019-10-20 15:26:04 +00:00
|
|
|
|
|
|
|
use crate::configs::memory_usage::MemoryConfig;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::StringFormatter;
|
2019-10-20 15:26:04 +00:00
|
|
|
|
|
|
|
fn format_kib(n_kib: u64) -> String {
|
|
|
|
let byte = Byte::from_unit(n_kib as f64, ByteUnit::KiB).unwrap_or_else(|_| Byte::from_bytes(0));
|
|
|
|
let mut display_bytes = byte.get_appropriate_unit(true).format(0);
|
|
|
|
display_bytes.retain(|c| c != ' ');
|
|
|
|
display_bytes
|
|
|
|
}
|
|
|
|
|
2021-11-01 21:19:27 +00:00
|
|
|
fn format_pct(pct_number: f64) -> String {
|
|
|
|
format!("{:.0}%", pct_number)
|
2020-07-07 22:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn format_usage_total(usage: u64, total: u64) -> String {
|
|
|
|
format!("{}/{}", format_kib(usage), format_kib(total))
|
|
|
|
}
|
|
|
|
|
2019-09-29 05:55:49 +00:00
|
|
|
/// Creates a module with system memory usage information
|
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|
|
|
let mut module = context.new_module("memory_usage");
|
2019-10-20 15:26:04 +00:00
|
|
|
let config = MemoryConfig::try_load(module.config);
|
|
|
|
|
2020-09-26 07:35:41 +00:00
|
|
|
// As we default to disabled=true, we have to check here after loading our config module,
|
|
|
|
// before it was only checking against whatever is in the config starship.toml
|
2019-10-20 15:26:04 +00:00
|
|
|
if config.disabled {
|
2019-10-05 10:03:48 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-11-24 02:17:15 +00:00
|
|
|
let system = match sys_info::mem_info() {
|
|
|
|
Ok(info) => info,
|
|
|
|
Err(err) => {
|
|
|
|
log::warn!("Unable to access memory usage information:\n{}", err);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// avail includes reclaimable memory, but isn't supported on all platforms
|
|
|
|
let avail_memory_kib = match system.avail {
|
|
|
|
0 => system.free,
|
|
|
|
_ => system.avail,
|
|
|
|
};
|
2021-05-04 15:28:44 +00:00
|
|
|
let used_memory_kib = system.total.saturating_sub(avail_memory_kib);
|
2020-11-24 02:17:15 +00:00
|
|
|
let total_memory_kib = system.total;
|
2020-07-07 22:45:32 +00:00
|
|
|
let ram_used = (used_memory_kib as f64 / total_memory_kib as f64) * 100.;
|
2021-11-01 21:19:27 +00:00
|
|
|
let ram_pct = format_pct(ram_used);
|
2019-09-29 05:55:49 +00:00
|
|
|
|
2019-10-20 15:26:04 +00:00
|
|
|
let threshold = config.threshold;
|
2020-07-07 22:45:32 +00:00
|
|
|
if ram_used.round() < threshold as f64 {
|
2019-09-29 05:55:49 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let ram = format_usage_total(used_memory_kib, total_memory_kib);
|
2020-11-24 02:17:15 +00:00
|
|
|
let total_swap_kib = system.swap_total;
|
2021-05-04 15:28:44 +00:00
|
|
|
let used_swap_kib = system.swap_total.saturating_sub(system.swap_free);
|
2020-07-07 22:45:32 +00:00
|
|
|
let percent_swap_used = (used_swap_kib as f64 / total_swap_kib as f64) * 100.;
|
2021-11-01 21:19:27 +00:00
|
|
|
let swap_pct = format_pct(percent_swap_used);
|
2020-07-07 22:45:32 +00:00
|
|
|
let swap = format_usage_total(used_swap_kib, total_swap_kib);
|
|
|
|
|
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_meta(|var, _| match var {
|
|
|
|
"symbol" => Some(config.symbol),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
|
|
|
"ram" => Some(Ok(&ram)),
|
|
|
|
"ram_pct" => Some(Ok(&ram_pct)),
|
|
|
|
// swap only shown if there is swap on the system
|
|
|
|
"swap" if total_swap_kib > 0 => Some(Ok(&swap)),
|
|
|
|
"swap_pct" if total_swap_kib > 0 => Some(Ok(&swap_pct)),
|
|
|
|
_ => None,
|
|
|
|
})
|
2021-11-01 21:18:45 +00:00
|
|
|
.parse(None, Some(context))
|
2020-07-07 22:45:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `memory_usage`:\n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
2019-09-29 05:55:49 +00:00
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|