mirror of
https://github.com/starship/starship.git
synced 2026-06-24 02:01:36 +07:00
refactor: Refactor memory_usage module to use module config. (#515)
Also addresses a number of bugs: - the percent sign not displaying correctly on some terminal emulators, including kitty - changing the symbol in the configuration file didn't do anything - swap being shown even if the system didn't have any
This commit is contained in:
committed by
Matan Kushner
parent
e3f1a76e97
commit
86bb923303
@@ -691,7 +691,7 @@ To enable it, set `disabled` to `false` in your configuration file.
|
|||||||
| Variable | Default | Description |
|
| Variable | Default | Description |
|
||||||
| ----------------- | ------------------------ | ------------------------------------------------------------- |
|
| ----------------- | ------------------------ | ------------------------------------------------------------- |
|
||||||
| `show_percentage` | `false` | Display memory usage as a percentage of the available memory. |
|
| `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. |
|
| `threshold` | `75` | Hide the memory usage unless it exceeds this percentage. |
|
||||||
| `symbol` | `"🐏 "` | The symbol used before displaying the memory usage. |
|
| `symbol` | `"🐏 "` | The symbol used before displaying the memory usage. |
|
||||||
| `style` | `"bold dimmed white"` | The style for the module. |
|
| `style` | `"bold dimmed white"` | The style for the module. |
|
||||||
|
|||||||
@@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ pub mod go;
|
|||||||
pub mod hostname;
|
pub mod hostname;
|
||||||
pub mod jobs;
|
pub mod jobs;
|
||||||
pub mod kubernetes;
|
pub mod kubernetes;
|
||||||
|
pub mod memory_usage;
|
||||||
pub mod nodejs;
|
pub mod nodejs;
|
||||||
pub mod package;
|
pub mod package;
|
||||||
pub mod python;
|
pub mod python;
|
||||||
|
|||||||
+51
-55
@@ -1,90 +1,86 @@
|
|||||||
use ansi_term::Color;
|
|
||||||
|
|
||||||
use super::{Context, Module};
|
|
||||||
use byte_unit::{Byte, ByteUnit};
|
use byte_unit::{Byte, ByteUnit};
|
||||||
use sysinfo::{RefreshKind, SystemExt};
|
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
|
/// Creates a module with system memory usage information
|
||||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||||
const DEFAULT_THRESHOLD: i64 = 75;
|
|
||||||
const DEFAULT_SHOW_PERCENTAGE: bool = false;
|
|
||||||
const RAM_CHAR: &str = "🐏 ";
|
|
||||||
|
|
||||||
let mut module = context.new_module("memory_usage");
|
let mut module = context.new_module("memory_usage");
|
||||||
|
let config = MemoryConfig::try_load(module.config);
|
||||||
|
|
||||||
if module.config_value_bool("disabled").unwrap_or(true) {
|
// 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 config.disabled {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let module_style = module
|
module.set_style(config.style);
|
||||||
.config_value_style("style")
|
|
||||||
.unwrap_or_else(|| Color::White.bold().dimmed());
|
|
||||||
|
|
||||||
let system = sysinfo::System::new_with_specifics(RefreshKind::new().with_system());
|
let system = sysinfo::System::new_with_specifics(RefreshKind::new().with_system());
|
||||||
|
|
||||||
let used_memory_kib = system.get_used_memory();
|
let used_memory_kib = system.get_used_memory();
|
||||||
let total_memory_kib = system.get_total_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_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
|
let threshold = config.threshold;
|
||||||
.config_value_i64("threshold")
|
|
||||||
.unwrap_or(DEFAULT_THRESHOLD);
|
|
||||||
|
|
||||||
if percent_mem_used.round() < threshold as f64 {
|
if percent_mem_used.round() < threshold as f64 {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let show_percentage = module
|
let show_percentage = config.show_percentage;
|
||||||
.config_value_bool("show_percentage")
|
|
||||||
.unwrap_or(DEFAULT_SHOW_PERCENTAGE);
|
|
||||||
|
|
||||||
let (display_mem, display_swap) = if show_percentage {
|
let mut display = if show_percentage {
|
||||||
(
|
format!("{:.0}{}", percent_mem_used, percent_sign)
|
||||||
format!("{:.0}%", percent_mem_used),
|
|
||||||
format!("{:.0}%", percent_swap_used),
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
fn format_kib(n_kib: u64) -> String {
|
format!(
|
||||||
let byte = Byte::from_unit(n_kib as f64, ByteUnit::KiB)
|
"{}/{}",
|
||||||
.unwrap_or_else(|_| Byte::from_bytes(0));
|
format_kib(used_memory_kib),
|
||||||
let mut display_bytes = byte.get_appropriate_unit(true).format(0);
|
format_kib(total_memory_kib)
|
||||||
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)
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let show_swap = module
|
// swap only shown if enabled and there is swap on the system
|
||||||
.config_value_bool("show_swap")
|
let total_swap_kib = system.get_total_swap();
|
||||||
.unwrap_or(total_swap_kib != 0);
|
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.;
|
||||||
|
|
||||||
module.new_segment("symbol", RAM_CHAR);
|
display = format!(
|
||||||
|
"{} | {}",
|
||||||
module.set_style(module_style);
|
display,
|
||||||
if show_swap {
|
if show_percentage {
|
||||||
module.new_segment(
|
format!("{:.0}{}", percent_swap_used, percent_sign)
|
||||||
"memory_usage",
|
} else {
|
||||||
&format!("{} | {}", display_mem, display_swap),
|
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("");
|
module.get_prefix().set_value("");
|
||||||
|
|
||||||
Some(module)
|
Some(module)
|
||||||
|
|||||||
Reference in New Issue
Block a user