| 1 | //! Progress from reported counts, never from an animation or elapsed time. |
| 2 | //! |
| 3 | //! [`CountBar`] says how many of how many in words (`3 of 5 phases`), because |
| 4 | //! the count is the fact. A bar beside it is optional ink and is drawn only |
| 5 | //! when the total is known: |
| 6 | //! |
| 7 | //! - no percentage, no time-remaining estimate, no smoothing: the bar fills |
| 8 | //! in eighth cells (whole cells in ASCII), never reaches full before |
| 9 | //! `done` reaches `total`, and never shows work that has not happened; |
| 10 | //! - when `done` passes `total` the words keep the real numbers (`7 of 5`); |
| 11 | //! only the painted fill is clamped; |
| 12 | //! - when the total is unknown there is no bar at all, and the words say so |
| 13 | //! (`12 files so far, total unknown`) instead of guessing a fraction. |
| 14 | //! |
| 15 | //! The state (and so the mark and word) comes from the host; the bar never |
| 16 | //! infers "done" from the counts. |
| 17 | |
| 18 | use std::borrow::Cow; |
| 19 | |
| 20 | use ratatui::{ |
| 21 | buffer::Buffer, |
| 22 | layout::Rect, |
| 23 | style::{Modifier, Style}, |
| 24 | symbols::block, |
| 25 | text::{Line, Span}, |
| 26 | widgets::Widget, |
| 27 | }; |
| 28 | |
| 29 | use crate::{ |
| 30 | Paint, Role, State, StateWords, StatusMark, Theme, color, detect::Appearance, glyphs, text, |
| 31 | }; |
| 32 | |
| 33 | /// Words a count bar prints. `Default` is English. |
| 34 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 35 | pub struct CountBarWords { |
| 36 | /// The word beside the mark. |
| 37 | pub states: StateWords, |
| 38 | /// `12 files so far, total unknown`: what follows the count. |
| 39 | pub unknown_total: Cow<'static, str>, |
| 40 | /// `3 of 5 phases`. |
| 41 | pub of: Cow<'static, str>, |
| 42 | /// `12 files so far`. |
| 43 | pub so_far: Cow<'static, str>, |
| 44 | } |
| 45 | |
| 46 | impl Default for CountBarWords { |
| 47 | fn default() -> Self { |
| 48 | Self { |
| 49 | states: StateWords::default(), |
| 50 | unknown_total: Cow::Borrowed("total unknown"), |
| 51 | of: Cow::Borrowed("of"), |
| 52 | so_far: Cow::Borrowed("so far"), |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// `● Working 3 of 5 phases [██████░░░░░░]`. |
| 58 | /// |
| 59 | /// The host reports the state and the exact counts. Only the painted |
| 60 | /// fraction is clamped: `12 of 10` stays `12 of 10`. A zero total paints no |
| 61 | /// completed ink; an unknown total has no bar. |
| 62 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 63 | pub struct CountBar { |
| 64 | pub done: u64, |
| 65 | /// `None` when nobody knows how many there will be. |
| 66 | pub total: Option<u64>, |
| 67 | pub state: State, |
| 68 | /// What is being counted: `phases`, `files`. |
| 69 | pub label: Cow<'static, str>, |
| 70 | pub words: CountBarWords, |
| 71 | } |
| 72 | |
| 73 | impl CountBar { |
| 74 | /// `done` of `total`, working. |
| 75 | #[must_use] |
| 76 | pub fn new(done: u64, total: u64) -> Self { |
| 77 | Self { |
| 78 | done, |
| 79 | total: Some(total), |
| 80 | state: State::Working, |
| 81 | label: Cow::Borrowed(""), |
| 82 | words: CountBarWords::default(), |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// `done` so far, with no known total: the indeterminate form. |
| 87 | #[must_use] |
| 88 | pub fn unknown(done: u64) -> Self { |
| 89 | Self { |
| 90 | total: None, |
| 91 | state: State::Unknown, |
| 92 | ..Self::new(done, 0) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | #[must_use] |
| 97 | pub fn state(mut self, state: State) -> Self { |
| 98 | self.state = state; |
| 99 | self |
| 100 | } |
| 101 | |
| 102 | #[must_use] |
| 103 | pub fn label(mut self, label: impl Into<Cow<'static, str>>) -> Self { |
| 104 | self.label = label.into(); |
| 105 | self |
| 106 | } |
| 107 | |
| 108 | #[must_use] |
| 109 | pub fn with_words(mut self, words: &CountBarWords) -> Self { |
| 110 | self.words = words.clone(); |
| 111 | self |
| 112 | } |
| 113 | |
| 114 | /// How many bar cells are filled out of `cells`: whole cells, rounded |
| 115 | /// down, and never all of them before the work is complete. |
| 116 | #[must_use] |
| 117 | pub fn filled(&self, cells: u16) -> u16 { |
| 118 | let Some(total) = self.total else { return 0 }; |
| 119 | if total == 0 || cells == 0 { |
| 120 | return 0; |
| 121 | } |
| 122 | if self.done >= total { |
| 123 | return cells; |
| 124 | } |
| 125 | let part = u128::from(self.done) * u128::from(cells) / u128::from(total); |
| 126 | u16::try_from(part).unwrap_or(cells).min(cells - 1) |
| 127 | } |
| 128 | |
| 129 | /// The words: `3 of 5 phases`, or `12 files so far, total unknown`. |
| 130 | fn counting(&self) -> String { |
| 131 | let label = text::display_safe(&self.label); |
| 132 | let unit = if label.is_empty() { |
| 133 | String::new() |
| 134 | } else { |
| 135 | format!(" {label}") |
| 136 | }; |
| 137 | match self.total { |
| 138 | Some(total) => format!( |
| 139 | "{} {} {total}{unit}", |
| 140 | self.done, |
| 141 | text::display_safe(&self.words.of) |
| 142 | ), |
| 143 | None => { |
| 144 | let sep = if unit.is_empty() { "" } else { " " }; |
| 145 | format!( |
| 146 | "{}{unit}{sep}{}, {}", |
| 147 | self.done, |
| 148 | text::display_safe(&self.words.so_far), |
| 149 | text::display_safe(&self.words.unknown_total) |
| 150 | ) |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /// The bar never grows past this many cells; a longer one says nothing more. |
| 157 | const MAX_BAR: usize = 24; |
| 158 | /// Narrower than this the bar is left out: the words carry the count. |
| 159 | const MIN_BAR: usize = 6; |
| 160 | |
| 161 | impl Paint for CountBar { |
| 162 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 163 | let area = area.intersection(buf.area); |
| 164 | if area.is_empty() { |
| 165 | return; |
| 166 | } |
| 167 | let width = usize::from(area.width); |
| 168 | let ascii = theme.ascii(); |
| 169 | let mark = StatusMark::with_words(self.state, &self.words.states); |
| 170 | let glyph = mark.glyph(theme); |
| 171 | let word = text::display_safe(&mark.word).into_owned(); |
| 172 | let counting = self.counting(); |
| 173 | // When the row is too narrow the state word goes first (the mark |
| 174 | // stays), then the end of the count's words; never the numbers. |
| 175 | let with_word = text::width(glyph) + 1 + text::width(&word) + 2; |
| 176 | let without_word = text::width(glyph) + 1; |
| 177 | let counting_w = text::width(&counting); |
| 178 | let show_word = with_word + counting_w <= width; |
| 179 | let lead_w = if show_word { with_word } else { without_word }; |
| 180 | |
| 181 | let mut spans = vec![Span::styled(glyph, theme.fg(self.state.role()))]; |
| 182 | if show_word { |
| 183 | spans.push(Span::styled( |
| 184 | format!(" {word} "), |
| 185 | theme.fg(Role::Foreground), |
| 186 | )); |
| 187 | } else { |
| 188 | spans.push(Span::raw(" ")); |
| 189 | } |
| 190 | let room = width.saturating_sub(lead_w); |
| 191 | let fits = counting_w <= room; |
| 192 | let shown = if fits { |
| 193 | counting |
| 194 | } else { |
| 195 | text::truncate(&counting, room, ascii).into_owned() |
| 196 | }; |
| 197 | spans.push(Span::styled(shown, theme.fg(Role::Foreground))); |
| 198 | |
| 199 | if self.total.is_some() && fits { |
| 200 | let cells = (room - counting_w).saturating_sub(4).min(MAX_BAR); |
| 201 | if cells >= MIN_BAR { |
| 202 | let filled = usize::from(self.filled(cells as u16)); |
| 203 | let fraction = match self.total { |
| 204 | Some(total) if !ascii && total > 0 && self.done < total => { |
| 205 | ((u128::from(self.done) * cells as u128 * 8 / u128::from(total)) % 8) |
| 206 | as usize |
| 207 | } |
| 208 | _ => 0, |
| 209 | }; |
| 210 | spans.push(Span::raw(" ")); |
| 211 | spans.push(Span::styled("[", theme.fg(Role::Muted))); |
| 212 | spans.push(Span::styled( |
| 213 | glyphs::pick("\u{2588}", ascii).repeat(filled), |
| 214 | theme.fg(self.state.role()), |
| 215 | )); |
| 216 | if fraction > 0 { |
| 217 | let edge = [ |
| 218 | "", |
| 219 | block::ONE_EIGHTH, |
| 220 | block::ONE_QUARTER, |
| 221 | block::THREE_EIGHTHS, |
| 222 | block::HALF, |
| 223 | block::FIVE_EIGHTHS, |
| 224 | block::THREE_QUARTERS, |
| 225 | block::SEVEN_EIGHTHS, |
| 226 | ][fraction]; |
| 227 | spans.push(Span::styled(edge, theme.fg(self.state.role()))); |
| 228 | } |
| 229 | spans.push(Span::styled( |
| 230 | glyphs::pick("\u{2591}", ascii) |
| 231 | .repeat(cells - filled - usize::from(fraction > 0)), |
| 232 | theme.fg(Role::Dim), |
| 233 | )); |
| 234 | spans.push(Span::styled("]", theme.fg(Role::Muted))); |
| 235 | } |
| 236 | } |
| 237 | Line::from(spans).render(Rect::new(area.x, area.y, area.width, 1), buf); |
| 238 | } |
| 239 | |
| 240 | fn height(&self, _width: u16, _theme: &Theme) -> u16 { |
| 241 | 1 |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /// One proportional slice of a [`SegmentedMeter`]. |
| 246 | #[derive(Clone, Debug, PartialEq)] |
| 247 | pub struct MeterSegment<'a> { |
| 248 | pub label: Cow<'a, str>, |
| 249 | pub value: f64, |
| 250 | pub role: Role, |
| 251 | } |
| 252 | |
| 253 | impl<'a> MeterSegment<'a> { |
| 254 | /// Create a named proportional segment colored by `role`. |
| 255 | #[must_use] |
| 256 | pub fn new(label: impl Into<Cow<'a, str>>, value: f64, role: Role) -> Self { |
| 257 | Self { |
| 258 | label: label.into(), |
| 259 | value, |
| 260 | role, |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /// Multi-segment proportional OKLab gradient ribbon with eighth-cell sub-pixel |
| 266 | /// precision and an optional live threshold marker (for example, an 80% |
| 267 | /// context compaction tick). |
| 268 | /// |
| 269 | /// On truecolor terminals with painted grounds, filled cells render smoothly |
| 270 | /// across a recessed OKLab trough with a subtle specular frontier glint on the |
| 271 | /// leading cell. On 256-color, 16-color, `NO_COLOR`, and ASCII profiles it |
| 272 | /// degrades cleanly to bracketed ASCII fills and exact text readouts. |
| 273 | #[derive(Clone, Debug)] |
| 274 | pub struct SegmentedMeter<'a> { |
| 275 | pub label: Cow<'a, str>, |
| 276 | pub segments: &'a [MeterSegment<'a>], |
| 277 | pub total: f64, |
| 278 | pub threshold: Option<f32>, |
| 279 | pub pressure_gradient: bool, |
| 280 | pub show_legend: bool, |
| 281 | pub summary: Option<Cow<'a, str>>, |
| 282 | } |
| 283 | |
| 284 | impl<'a> SegmentedMeter<'a> { |
| 285 | /// Create a segmented meter against `total` capacity. |
| 286 | #[must_use] |
| 287 | pub fn new(segments: &'a [MeterSegment<'a>], total: f64) -> Self { |
| 288 | Self { |
| 289 | label: Cow::Borrowed(""), |
| 290 | segments, |
| 291 | total, |
| 292 | threshold: None, |
| 293 | pressure_gradient: false, |
| 294 | show_legend: false, |
| 295 | summary: None, |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /// Leading title label (for example, `"Context"`). |
| 300 | #[must_use] |
| 301 | pub fn label(mut self, label: impl Into<Cow<'a, str>>) -> Self { |
| 302 | self.label = label.into(); |
| 303 | self |
| 304 | } |
| 305 | |
| 306 | /// Optional threshold tick marker in `0.0..=1.0` (for example, `0.80` for |
| 307 | /// auto-compaction). |
| 308 | #[must_use] |
| 309 | pub fn threshold(mut self, threshold: f32) -> Self { |
| 310 | self.threshold = threshold.is_finite().then(|| threshold.clamp(0.0, 1.0)); |
| 311 | self |
| 312 | } |
| 313 | |
| 314 | /// Sweep the filled ribbon through a continuous OKLab pressure gradient |
| 315 | /// (`Accent` → `Live` → `Attention` → `Danger`) instead of flat per-segment |
| 316 | /// fills. |
| 317 | #[must_use] |
| 318 | pub const fn pressure_gradient(mut self, enabled: bool) -> Self { |
| 319 | self.pressure_gradient = enabled; |
| 320 | self |
| 321 | } |
| 322 | |
| 323 | /// Render a compact second-row legend naming each segment and its share. |
| 324 | #[must_use] |
| 325 | pub const fn show_legend(mut self, enabled: bool) -> Self { |
| 326 | self.show_legend = enabled; |
| 327 | self |
| 328 | } |
| 329 | |
| 330 | /// Custom trailing summary text (defaults to `{percent}%`). |
| 331 | #[must_use] |
| 332 | pub fn summary(mut self, summary: impl Into<Cow<'a, str>>) -> Self { |
| 333 | self.summary = Some(summary.into()); |
| 334 | self |
| 335 | } |
| 336 | |
| 337 | /// Sum of finite non-negative segment values. |
| 338 | #[must_use] |
| 339 | pub fn used(&self) -> f64 { |
| 340 | self.segments |
| 341 | .iter() |
| 342 | .filter(|s| s.value.is_finite() && s.value > 0.0) |
| 343 | .map(|s| s.value) |
| 344 | .sum() |
| 345 | } |
| 346 | |
| 347 | /// Overall fill ratio clamped to `0.0..=1.0`. |
| 348 | #[must_use] |
| 349 | pub fn ratio(&self) -> f32 { |
| 350 | if !self.total.is_finite() || self.total <= 0.0 { |
| 351 | return 0.0; |
| 352 | } |
| 353 | (self.used() / self.total).clamp(0.0, 1.0) as f32 |
| 354 | } |
| 355 | |
| 356 | /// Exact plain-text summary for transcripts, logs and screen readers. |
| 357 | #[must_use] |
| 358 | pub fn describe(&self) -> String { |
| 359 | let pct = (self.ratio() * 100.0).round() as u32; |
| 360 | let head = if self.label.is_empty() { |
| 361 | format!("{pct}%") |
| 362 | } else { |
| 363 | format!("{}: {pct}%", text::display_safe(&self.label)) |
| 364 | }; |
| 365 | if self.segments.is_empty() || !self.total.is_finite() || self.total <= 0.0 { |
| 366 | return head; |
| 367 | } |
| 368 | let parts: Vec<String> = self |
| 369 | .segments |
| 370 | .iter() |
| 371 | .filter(|s| s.value.is_finite() && s.value > 0.0) |
| 372 | .map(|s| { |
| 373 | let share = ((s.value / self.total) * 100.0).round() as u32; |
| 374 | format!("{} {share}%", text::display_safe(&s.label)) |
| 375 | }) |
| 376 | .collect(); |
| 377 | if parts.is_empty() { |
| 378 | head |
| 379 | } else { |
| 380 | format!("{head} ({})", parts.join(", ")) |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | const METER_EIGHTHS: [&str; 9] = [ |
| 386 | " ", |
| 387 | block::ONE_EIGHTH, |
| 388 | block::ONE_QUARTER, |
| 389 | block::THREE_EIGHTHS, |
| 390 | block::HALF, |
| 391 | block::FIVE_EIGHTHS, |
| 392 | block::THREE_QUARTERS, |
| 393 | block::SEVEN_EIGHTHS, |
| 394 | block::FULL, |
| 395 | ]; |
| 396 | |
| 397 | const METER_ASCII_MARKS: [&str; 4] = ["#", "=", "-", "+"]; |
| 398 | |
| 399 | impl Paint for SegmentedMeter<'_> { |
| 400 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 401 | let area = area.intersection(buf.area); |
| 402 | if area.is_empty() { |
| 403 | return; |
| 404 | } |
| 405 | let width = usize::from(area.width); |
| 406 | let ascii = theme.ascii(); |
| 407 | let ratio = self.ratio(); |
| 408 | let pct = (ratio * 100.0).round() as u32; |
| 409 | |
| 410 | let summary_text = self |
| 411 | .summary |
| 412 | .as_deref() |
| 413 | .map(|s| text::display_safe(s).into_owned()) |
| 414 | .unwrap_or_else(|| format!("{pct}%")); |
| 415 | let summary_role = if ratio >= 0.92 { |
| 416 | Role::Danger |
| 417 | } else if self.threshold.is_some_and(|t| ratio >= t) { |
| 418 | Role::Attention |
| 419 | } else { |
| 420 | Role::Foreground |
| 421 | }; |
| 422 | |
| 423 | let label_safe = text::display_safe(&self.label); |
| 424 | let label_max = if label_safe.is_empty() { |
| 425 | 0 |
| 426 | } else { |
| 427 | (width / 3).max(4) |
| 428 | }; |
| 429 | let label_shown = if label_max > 0 { |
| 430 | text::truncate(&label_safe, label_max, ascii).into_owned() |
| 431 | } else { |
| 432 | String::new() |
| 433 | }; |
| 434 | let label_w = text::width(&label_shown); |
| 435 | let summary_w = text::width(&summary_text); |
| 436 | |
| 437 | let lead_gap = if label_w > 0 { 2 } else { 0 }; |
| 438 | let truecolor = |
| 439 | theme.depth() == color::ColorDepth::TrueColor && theme.paints_grounds() && !ascii; |
| 440 | let bracket_overhead = if truecolor { 0 } else { 2 }; |
| 441 | let tail_gap = 2; |
| 442 | let reserved = label_w + lead_gap + bracket_overhead + tail_gap + summary_w; |
| 443 | let bar_cells = width.saturating_sub(reserved); |
| 444 | |
| 445 | let mut x = area.x; |
| 446 | let row_y = area.y; |
| 447 | |
| 448 | if label_w > 0 { |
| 449 | buf.set_stringn( |
| 450 | x, |
| 451 | row_y, |
| 452 | &label_shown, |
| 453 | label_w.min(usize::from(area.right().saturating_sub(x))), |
| 454 | theme.fg(Role::Foreground).add_modifier(Modifier::BOLD), |
| 455 | ); |
| 456 | x = x.saturating_add((label_w + lead_gap) as u16); |
| 457 | } |
| 458 | |
| 459 | if bar_cells >= MIN_BAR && x < area.right() { |
| 460 | let total_eighths = bar_cells * 8; |
| 461 | let mut boundaries: Vec<(usize, usize, Role)> = Vec::new(); |
| 462 | let mut cursor_val = 0.0_f64; |
| 463 | let denom = if self.total.is_finite() && self.total > 0.0 { |
| 464 | self.total |
| 465 | } else { |
| 466 | 0.0 |
| 467 | }; |
| 468 | for seg in self.segments { |
| 469 | if !seg.value.is_finite() || seg.value <= 0.0 || denom <= 0.0 { |
| 470 | continue; |
| 471 | } |
| 472 | let start_e = |
| 473 | ((cursor_val / denom).clamp(0.0, 1.0) * total_eighths as f64).round() as usize; |
| 474 | cursor_val += seg.value; |
| 475 | let mut end_e = |
| 476 | ((cursor_val / denom).clamp(0.0, 1.0) * total_eighths as f64).round() as usize; |
| 477 | if end_e == start_e && start_e < total_eighths { |
| 478 | end_e = (start_e + 1).min(total_eighths); |
| 479 | } |
| 480 | if end_e > start_e { |
| 481 | boundaries.push((start_e, end_e, seg.role)); |
| 482 | } |
| 483 | } |
| 484 | let filled_eighths = boundaries.last().map_or(0, |(_, end, _)| *end); |
| 485 | let frontier_cell = filled_eighths.checked_sub(1).map(|e| e / 8); |
| 486 | let threshold_cell = self.threshold.map(|t| { |
| 487 | ((t * bar_cells as f32).round() as usize).min(bar_cells.saturating_sub(1)) |
| 488 | }); |
| 489 | |
| 490 | if !truecolor { |
| 491 | buf.set_stringn(x, row_y, "[", 1, theme.fg(Role::Muted)); |
| 492 | x = x.saturating_add(1); |
| 493 | } |
| 494 | |
| 495 | let light = theme.caps().appearance == Appearance::Light; |
| 496 | let track_bg = if truecolor { |
| 497 | theme |
| 498 | .color(Role::Surface) |
| 499 | .or_else(|| theme.color(Role::Background)) |
| 500 | .map(|base| color::depth_shade(base, if light { 0.08 } else { 0.28 })) |
| 501 | } else { |
| 502 | None |
| 503 | }; |
| 504 | let pressure_stops = if truecolor { |
| 505 | match ( |
| 506 | theme.color(Role::Accent), |
| 507 | theme.color(Role::Live), |
| 508 | theme.color(Role::Attention), |
| 509 | theme.color(Role::Danger), |
| 510 | ) { |
| 511 | (Some(a), Some(b), Some(c), Some(d)) => Some([a, b, c, d]), |
| 512 | _ => None, |
| 513 | } |
| 514 | } else { |
| 515 | None |
| 516 | }; |
| 517 | let specular_light = theme.color(Role::Foreground); |
| 518 | |
| 519 | for cell_idx in 0..bar_cells { |
| 520 | if x >= area.right() { |
| 521 | break; |
| 522 | } |
| 523 | let cell_start = cell_idx * 8; |
| 524 | let cell_end = cell_start + 8; |
| 525 | |
| 526 | let mut seg_fill = 0usize; |
| 527 | let mut primary_role = Role::Accent; |
| 528 | let mut primary_seg_idx = 0usize; |
| 529 | let mut prev_role: Option<Role> = None; |
| 530 | |
| 531 | for (s_idx, &(s_start, s_end, role)) in boundaries.iter().enumerate() { |
| 532 | let overlap_start = s_start.max(cell_start); |
| 533 | let overlap_end = s_end.min(cell_end); |
| 534 | if overlap_end > overlap_start { |
| 535 | let span = overlap_end - overlap_start; |
| 536 | if seg_fill == 0 { |
| 537 | primary_role = role; |
| 538 | primary_seg_idx = s_idx; |
| 539 | } else { |
| 540 | prev_role = Some(primary_role); |
| 541 | primary_role = role; |
| 542 | primary_seg_idx = s_idx; |
| 543 | } |
| 544 | seg_fill = (seg_fill + span).min(8); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | let is_threshold = threshold_cell == Some(cell_idx); |
| 549 | let is_frontier = frontier_cell == Some(cell_idx); |
| 550 | |
| 551 | let cell = &mut buf[(x, row_y)]; |
| 552 | if seg_fill == 0 { |
| 553 | if is_threshold { |
| 554 | let tick = if ascii { "|" } else { "▕" }; |
| 555 | let mut style = theme.fg(Role::Attention); |
| 556 | if let Some(bg) = track_bg { |
| 557 | style = style.bg(bg); |
| 558 | } |
| 559 | cell.set_symbol(tick).set_style(style); |
| 560 | } else if let Some(bg) = track_bg { |
| 561 | cell.set_symbol(" ").set_style(Style::default().bg(bg)); |
| 562 | } else { |
| 563 | let empty = if ascii { "." } else { "░" }; |
| 564 | cell.set_symbol(empty).set_style(theme.fg(Role::Dim)); |
| 565 | } |
| 566 | } else { |
| 567 | let glyph = if is_threshold && !truecolor { |
| 568 | if ascii { "|" } else { "▕" } |
| 569 | } else if ascii { |
| 570 | METER_ASCII_MARKS[primary_seg_idx % METER_ASCII_MARKS.len()] |
| 571 | } else { |
| 572 | METER_EIGHTHS[seg_fill] |
| 573 | }; |
| 574 | |
| 575 | if truecolor { |
| 576 | let t = cell_idx as f32 / bar_cells.saturating_sub(1).max(1) as f32; |
| 577 | let mut fg_color = if self.pressure_gradient |
| 578 | && let Some(stops) = pressure_stops |
| 579 | { |
| 580 | color::oklab_ramp(&stops, t) |
| 581 | } else if let Some(base) = theme.color(primary_role) { |
| 582 | let lift = 0.06 + 0.14 * t; |
| 583 | if let Some(light_c) = specular_light { |
| 584 | color::mix_oklab(base, light_c, lift) |
| 585 | } else { |
| 586 | base |
| 587 | } |
| 588 | } else { |
| 589 | theme.token(primary_role) |
| 590 | }; |
| 591 | if is_frontier && let Some(light_c) = specular_light { |
| 592 | fg_color = color::specular_lift(fg_color, light_c, 0.34); |
| 593 | } |
| 594 | let mut style = Style::default().fg(fg_color); |
| 595 | if is_threshold && let Some(warn) = theme.color(Role::Attention) { |
| 596 | style = Style::default().fg(warn); |
| 597 | if let Some(bg) = track_bg { |
| 598 | style = style.bg(color::mix_oklab(bg, fg_color, 0.55)); |
| 599 | } |
| 600 | cell.set_symbol("▕").set_style(style); |
| 601 | x = x.saturating_add(1); |
| 602 | continue; |
| 603 | } |
| 604 | if seg_fill < 8 { |
| 605 | if let Some(bg) = track_bg { |
| 606 | style = style.bg(bg); |
| 607 | } |
| 608 | } else if let Some(pr) = prev_role |
| 609 | && let Some(prev_c) = theme.color(pr) |
| 610 | { |
| 611 | style = style.bg(prev_c); |
| 612 | } |
| 613 | cell.set_symbol(glyph).set_style(style); |
| 614 | } else { |
| 615 | let role = if is_threshold { |
| 616 | Role::Attention |
| 617 | } else { |
| 618 | primary_role |
| 619 | }; |
| 620 | cell.set_symbol(glyph).set_style(theme.fg(role)); |
| 621 | } |
| 622 | } |
| 623 | x = x.saturating_add(1); |
| 624 | } |
| 625 | |
| 626 | if !truecolor && x < area.right() { |
| 627 | buf.set_stringn(x, row_y, "]", 1, theme.fg(Role::Muted)); |
| 628 | x = x.saturating_add(1); |
| 629 | } |
| 630 | x = x.saturating_add(tail_gap as u16); |
| 631 | } |
| 632 | |
| 633 | if x < area.right() { |
| 634 | let remaining = usize::from(area.right().saturating_sub(x)); |
| 635 | let shown = text::truncate(&summary_text, remaining, ascii); |
| 636 | buf.set_stringn( |
| 637 | x, |
| 638 | row_y, |
| 639 | &shown, |
| 640 | remaining, |
| 641 | theme.fg(summary_role).add_modifier(Modifier::BOLD), |
| 642 | ); |
| 643 | } |
| 644 | |
| 645 | if self.show_legend && area.height >= 2 && self.total.is_finite() && self.total > 0.0 { |
| 646 | let dot = glyphs::pick(glyphs::CURRENT, ascii); |
| 647 | let mut spans = Vec::new(); |
| 648 | for (idx, seg) in self |
| 649 | .segments |
| 650 | .iter() |
| 651 | .filter(|s| s.value.is_finite() && s.value > 0.0) |
| 652 | .enumerate() |
| 653 | { |
| 654 | if idx > 0 { |
| 655 | spans.push(Span::raw(" ")); |
| 656 | } |
| 657 | let share = ((seg.value / self.total) * 100.0).round() as u32; |
| 658 | spans.push(Span::styled(dot, theme.fg(seg.role))); |
| 659 | spans.push(Span::styled( |
| 660 | format!(" {} ", text::display_safe(&seg.label)), |
| 661 | theme.fg(Role::Muted), |
| 662 | )); |
| 663 | spans.push(Span::styled( |
| 664 | format!("{share}%"), |
| 665 | theme.fg(Role::Foreground), |
| 666 | )); |
| 667 | } |
| 668 | if let Some(t) = self.threshold { |
| 669 | if !spans.is_empty() { |
| 670 | spans.push(Span::raw(" ")); |
| 671 | } |
| 672 | let thresh_pct = (t * 100.0).round() as u32; |
| 673 | let tick = if ascii { "|" } else { "▕" }; |
| 674 | spans.push(Span::styled(tick, theme.fg(Role::Attention))); |
| 675 | spans.push(Span::styled( |
| 676 | format!(" {thresh_pct}%"), |
| 677 | theme.fg(Role::Muted), |
| 678 | )); |
| 679 | } |
| 680 | Line::from(spans).render(Rect::new(area.x, area.y + 1, area.width, 1), buf); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | fn height(&self, _width: u16, _theme: &Theme) -> u16 { |
| 685 | if self.show_legend { 2 } else { 1 } |
| 686 | } |
| 687 | } |
| 688 |