2019-09-29 05:55:49 +00:00
|
|
|
use byte_unit::{Byte, ByteUnit};
|
|
|
|
use sysinfo::{RefreshKind, SystemExt};
|
|
|
|
|
2020-01-26 22:37:18 +00:00
|
|
|
use super::{Context, Module, RootModuleConfig, Shell};
|
2019-10-20 15:26:04 +00:00
|
|
|
|
|
|
|
use crate::configs::memory_usage::MemoryConfig;
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// TODO: Update when v1.0 printing refactor is implemented to only
|
|
|
|
// print escapes in a prompt context.
|
2020-01-26 22:37:18 +00:00
|
|
|
let percent_sign = match context.shell {
|
|
|
|
Shell::Zsh => "%%", // % is an escape in zsh, see PROMPT in `man zshmisc`
|
2019-10-20 15:26:04 +00:00
|
|
|
_ => "%",
|
|
|
|
};
|
2019-09-29 05:55:49 +00:00
|
|
|
|
2019-10-20 15:26:04 +00:00
|
|
|
if config.disabled {
|
2019-10-05 10:03:48 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-10-20 15:26:04 +00:00
|
|
|
module.set_style(config.style);
|
2019-11-13 01:57:46 +00:00
|
|
|
module.create_segment("symbol", &config.symbol);
|
2019-09-29 05:55:49 +00:00
|
|
|
|
2019-12-12 20:32:46 +00:00
|
|
|
let system = sysinfo::System::new_with_specifics(RefreshKind::new().with_memory());
|
2019-09-29 05:55:49 +00:00
|
|
|
|
|
|
|
let used_memory_kib = system.get_used_memory();
|
|
|
|
let total_memory_kib = system.get_total_memory();
|
|
|
|
|
|
|
|
let percent_mem_used = (used_memory_kib as f64 / total_memory_kib as f64) * 100.;
|
|
|
|
|
2019-10-20 15:26:04 +00:00
|
|
|
let threshold = config.threshold;
|
2019-09-29 05:55:49 +00:00
|
|
|
|
|
|
|
if percent_mem_used.round() < threshold as f64 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-10-20 15:26:04 +00:00
|
|
|
let show_percentage = config.show_percentage;
|
2019-09-29 05:55:49 +00:00
|
|
|
|
2019-11-13 01:57:46 +00:00
|
|
|
let ram = if show_percentage {
|
2019-10-20 15:26:04 +00:00
|
|
|
format!("{:.0}{}", percent_mem_used, percent_sign)
|
2019-09-29 05:55:49 +00:00
|
|
|
} else {
|
2019-10-20 15:26:04 +00:00
|
|
|
format!(
|
|
|
|
"{}/{}",
|
|
|
|
format_kib(used_memory_kib),
|
|
|
|
format_kib(total_memory_kib)
|
2019-09-29 05:55:49 +00:00
|
|
|
)
|
|
|
|
};
|
2019-11-13 01:57:46 +00:00
|
|
|
module.create_segment("ram", &config.ram.with_value(&ram));
|
2019-09-29 05:55:49 +00:00
|
|
|
|
2019-10-20 15:26:04 +00:00
|
|
|
// swap only shown if enabled and there is swap on the system
|
|
|
|
let total_swap_kib = system.get_total_swap();
|
|
|
|
if config.show_swap && total_swap_kib > 0 {
|
|
|
|
let used_swap_kib = system.get_used_swap();
|
|
|
|
let percent_swap_used = (used_swap_kib as f64 / total_swap_kib as f64) * 100.;
|
|
|
|
|
2019-11-13 01:57:46 +00:00
|
|
|
let swap = if show_percentage {
|
|
|
|
format!("{:.0}{}", percent_swap_used, percent_sign)
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"{}/{}",
|
|
|
|
format_kib(used_swap_kib),
|
|
|
|
format_kib(total_swap_kib)
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
module.create_segment("separator", &config.separator);
|
|
|
|
module.create_segment("swap", &config.swap.with_value(&swap));
|
2019-09-29 05:55:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|