1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-12 06:07:56 +00:00
starship/src/modules/memory_usage.rs
Jan Katins c93bd7b705
fix: actually disable per default disabled modules (#1677)
The default `disabled: true` is actually only available within the module (when the config struct is used and not the user toml) but not all (the hg_branch) modules checked it there again.

Document this in all places and add the check (+ test) to the hg_branch module.
2020-09-26 09:35:41 +02:00

91 lines
3.2 KiB
Rust

use byte_unit::{Byte, ByteUnit};
use sysinfo::{RefreshKind, SystemExt};
use super::{Context, Module, RootModuleConfig, Shell};
use crate::configs::memory_usage::MemoryConfig;
use crate::formatter::StringFormatter;
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
}
fn format_pct(pct_number: f64, pct_sign: &str) -> String {
format!("{:.0}{}", pct_number, pct_sign)
}
fn format_usage_total(usage: u64, total: u64) -> String {
format!("{}/{}", format_kib(usage), format_kib(total))
}
/// 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");
let config = MemoryConfig::try_load(module.config);
// TODO: Update when v1.0 printing refactor is implemented to only
// print escapes in a prompt context.
let pct_sign = match context.shell {
Shell::Zsh => "%%", // % is an escape in zsh, see PROMPT in `man zshmisc`
_ => "%",
};
// 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
if config.disabled {
return None;
}
let system = sysinfo::System::new_with_specifics(RefreshKind::new().with_memory());
let used_memory_kib = system.get_used_memory();
let total_memory_kib = system.get_total_memory();
let ram_used = (used_memory_kib as f64 / total_memory_kib as f64) * 100.;
let ram_pct = format_pct(ram_used, pct_sign);
let threshold = config.threshold;
if ram_used.round() < threshold as f64 {
return None;
}
let ram = format_usage_total(used_memory_kib, total_memory_kib);
let total_swap_kib = system.get_total_swap();
let used_swap_kib = system.get_used_swap();
let percent_swap_used = (used_swap_kib as f64 / total_swap_kib as f64) * 100.;
let swap_pct = format_pct(percent_swap_used, pct_sign);
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,
})
.parse(None)
});
module.set_segments(match parsed {
Ok(segments) => segments,
Err(error) => {
log::warn!("Error in module `memory_usage`:\n{}", error);
return None;
}
});
Some(module)
}