feat: Add separator config to the memory module (#603)

This commit is contained in:
Matias Kotlik 2019-11-12 19:57:46 -06:00 committed by Matan Kushner
parent 512ed75ef2
commit 135dddbb4f
3 changed files with 23 additions and 21 deletions

View File

@ -713,6 +713,7 @@ To enable it, set `disabled` to `false` in your configuration file.
| `show_swap` | `true` | Display swap usage if total swap is non-zero. |
| `threshold` | `75` | Hide the memory usage unless it exceeds this percentage. |
| `symbol` | `"🐏 "` | The symbol used before displaying the memory usage. |
| `separator` | `" | "` | The symbol or text that will seperate the ram and swap usage. |
| `style` | `"bold dimmed white"` | The style for the module. |
| `disabled` | `true` | Disables the `memory_usage` module. |
@ -726,6 +727,7 @@ show_percentage = true
show_swap = true
threshold = -1
symbol = " "
separator = "/"
style = "bold dimmed green"
```

View File

@ -9,7 +9,9 @@ pub struct MemoryConfig<'a> {
pub show_swap: bool,
pub threshold: i64,
pub symbol: SegmentConfig<'a>,
pub display: SegmentConfig<'a>,
pub separator: SegmentConfig<'a>,
pub ram: SegmentConfig<'a>,
pub swap: SegmentConfig<'a>,
pub style: Style,
pub disabled: bool,
}
@ -20,8 +22,10 @@ impl<'a> RootModuleConfig<'a> for MemoryConfig<'a> {
show_percentage: false,
show_swap: true,
threshold: 75,
display: SegmentConfig::default(),
symbol: SegmentConfig::new("🐏 "),
separator: SegmentConfig::new(" | "),
ram: SegmentConfig::default(),
swap: SegmentConfig::default(),
style: Color::White.bold().dimmed(),
disabled: true,
}

View File

@ -31,6 +31,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}
module.set_style(config.style);
module.create_segment("symbol", &config.symbol);
let system = sysinfo::System::new_with_specifics(RefreshKind::new().with_system());
@ -47,7 +48,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let show_percentage = config.show_percentage;
let mut display = if show_percentage {
let ram = if show_percentage {
format!("{:.0}{}", percent_mem_used, percent_sign)
} else {
format!(
@ -56,6 +57,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
format_kib(total_memory_kib)
)
};
module.create_segment("ram", &config.ram.with_value(&ram));
// swap only shown if enabled and there is swap on the system
let total_swap_kib = system.get_total_swap();
@ -63,25 +65,19 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let used_swap_kib = system.get_used_swap();
let percent_swap_used = (used_swap_kib as f64 / total_swap_kib as f64) * 100.;
display = format!(
"{} | {}",
display,
if show_percentage {
format!("{:.0}{}", percent_swap_used, percent_sign)
} else {
format!(
"{}/{}",
format_kib(used_swap_kib),
format_kib(total_swap_kib)
)
}
);
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));
}
module.create_segment("symbol", &config.symbol);
module.create_segment("memory_usage", &config.display.with_value(&display));
module.get_prefix().set_value("");
Some(module)
}