use std::borrow::Cow;
use crate::format::{bar, format_bytes, gb, pad};
use crate::grouping::Group;
use crate::model::{Pressure, SystemStats};
pub const BAR_WIDTH: usize = 13;
pub const MAX_NAME_WIDTH: usize = 40;
const MIN_NAME_WIDTH: usize = 8;
pub const TAIL_COLUMNS: usize = 2 + 8 + 2 + 9;
const BAR_COLUMNS: usize = 2 + BAR_WIDTH;
pub fn name_width(available: usize, with_bar: bool) -> usize {
let tail = TAIL_COLUMNS + if with_bar { BAR_COLUMNS } else { 0 };
available.saturating_sub(tail).clamp(MIN_NAME_WIDTH, MAX_NAME_WIDTH)
}
pub fn group_columns(group: &Group, label: &str, name_width: usize, total_memory: u64, with_bar: bool) -> String {
let mut text = format!(
"{} {:>3} proc {:>9}",
pad(label, name_width),
group.procs.len(),
total_label(group)
);
if with_bar {
text.push_str(" ");
text.push_str(&bar(group.total, total_memory, BAR_WIDTH));
}
text
}
pub fn alert_label(group: &Group, mark: bool) -> Cow<'_, str> {
if mark {
Cow::Owned(format!("!{}", group.name))
} else {
Cow::Borrowed(&group.name)
}
}
pub fn total_label(group: &Group) -> String {
let prefix = if group.estimated { "~" } else { "" };
format!("{prefix}{}", format_bytes(group.total))
}
pub fn pressure_word(pressure: Pressure) -> &'static str {
match pressure {
Pressure::Normal => "normal",
Pressure::Warn => "warn",
Pressure::Critical => "critical",
}
}
pub fn memory_text(stats: &SystemStats) -> String {
format!(
"Memory {}/{} GB (wired {}) Pressure ",
gb(stats.used),
gb(stats.total),
gb(stats.wired)
)
}
pub fn swap_text(stats: &SystemStats) -> String {
format!(" Swap {} GB", gb(stats.swap_used))
}