M2: terminal UI (ratatui)
- layout: header, top menu (quicklook, cpu, mem, swap, load), left sidebar, process list and alert footer - decorations/colors, bars, per-plugin render implementations - hotkeys: quit, help, modes, sorting, cursor, nice, kill, filters, toggles - TestBackend layout test
This commit is contained in:
@@ -67,6 +67,35 @@ pub fn auto_unit(number: f64, low_precision: bool, min_symbol: &str, none_symbol
|
||||
format!("{number:.decimal_precision$}")
|
||||
}
|
||||
|
||||
/// Render a progress bar the way the Python `Bar` class does.
|
||||
pub fn bar_percent(percent: f64, size: usize, bar_char: &str, display_value: bool) -> String {
|
||||
let size = if display_value { size.saturating_sub(6) } else { size };
|
||||
let value = percent.clamp(0.0, 100.0);
|
||||
let scaled = size as f64 * value / 100.0;
|
||||
let whole = scaled.floor() as usize;
|
||||
let frac = scaled - whole as f64;
|
||||
let mut bar = bar_char.repeat(whole);
|
||||
let mut whole = whole;
|
||||
if frac > 0.0 {
|
||||
// curses_bars = [empty; 5] + [bar; 5]: index 5..7 draws a partial block.
|
||||
if (frac * 8.0) as usize >= 5 {
|
||||
bar.push_str(bar_char);
|
||||
} else {
|
||||
bar.push(' ');
|
||||
}
|
||||
whole += 1;
|
||||
}
|
||||
bar.push_str(&" ".repeat(size.saturating_sub(whole)));
|
||||
if display_value {
|
||||
if percent >= 100.0 {
|
||||
bar = format!("{bar} {}{:3.0}%", if percent > 100.0 { ">" } else { " " }, 100.0);
|
||||
} else {
|
||||
bar = format!("{bar}{percent:5.1}%");
|
||||
}
|
||||
}
|
||||
bar
|
||||
}
|
||||
|
||||
/// Parse a value like `'12.5 MB'` into a float (port of `string_value_to_float`).
|
||||
pub fn string_value_to_float(s: &str) -> Option<f64> {
|
||||
let cleaned: String = s.chars().filter(|c| *c != ' ').collect();
|
||||
|
||||
Reference in New Issue
Block a user