diff --git a/docs/config/README.md b/docs/config/README.md index c8cbee2bf290..369a7febf957 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -691,7 +691,7 @@ To enable it, set `disabled` to `false` in your configuration file. | Variable | Default | Description | | ----------------- | ------------------------ | ------------------------------------------------------------- | | `show_percentage` | `false` | Display memory usage as a percentage of the available memory. | -| `show_swap` | when total swap non-zero | Display swap usage. | +| `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. | | `style` | `"bold dimmed white"` | The style for the module. | diff --git a/src/configs/memory_usage.rs b/src/configs/memory_usage.rs new file mode 100644 index 000000000000..8b2b87982e4f --- /dev/null +++ b/src/configs/memory_usage.rs @@ -0,0 +1,29 @@ +use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; + +use ansi_term::{Color, Style}; +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct MemoryConfig<'a> { + pub show_percentage: bool, + pub show_swap: bool, + pub threshold: i64, + pub symbol: SegmentConfig<'a>, + pub display: SegmentConfig<'a>, + pub style: Style, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for MemoryConfig<'a> { + fn new() -> Self { + MemoryConfig { + show_percentage: false, + show_swap: true, + threshold: 75, + display: SegmentConfig::default(), + symbol: SegmentConfig::new("🐏 "), + style: Color::White.bold().dimmed(), + disabled: true, + } + } +} diff --git a/src/configs/mod.rs b/src/configs/mod.rs index 95743df1c30d..f313a24e7a94 100644 --- a/src/configs/mod.rs +++ b/src/configs/mod.rs @@ -11,6 +11,7 @@ pub mod go; pub mod hostname; pub mod jobs; pub mod kubernetes; +pub mod memory_usage; pub mod nodejs; pub mod package; pub mod python; diff --git a/src/modules/memory_usage.rs b/src/modules/memory_usage.rs index e3b7918c7222..73324d83f30b 100644 --- a/src/modules/memory_usage.rs +++ b/src/modules/memory_usage.rs @@ -1,90 +1,86 @@ -use ansi_term::Color; - -use super::{Context, Module}; use byte_unit::{Byte, ByteUnit}; use sysinfo::{RefreshKind, SystemExt}; +use super::{Context, Module, RootModuleConfig}; + +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 +} + /// Creates a module with system memory usage information pub fn module<'a>(context: &'a Context) -> Option> { - const DEFAULT_THRESHOLD: i64 = 75; - const DEFAULT_SHOW_PERCENTAGE: bool = false; - const RAM_CHAR: &str = "🐏 "; - 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 shell = std::env::var("STARSHIP_SHELL").unwrap_or_default(); + let percent_sign = match shell.as_str() { + "zsh" => "%%", // % is an escape in zsh, see PROMPT in `man zshmisc` + "powershell" => "`%", + _ => "%", + }; - if module.config_value_bool("disabled").unwrap_or(true) { + if config.disabled { return None; } - let module_style = module - .config_value_style("style") - .unwrap_or_else(|| Color::White.bold().dimmed()); + module.set_style(config.style); let system = sysinfo::System::new_with_specifics(RefreshKind::new().with_system()); let used_memory_kib = system.get_used_memory(); let total_memory_kib = system.get_total_memory(); - let used_swap_kib = system.get_used_swap(); - let total_swap_kib = system.get_total_swap(); let percent_mem_used = (used_memory_kib as f64 / total_memory_kib as f64) * 100.; - let percent_swap_used = (used_swap_kib as f64 / total_swap_kib as f64) * 100.; - let threshold = module - .config_value_i64("threshold") - .unwrap_or(DEFAULT_THRESHOLD); + let threshold = config.threshold; if percent_mem_used.round() < threshold as f64 { return None; } - let show_percentage = module - .config_value_bool("show_percentage") - .unwrap_or(DEFAULT_SHOW_PERCENTAGE); + let show_percentage = config.show_percentage; - let (display_mem, display_swap) = if show_percentage { - ( - format!("{:.0}%", percent_mem_used), - format!("{:.0}%", percent_swap_used), - ) + let mut display = if show_percentage { + format!("{:.0}{}", percent_mem_used, percent_sign) } else { - 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 - } - ( - format!( - "{}/{}", - format_kib(used_memory_kib), - format_kib(total_memory_kib) - ), - format!( - "{}/{}", - format_kib(used_swap_kib), - format_kib(total_swap_kib) - ), + format!( + "{}/{}", + format_kib(used_memory_kib), + format_kib(total_memory_kib) ) }; - let show_swap = module - .config_value_bool("show_swap") - .unwrap_or(total_swap_kib != 0); - - module.new_segment("symbol", RAM_CHAR); - - module.set_style(module_style); - if show_swap { - module.new_segment( - "memory_usage", - &format!("{} | {}", display_mem, display_swap), + // 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.; + + 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) + ) + } ); - } else { - module.new_segment("memory_usage", &display_mem); } + module.create_segment("symbol", &config.symbol); + module.create_segment("memory_usage", &config.display.with_value(&display)); + module.get_prefix().set_value(""); Some(module)