use crate::format_util::{format_duration, format_kib, format_percent};
pub const AGENT_FIXED_COL_WIDTHS: [u16; 7] = [2, 10, 14, 8, 10, 9, 12];
pub const AGENT_COL_SPACING: u16 = 1;
const MIN_TASK_WIDTH: u16 = 1;
fn spacing_total() -> u16 {
AGENT_COL_SPACING * (AGENT_FIXED_COL_WIDTHS.len() as u16)
}
pub fn compressed_col_widths(area_width: u16) -> [u16; 7] {
let nominal_total: u16 = AGENT_FIXED_COL_WIDTHS.iter().sum();
let inner = area_width.saturating_sub(2);
let budget = inner
.saturating_sub(spacing_total())
.saturating_sub(MIN_TASK_WIDTH);
if budget >= nominal_total {
return AGENT_FIXED_COL_WIDTHS;
}
let mut out = [1u16; 7];
let mut used = 0u16;
for (slot, nominal) in out.iter_mut().zip(AGENT_FIXED_COL_WIDTHS) {
let scaled = (u32::from(nominal) * u32::from(budget)) / u32::from(nominal_total);
*slot = (scaled as u16).max(1);
used += *slot;
}
while used > budget {
let Some(widest) = widest_shrinkable(&out) else {
break;
};
out[widest] -= 1;
used -= 1;
}
out
}
fn widest_shrinkable(widths: &[u16; 7]) -> Option<usize> {
widths
.iter()
.enumerate()
.filter(|(_, w)| **w > 1)
.max_by_key(|(_, w)| **w)
.map(|(i, _)| i)
}
pub fn task_width(area_width: u16, fixed: &[u16; 7]) -> usize {
let fixed_total: u16 = fixed.iter().sum();
area_width
.saturating_sub(2)
.saturating_sub(fixed_total)
.saturating_sub(spacing_total())
.max(MIN_TASK_WIDTH) as usize
}
pub fn fit_header(header: &str, width: usize) -> String {
keep_leading(header, width)
}
pub fn fit_pid(pid: i32, width: usize) -> String {
keep_leading(&pid.to_string(), width)
}
pub fn fit_cpu(pct: f64, width: usize) -> String {
let full = format_percent(pct);
if full.chars().count() <= width {
return full;
}
let coarse = format!("{:.0}%", pct);
if coarse.chars().count() <= width {
return coarse;
}
keep_leading(&coarse, width)
}
pub fn fit_elapsed(secs: u64, width: usize) -> String {
let full = format_duration(secs);
if full.chars().count() <= width {
return full;
}
let h = secs / 3600;
let m = (secs % 3600) / 60;
if h > 0 {
let hm = format!("{}:{:02}", h, m);
if hm.chars().count() <= width {
return hm;
}
let hours = format!("{}h", h);
if hours.chars().count() <= width {
return hours;
}
return keep_leading(&hours, width);
}
if m > 0 {
let mins = format!("{}m", m);
if mins.chars().count() <= width {
return mins;
}
return keep_leading(&mins, width);
}
keep_leading(&full, width)
}
pub fn fit_mem(rss_kib: u64, width: usize) -> String {
let full = format_kib(rss_kib);
if full.chars().count() <= width {
return full;
}
let (value, unit, short) = scaled_mem(rss_kib);
let no_decimal = format!("{:.0}{}", value, unit);
if no_decimal.chars().count() <= width {
return no_decimal;
}
let short_unit = format!("{:.0}{}", value, short);
if short_unit.chars().count() <= width {
return short_unit;
}
keep_leading(&short_unit, width)
}
fn scaled_mem(kib: u64) -> (f64, &'static str, &'static str) {
let v = kib as f64;
if v >= 1_073_741_824.0 {
(v / 1_073_741_824.0, "TiB", "T")
} else if v >= 1_048_576.0 {
(v / 1_048_576.0, "GiB", "G")
} else if v >= 1024.0 {
(v / 1024.0, "MiB", "M")
} else {
(v, "KiB", "K")
}
}
fn keep_leading(s: &str, width: usize) -> String {
s.chars().take(width).collect()
}
#[cfg(test)]
mod tests {
use super::*;
const NOMINAL_MIN_WIDTH: u16 = 2 + 65 + 7 + 1;
#[test]
fn wide_terminal_keeps_the_nominal_widths() {
assert_eq!(compressed_col_widths(200), AGENT_FIXED_COL_WIDTHS);
assert_eq!(compressed_col_widths(120), AGENT_FIXED_COL_WIDTHS);
assert_eq!(compressed_col_widths(80), AGENT_FIXED_COL_WIDTHS);
assert_eq!(
compressed_col_widths(NOMINAL_MIN_WIDTH),
AGENT_FIXED_COL_WIDTHS
);
assert_ne!(
compressed_col_widths(NOMINAL_MIN_WIDTH - 1),
AGENT_FIXED_COL_WIDTHS
);
}
#[test]
fn narrow_terminal_shrinks_within_budget_and_never_below_one() {
for area_width in 1u16..=NOMINAL_MIN_WIDTH {
let widths = compressed_col_widths(area_width);
let total: u16 = widths.iter().sum();
assert!(
widths.iter().all(|w| *w >= 1),
"width {area_width} produced a zero-width column: {widths:?}"
);
let budget = area_width
.saturating_sub(2)
.saturating_sub(7)
.saturating_sub(1);
assert!(
total <= budget.max(7),
"width {area_width}: {widths:?} sums to {total}, budget {budget}"
);
}
}
#[test]
fn task_column_always_keeps_at_least_one_column() {
for area_width in 0u16..=200 {
let widths = compressed_col_widths(area_width);
assert!(task_width(area_width, &widths) >= 1);
}
}
#[test]
fn table_never_overflows_the_terminal_width() {
for area_width in 17u16..=200 {
let widths = compressed_col_widths(area_width);
let total: u16 = widths.iter().sum();
let laid_out = total + 7 + task_width(area_width, &widths) as u16;
assert_eq!(
laid_out,
area_width - 2,
"width {area_width} lays out {laid_out} inside {}",
area_width - 2
);
}
}
#[test]
fn mem_ladder_trades_precision_then_unit_then_digits() {
let rss = 868_966; assert_eq!(fit_mem(rss, 12), "848.6MiB");
assert_eq!(fit_mem(rss, 8), "848.6MiB");
assert_eq!(fit_mem(rss, 7), "849MiB");
assert_eq!(fit_mem(rss, 6), "849MiB");
assert_eq!(fit_mem(rss, 5), "849M");
assert_eq!(fit_mem(rss, 4), "849M");
assert_eq!(fit_mem(rss, 3), "849");
assert_eq!(fit_mem(rss, 1), "8");
assert_eq!(fit_mem(rss, 0), "");
}
#[test]
fn mem_ladder_covers_every_unit() {
assert_eq!(fit_mem(12_884_901, 8), "12.3GiB");
assert_eq!(fit_mem(12_884_901, 5), "12GiB");
assert_eq!(fit_mem(12_884_901, 4), "12G");
assert_eq!(fit_mem(12_884_901, 3), "12G");
assert_eq!(fit_mem(512, 6), "512KiB");
assert_eq!(fit_mem(512, 4), "512K");
assert_eq!(fit_mem(1_073_741_824, 6), "1.0TiB");
assert_eq!(fit_mem(1_073_741_824, 4), "1TiB");
assert_eq!(fit_mem(1_073_741_824, 2), "1T");
}
#[test]
fn elapsed_ladder_keeps_the_hours() {
let secs = 38 * 3600 + 23 * 60 + 40; assert_eq!(fit_elapsed(secs, 10), "38:23:40");
assert_eq!(fit_elapsed(secs, 8), "38:23:40");
assert_eq!(fit_elapsed(secs, 7), "38:23");
assert_eq!(fit_elapsed(secs, 5), "38:23");
assert_eq!(fit_elapsed(secs, 4), "38h");
assert_eq!(fit_elapsed(secs, 3), "38h");
assert_eq!(fit_elapsed(secs, 2), "38");
assert_eq!(fit_elapsed(secs, 1), "3");
}
#[test]
fn elapsed_ladder_handles_sub_hour_and_sub_minute() {
assert_eq!(fit_elapsed(125, 10), "2:05");
assert_eq!(fit_elapsed(125, 4), "2:05");
assert_eq!(fit_elapsed(125, 3), "2m");
assert_eq!(fit_elapsed(125, 1), "2");
assert_eq!(fit_elapsed(5, 4), "5s");
assert_eq!(fit_elapsed(5, 1), "5");
}
#[test]
fn cpu_ladder_drops_the_decimal_before_a_digit() {
assert_eq!(fit_cpu(131.8, 9), "131.8%");
assert_eq!(fit_cpu(131.8, 6), "131.8%");
assert_eq!(fit_cpu(131.8, 5), "132%");
assert_eq!(fit_cpu(131.8, 4), "132%");
assert_eq!(fit_cpu(131.8, 3), "132");
assert_eq!(fit_cpu(131.8, 2), "13");
assert_eq!(fit_cpu(2.0, 4), "2.0%");
assert_eq!(fit_cpu(2.0, 3), "2%");
}
#[test]
fn pid_and_header_keep_their_leading_characters() {
assert_eq!(fit_pid(827_115, 8), "827115");
assert_eq!(fit_pid(827_115, 4), "8271");
assert_eq!(fit_pid(827_115, 0), "");
assert_eq!(fit_header("ELAPSED", 10), "ELAPSED");
assert_eq!(fit_header("ELAPSED", 5), "ELAPS");
assert_eq!(fit_header("MEM", 1), "M");
}
#[test]
fn every_fitted_cell_stays_inside_its_budget() {
let pids = [1, 42, 827_115, i32::MAX];
let elapsed = [0, 5, 125, 3725, 38 * 3600 + 23 * 60 + 40, 900 * 3600];
let cpus = [0.0, 2.0, 31.8, 131.8, 1600.0];
let mems = [0, 512, 868_966, 12_884_901, 3_221_225_472];
for area_width in 1u16..=200 {
let widths = compressed_col_widths(area_width);
let (pid_w, elapsed_w, cpu_w, mem_w) = (
widths[3] as usize,
widths[4] as usize,
widths[5] as usize,
widths[6] as usize,
);
for pid in pids {
assert!(fit_pid(pid, pid_w).chars().count() <= pid_w);
}
for secs in elapsed {
assert!(fit_elapsed(secs, elapsed_w).chars().count() <= elapsed_w);
}
for pct in cpus {
assert!(fit_cpu(pct, cpu_w).chars().count() <= cpu_w);
}
for kib in mems {
assert!(fit_mem(kib, mem_w).chars().count() <= mem_w);
}
for (header, w) in [
("PID", pid_w),
("ELAPSED", elapsed_w),
("CPU%", cpu_w),
("MEM", mem_w),
] {
assert!(fit_header(header, w).chars().count() <= w);
}
}
}
}