| 1 | //! The two rows beneath Codewhale's composer, extracted from the current |
| 2 | //! TUI's `phase_strip.rs::TidelineFooter` and `infoline.rs::InfoLine`. |
| 3 | //! |
| 4 | //! The posture bar says permission, mode and current activity. The metrics |
| 5 | //! line says model, context and measured numbers. Both take caller facts; |
| 6 | //! neither discovers routes, counts work, starts a clock or dispatches keys. |
| 7 | |
| 8 | use std::borrow::Cow; |
| 9 | |
| 10 | use ratatui::{ |
| 11 | buffer::Buffer, |
| 12 | layout::{Margin, Rect}, |
| 13 | style::{Modifier, Style}, |
| 14 | }; |
| 15 | use unicode_segmentation::UnicodeSegmentation; |
| 16 | |
| 17 | use crate::{Paint, Role, Theme, TuiInk, glyphs, text}; |
| 18 | |
| 19 | const ITEM_JOIN: &str = " "; |
| 20 | const COUNT_JOIN: &str = ", "; |
| 21 | const HELP_GAP: usize = 2; |
| 22 | const SHED_SESSION_CLOCK: u8 = 1; |
| 23 | const SHED_PERMISSION_KEY: u8 = 2; |
| 24 | const SHED_HINT: u8 = 3; |
| 25 | const SHED_COUNTS: u8 = 4; |
| 26 | const SHED_TURN_CLOCK: u8 = 5; |
| 27 | const SHED_CAP_WARNING: u8 = 6; |
| 28 | const SHED_MODE_KEY: u8 = 7; |
| 29 | const SHED_THINKING: u8 = 8; |
| 30 | const SHED_MODE: u8 = 9; |
| 31 | const MAX_SHED: u8 = SHED_MODE; |
| 32 | const COMPACT_SHED: u8 = SHED_TURN_CLOCK; |
| 33 | |
| 34 | /// One caller-authored posture word and its semantic ink. |
| 35 | #[derive(Clone, Debug)] |
| 36 | pub struct PostureFact<'a> { |
| 37 | pub text: Cow<'a, str>, |
| 38 | pub role: Role, |
| 39 | pub ink: Option<TuiInk>, |
| 40 | } |
| 41 | |
| 42 | impl<'a> PostureFact<'a> { |
| 43 | #[must_use] |
| 44 | pub fn new(value: impl Into<Cow<'a, str>>, role: Role) -> Self { |
| 45 | Self { |
| 46 | text: value.into(), |
| 47 | role, |
| 48 | ink: None, |
| 49 | } |
| 50 | } |
| 51 | /// Choose an exact native permission, mode or status ink. |
| 52 | #[must_use] |
| 53 | pub fn ink(mut self, ink: TuiInk) -> Self { |
| 54 | self.ink = Some(ink); |
| 55 | self |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// Codewhale's one-row posture bar, immediately below the composer. |
| 60 | /// |
| 61 | /// Permission is the floor. Optional clocks, keys, hints and counts shed |
| 62 | /// before mode, the live turn clock last of them; the right notice never |
| 63 | /// covers permission or the turn clock. At 80% context, the cap warning |
| 64 | /// takes precedence and keeps its command when only the short form fits. |
| 65 | #[derive(Clone, Debug)] |
| 66 | pub struct PostureBar<'a> { |
| 67 | pub permission: PostureFact<'a>, |
| 68 | pub permission_key: Option<Cow<'a, str>>, |
| 69 | pub mode: Option<PostureFact<'a>>, |
| 70 | pub mode_key: Option<Cow<'a, str>>, |
| 71 | /// Optional reasoning/thinking effort badge, shed after `mode_key` and before `mode`. |
| 72 | pub thinking: Option<PostureFact<'a>>, |
| 73 | pub turn_clock: Option<PostureFact<'a>>, |
| 74 | pub awaiting_decision: bool, |
| 75 | pub counts: Vec<PostureFact<'a>>, |
| 76 | pub session_clock: Option<PostureFact<'a>>, |
| 77 | pub hint: Option<PostureFact<'a>>, |
| 78 | pub context_percent: u8, |
| 79 | pub cap_warning: Cow<'a, str>, |
| 80 | pub right: Option<PostureFact<'a>>, |
| 81 | pub compact: bool, |
| 82 | } |
| 83 | |
| 84 | impl<'a> PostureBar<'a> { |
| 85 | #[must_use] |
| 86 | pub fn new(permission: impl Into<Cow<'a, str>>) -> Self { |
| 87 | Self { |
| 88 | permission: PostureFact::new(permission, Role::Attention).ink(TuiInk::PermissionAsk), |
| 89 | permission_key: None, |
| 90 | mode: None, |
| 91 | mode_key: None, |
| 92 | thinking: None, |
| 93 | turn_clock: None, |
| 94 | awaiting_decision: false, |
| 95 | counts: Vec::new(), |
| 96 | session_clock: None, |
| 97 | hint: None, |
| 98 | context_percent: 0, |
| 99 | cap_warning: Cow::Borrowed("surface soon — /compact"), |
| 100 | right: None, |
| 101 | compact: false, |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /// Construct a posture bar using the canonical `Ask` permission label. |
| 106 | #[must_use] |
| 107 | pub fn ask() -> Self { |
| 108 | Self::new("Ask") |
| 109 | } |
| 110 | |
| 111 | /// Construct a posture bar using the canonical `Accept Edits` permission label. |
| 112 | #[must_use] |
| 113 | pub fn accept_edits() -> Self { |
| 114 | Self::new("Accept Edits").permission_role(Role::Accent) |
| 115 | } |
| 116 | |
| 117 | /// Construct a posture bar using the canonical `Plan` permission label. |
| 118 | #[must_use] |
| 119 | pub fn plan() -> Self { |
| 120 | Self::new("Plan") |
| 121 | .permission_role(Role::Accent) |
| 122 | .permission_ink(TuiInk::ModePlan) |
| 123 | } |
| 124 | |
| 125 | /// Construct a posture bar using the canonical `Auto-Review` permission label. |
| 126 | #[must_use] |
| 127 | pub fn auto_review() -> Self { |
| 128 | Self::new("Auto-Review").permission_ink(TuiInk::PermissionAutoReview) |
| 129 | } |
| 130 | |
| 131 | /// Construct a posture bar using the canonical `Full Access` permission label. |
| 132 | #[must_use] |
| 133 | pub fn full_access() -> Self { |
| 134 | Self::new("Full Access").permission_ink(TuiInk::PermissionFullAccess) |
| 135 | } |
| 136 | |
| 137 | #[must_use] |
| 138 | pub fn permission_role(mut self, role: Role) -> Self { |
| 139 | self.permission.role = role; |
| 140 | self.permission.ink = None; |
| 141 | self |
| 142 | } |
| 143 | |
| 144 | #[must_use] |
| 145 | pub fn permission_ink(mut self, ink: TuiInk) -> Self { |
| 146 | self.permission.ink = Some(ink); |
| 147 | self |
| 148 | } |
| 149 | |
| 150 | #[must_use] |
| 151 | pub fn mode_ink(mut self, value: impl Into<Cow<'a, str>>, ink: TuiInk) -> Self { |
| 152 | self.mode = Some(PostureFact::new(value, ink.fallback_role()).ink(ink)); |
| 153 | self |
| 154 | } |
| 155 | |
| 156 | #[must_use] |
| 157 | pub fn permission_key(mut self, value: impl Into<Cow<'a, str>>) -> Self { |
| 158 | self.permission_key = Some(value.into()); |
| 159 | self |
| 160 | } |
| 161 | |
| 162 | #[must_use] |
| 163 | pub fn mode(mut self, value: impl Into<Cow<'a, str>>, role: Role) -> Self { |
| 164 | self.mode = Some(PostureFact::new(value, role)); |
| 165 | self |
| 166 | } |
| 167 | |
| 168 | #[must_use] |
| 169 | pub fn mode_key(mut self, value: impl Into<Cow<'a, str>>) -> Self { |
| 170 | self.mode_key = Some(value.into()); |
| 171 | self |
| 172 | } |
| 173 | |
| 174 | /// Attach a thinking/reasoning effort indicator, shed after `mode_key` and before `mode`. |
| 175 | #[must_use] |
| 176 | pub fn thinking(mut self, value: impl Into<Cow<'a, str>>, role: Role) -> Self { |
| 177 | self.thinking = Some(PostureFact::new(value, role)); |
| 178 | self |
| 179 | } |
| 180 | |
| 181 | #[must_use] |
| 182 | pub fn turn_clock(mut self, value: impl Into<Cow<'a, str>>, role: Role) -> Self { |
| 183 | self.turn_clock = Some(PostureFact::new(value, role)); |
| 184 | self |
| 185 | } |
| 186 | |
| 187 | #[must_use] |
| 188 | pub fn session_clock(mut self, value: impl Into<Cow<'a, str>>, role: Role) -> Self { |
| 189 | self.session_clock = Some(PostureFact::new(value, role)); |
| 190 | self |
| 191 | } |
| 192 | |
| 193 | #[must_use] |
| 194 | pub fn counts(mut self, values: Vec<PostureFact<'a>>) -> Self { |
| 195 | self.counts = values; |
| 196 | self |
| 197 | } |
| 198 | |
| 199 | #[must_use] |
| 200 | pub fn hint(mut self, value: impl Into<Cow<'a, str>>, role: Role) -> Self { |
| 201 | self.hint = Some(PostureFact::new(value, role)); |
| 202 | self |
| 203 | } |
| 204 | |
| 205 | #[must_use] |
| 206 | pub fn right(mut self, value: impl Into<Cow<'a, str>>, role: Role) -> Self { |
| 207 | self.right = Some(PostureFact::new(value, role)); |
| 208 | self |
| 209 | } |
| 210 | |
| 211 | #[must_use] |
| 212 | pub fn context_percent(mut self, percent: u8) -> Self { |
| 213 | self.context_percent = percent.min(100); |
| 214 | self |
| 215 | } |
| 216 | |
| 217 | /// Localize the native cap warning without changing its priority. |
| 218 | #[must_use] |
| 219 | pub fn cap_warning(mut self, value: impl Into<Cow<'a, str>>) -> Self { |
| 220 | self.cap_warning = value.into(); |
| 221 | self |
| 222 | } |
| 223 | |
| 224 | #[must_use] |
| 225 | pub fn compact(mut self, compact: bool) -> Self { |
| 226 | self.compact = compact; |
| 227 | self |
| 228 | } |
| 229 | |
| 230 | fn at_context_cap(&self) -> bool { |
| 231 | self.context_percent >= 80 |
| 232 | } |
| 233 | |
| 234 | fn items(&self, shed: u8, ascii: bool, cap_warning: &str) -> Vec<PostureItem> { |
| 235 | let mut items = vec![PostureItem { |
| 236 | text: format!( |
| 237 | "{} {}", |
| 238 | project(glyphs::CURRENT, ascii), |
| 239 | project(&self.permission.text, ascii) |
| 240 | ), |
| 241 | key: self |
| 242 | .permission_key |
| 243 | .as_deref() |
| 244 | .filter(|_| shed < SHED_PERMISSION_KEY) |
| 245 | .map(|key| format!(" {}", project(key, ascii))), |
| 246 | role: self.permission.role, |
| 247 | ink: self.permission.ink, |
| 248 | bold: true, |
| 249 | joined: false, |
| 250 | count_index: None, |
| 251 | turn_clock: false, |
| 252 | }]; |
| 253 | if let Some(mode) = self.mode.as_ref().filter(|_| shed < SHED_MODE) { |
| 254 | items.push(PostureItem { |
| 255 | text: project(&mode.text, ascii), |
| 256 | key: self |
| 257 | .mode_key |
| 258 | .as_deref() |
| 259 | .filter(|_| shed < SHED_MODE_KEY) |
| 260 | .map(|key| format!(" ({})", project(key, ascii))), |
| 261 | role: mode.role, |
| 262 | ink: mode.ink, |
| 263 | bold: false, |
| 264 | joined: false, |
| 265 | count_index: None, |
| 266 | turn_clock: false, |
| 267 | }); |
| 268 | } |
| 269 | if let Some(thinking) = self.thinking.as_ref().filter(|_| shed < SHED_THINKING) { |
| 270 | items.push(PostureItem::fact(thinking, ascii)); |
| 271 | } |
| 272 | let clock_rung = if self.awaiting_decision { |
| 273 | SHED_MODE_KEY |
| 274 | } else { |
| 275 | SHED_TURN_CLOCK |
| 276 | }; |
| 277 | if let Some(clock) = self.turn_clock.as_ref().filter(|_| shed < clock_rung) { |
| 278 | let mut item = PostureItem::fact(clock, ascii); |
| 279 | item.turn_clock = true; |
| 280 | items.push(item); |
| 281 | } |
| 282 | if shed < SHED_COUNTS { |
| 283 | for (index, count) in self.counts.iter().enumerate() { |
| 284 | let count_text = project(&count.text, ascii); |
| 285 | let (value, key) = match count_text.strip_suffix(" (Ctrl+])") { |
| 286 | Some(label) => (label.to_owned(), Some(" (Ctrl+])".to_owned())), |
| 287 | None => (count_text, None), |
| 288 | }; |
| 289 | items.push(PostureItem { |
| 290 | text: value, |
| 291 | key, |
| 292 | role: count.role, |
| 293 | ink: count.ink, |
| 294 | bold: false, |
| 295 | joined: index > 0, |
| 296 | count_index: Some(index), |
| 297 | turn_clock: false, |
| 298 | }); |
| 299 | } |
| 300 | } |
| 301 | if let Some(clock) = self |
| 302 | .session_clock |
| 303 | .as_ref() |
| 304 | .filter(|_| shed < SHED_SESSION_CLOCK) |
| 305 | { |
| 306 | items.push(PostureItem::fact(clock, ascii)); |
| 307 | } |
| 308 | let hint_rung = if self.at_context_cap() { |
| 309 | SHED_CAP_WARNING |
| 310 | } else { |
| 311 | SHED_HINT |
| 312 | }; |
| 313 | if shed < hint_rung { |
| 314 | if self.at_context_cap() { |
| 315 | items.push(PostureItem::fact( |
| 316 | &PostureFact::new(format!("▲ {cap_warning}"), Role::Attention) |
| 317 | .ink(TuiInk::Warning), |
| 318 | ascii, |
| 319 | )); |
| 320 | } else if let Some(hint) = &self.hint { |
| 321 | items.push(PostureItem::fact(hint, ascii)); |
| 322 | } |
| 323 | } |
| 324 | items |
| 325 | } |
| 326 | |
| 327 | fn layout(&self, area: Rect, theme: &Theme) -> Option<PostureLayout> { |
| 328 | if area.width < 8 || area.height == 0 { |
| 329 | return None; |
| 330 | } |
| 331 | let area = area.inner(Margin::new(1, 0)); |
| 332 | let ascii = theme.ascii(); |
| 333 | let width = usize::from(area.width); |
| 334 | let permission_width = left_width(&self.items(MAX_SHED, ascii, &self.cap_warning)); |
| 335 | let join = text::width(ITEM_JOIN); |
| 336 | let warning_width = |
| 337 | |value: &str| join + text::width(&project(&format!("▲ {value}"), ascii)); |
| 338 | let urgent_right = self |
| 339 | .right |
| 340 | .as_ref() |
| 341 | .filter(|fact| { |
| 342 | matches!(fact.role, Role::Danger | Role::Attention) |
| 343 | || fact.ink == Some(TuiInk::Warning) |
| 344 | }) |
| 345 | .map_or(0, |fact| 1 + text::width(&project(&fact.text, ascii))); |
| 346 | let clock_width = self |
| 347 | .turn_clock |
| 348 | .as_ref() |
| 349 | .map_or(0, |clock| join + text::width(&project(&clock.text, ascii))); |
| 350 | let decision_width = if self.awaiting_decision { |
| 351 | clock_width |
| 352 | } else { |
| 353 | 0 |
| 354 | }; |
| 355 | let mut warning = self.cap_warning.as_ref(); |
| 356 | let mut floor = permission_width; |
| 357 | if self.at_context_cap() { |
| 358 | if permission_width + warning_width(warning) + urgent_right + decision_width > width |
| 359 | && let Some(action) = warning |
| 360 | .split_whitespace() |
| 361 | .last() |
| 362 | .filter(|s| s.starts_with('/')) |
| 363 | { |
| 364 | warning = action; |
| 365 | } |
| 366 | floor += decision_width; |
| 367 | if !self.awaiting_decision |
| 368 | || urgent_right == 0 |
| 369 | || floor + warning_width(warning) + urgent_right <= width |
| 370 | { |
| 371 | floor += warning_width(warning); |
| 372 | } |
| 373 | } else if !self.compact || self.awaiting_decision { |
| 374 | floor += clock_width; |
| 375 | } |
| 376 | let right = self.right.as_ref().and_then(|fact| { |
| 377 | let value = project(&fact.text, ascii); |
| 378 | let room = width.saturating_sub(floor + 1); |
| 379 | if room == 0 || (self.at_context_cap() && text::width(&value) > room) { |
| 380 | return None; |
| 381 | } |
| 382 | Some((clip_native(&value, room), fact.role, fact.ink)) |
| 383 | }); |
| 384 | let right_width = right |
| 385 | .as_ref() |
| 386 | .map_or(0, |(value, _, _)| text::width(value) + 1); |
| 387 | let budget = width.saturating_sub(right_width); |
| 388 | let first = if self.compact { COMPACT_SHED } else { 0 }; |
| 389 | let items = (first..=MAX_SHED) |
| 390 | .map(|shed| self.items(shed, ascii, warning)) |
| 391 | .find(|items| left_width(items) <= budget) |
| 392 | .unwrap_or_else(|| self.items(MAX_SHED, ascii, warning)); |
| 393 | Some(PostureLayout { |
| 394 | area, |
| 395 | budget, |
| 396 | items, |
| 397 | right, |
| 398 | }) |
| 399 | } |
| 400 | |
| 401 | /// Geometry of the live-count affordances in the painted row. |
| 402 | /// Pass the same area used for painting, intersected with the buffer. |
| 403 | #[must_use] |
| 404 | pub fn count_hitboxes(&self, area: Rect, theme: &Theme) -> Vec<(usize, Rect)> { |
| 405 | let Some(layout) = self.layout(area, theme) else { |
| 406 | return Vec::new(); |
| 407 | }; |
| 408 | layout.count_hitboxes(theme.ascii()) |
| 409 | } |
| 410 | |
| 411 | #[must_use] |
| 412 | pub fn turn_clock_hitbox(&self, area: Rect, theme: &Theme) -> Option<Rect> { |
| 413 | self.layout(area, theme)?.turn_clock_hitbox(theme.ascii()) |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | struct PostureItem { |
| 418 | text: String, |
| 419 | key: Option<String>, |
| 420 | role: Role, |
| 421 | ink: Option<TuiInk>, |
| 422 | bold: bool, |
| 423 | joined: bool, |
| 424 | count_index: Option<usize>, |
| 425 | turn_clock: bool, |
| 426 | } |
| 427 | |
| 428 | impl PostureItem { |
| 429 | fn fact(fact: &PostureFact<'_>, ascii: bool) -> Self { |
| 430 | Self { |
| 431 | text: project(&fact.text, ascii), |
| 432 | key: None, |
| 433 | role: fact.role, |
| 434 | ink: fact.ink, |
| 435 | bold: false, |
| 436 | joined: false, |
| 437 | count_index: None, |
| 438 | turn_clock: false, |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | fn width(&self) -> usize { |
| 443 | text::width(&self.text) + self.key.as_deref().map_or(0, text::width) |
| 444 | } |
| 445 | |
| 446 | fn separator(&self) -> &'static str { |
| 447 | if self.joined { |
| 448 | COUNT_JOIN |
| 449 | } else if self.turn_clock { |
| 450 | " │ " |
| 451 | } else { |
| 452 | ITEM_JOIN |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | fn left_width(items: &[PostureItem]) -> usize { |
| 458 | items.iter().map(PostureItem::width).sum::<usize>() |
| 459 | + items |
| 460 | .iter() |
| 461 | .skip(1) |
| 462 | .map(|item| text::width(item.separator())) |
| 463 | .sum::<usize>() |
| 464 | } |
| 465 | |
| 466 | struct PostureLayout { |
| 467 | area: Rect, |
| 468 | budget: usize, |
| 469 | items: Vec<PostureItem>, |
| 470 | right: Option<(String, Role, Option<TuiInk>)>, |
| 471 | } |
| 472 | |
| 473 | impl PostureLayout { |
| 474 | fn clip(&self, x: usize, value: &str) -> String { |
| 475 | clip_native( |
| 476 | value, |
| 477 | (usize::from(self.area.x) + self.budget).saturating_sub(x), |
| 478 | ) |
| 479 | } |
| 480 | |
| 481 | fn count_hitboxes(&self, ascii: bool) -> Vec<(usize, Rect)> { |
| 482 | let mut out = Vec::new(); |
| 483 | let mut x = usize::from(self.area.x); |
| 484 | for (index, item) in self.items.iter().enumerate() { |
| 485 | if index > 0 { |
| 486 | x += text::width(&project(item.separator(), ascii)); |
| 487 | } |
| 488 | let value = self.clip(x, &item.text); |
| 489 | let key = item |
| 490 | .key |
| 491 | .as_deref() |
| 492 | .map(|key| self.clip(x + text::width(&item.text), key)) |
| 493 | .unwrap_or_default(); |
| 494 | if let Some(index) = item.count_index |
| 495 | && !value.is_empty() |
| 496 | { |
| 497 | out.push(( |
| 498 | index, |
| 499 | Rect::new( |
| 500 | x as u16, |
| 501 | self.area.y, |
| 502 | (text::width(&value) + text::width(&key)) as u16, |
| 503 | 1, |
| 504 | ), |
| 505 | )); |
| 506 | } |
| 507 | x += item.width(); |
| 508 | } |
| 509 | out |
| 510 | } |
| 511 | |
| 512 | fn turn_clock_hitbox(&self, ascii: bool) -> Option<Rect> { |
| 513 | let mut x = usize::from(self.area.x); |
| 514 | for (index, item) in self.items.iter().enumerate() { |
| 515 | if index > 0 { |
| 516 | x += text::width(&project(item.separator(), ascii)); |
| 517 | } |
| 518 | if item.turn_clock { |
| 519 | let width = text::width(&self.clip(x, &item.text)); |
| 520 | return (width > 0 && x <= usize::from(u16::MAX)) |
| 521 | .then(|| Rect::new(x as u16, self.area.y, width as u16, 1)); |
| 522 | } |
| 523 | x += item.width(); |
| 524 | } |
| 525 | None |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | impl Paint for PostureBar<'_> { |
| 530 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 531 | let Some(layout) = self.layout(area.intersection(buf.area), theme) else { |
| 532 | return; |
| 533 | }; |
| 534 | let mut x = usize::from(layout.area.x); |
| 535 | for (index, item) in layout.items.iter().enumerate() { |
| 536 | if index > 0 { |
| 537 | let separator = project(item.separator(), theme.ascii()); |
| 538 | put( |
| 539 | layout.area, |
| 540 | buf, |
| 541 | x, |
| 542 | &layout.clip(x, &separator), |
| 543 | theme.fg(Role::Dim), |
| 544 | ); |
| 545 | x += text::width(&separator); |
| 546 | } |
| 547 | let base = item |
| 548 | .ink |
| 549 | .map_or_else(|| theme.fg(item.role), |ink| theme.tui_ink(ink)); |
| 550 | let style = if item.bold { |
| 551 | theme.fg(Role::Foreground).add_modifier(Modifier::BOLD) |
| 552 | } else if item.turn_clock { |
| 553 | base.add_modifier(Modifier::BOLD) |
| 554 | } else { |
| 555 | base |
| 556 | }; |
| 557 | put(layout.area, buf, x, &layout.clip(x, &item.text), style); |
| 558 | if item.bold { |
| 559 | let marker = glyphs::pick(glyphs::CURRENT, theme.ascii()); |
| 560 | put( |
| 561 | layout.area, |
| 562 | buf, |
| 563 | x, |
| 564 | &layout.clip(x, marker), |
| 565 | base.add_modifier(Modifier::BOLD), |
| 566 | ); |
| 567 | } |
| 568 | if let Some(key) = &item.key { |
| 569 | let key_x = x + text::width(&item.text); |
| 570 | put( |
| 571 | layout.area, |
| 572 | buf, |
| 573 | key_x, |
| 574 | &layout.clip(key_x, key), |
| 575 | theme.fg(Role::Hint), |
| 576 | ); |
| 577 | } |
| 578 | x += item.width(); |
| 579 | } |
| 580 | if let Some((value, role, ink)) = &layout.right |
| 581 | && !value.is_empty() |
| 582 | { |
| 583 | let x = usize::from(layout.area.right()).saturating_sub(text::width(value)); |
| 584 | let style = ink.map_or_else(|| theme.fg(*role), |ink| theme.tui_ink(ink)); |
| 585 | put(layout.area, buf, x, value, style); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | fn height(&self, width: u16, _theme: &Theme) -> u16 { |
| 590 | u16::from(width > 0) |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | /// Identity and native shedding priority of a metrics-line segment. |
| 595 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 596 | pub enum MetricKind { |
| 597 | Model, |
| 598 | Context, |
| 599 | Cost, |
| 600 | BillingTier, |
| 601 | OutputTokens, |
| 602 | Ttft, |
| 603 | Rate, |
| 604 | Cache, |
| 605 | Balance, |
| 606 | Goal, |
| 607 | Workspace, |
| 608 | GitBranch, |
| 609 | Update, |
| 610 | /// Active thinking/reasoning effort indicator. |
| 611 | Thinking, |
| 612 | } |
| 613 | |
| 614 | impl MetricKind { |
| 615 | const SHED_BEFORE_HELP: u8 = 7; |
| 616 | |
| 617 | /// Higher priorities shed first; model and context never shed. |
| 618 | #[must_use] |
| 619 | pub const fn shed_priority(self) -> u8 { |
| 620 | match self { |
| 621 | Self::Cache => 6, |
| 622 | Self::Rate | Self::Ttft | Self::Cost => 5, |
| 623 | Self::OutputTokens | Self::BillingTier => 7, |
| 624 | Self::Balance | Self::Workspace | Self::GitBranch | Self::Thinking => 4, |
| 625 | Self::Goal | Self::Update => 3, |
| 626 | Self::Model | Self::Context => 0, |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | /// A reported number or route name. Empty labels paint just the value. |
| 632 | #[derive(Clone, Debug)] |
| 633 | pub struct MetricSegment<'a> { |
| 634 | pub kind: MetricKind, |
| 635 | pub label: Cow<'a, str>, |
| 636 | pub value: Cow<'a, str>, |
| 637 | pub role: Role, |
| 638 | } |
| 639 | |
| 640 | impl<'a> MetricSegment<'a> { |
| 641 | #[must_use] |
| 642 | pub fn new( |
| 643 | kind: MetricKind, |
| 644 | label: impl Into<Cow<'a, str>>, |
| 645 | value: impl Into<Cow<'a, str>>, |
| 646 | ) -> Self { |
| 647 | Self { |
| 648 | kind, |
| 649 | label: label.into(), |
| 650 | value: value.into(), |
| 651 | role: Role::Muted, |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | #[must_use] |
| 656 | pub fn role(mut self, role: Role) -> Self { |
| 657 | self.role = role; |
| 658 | self |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /// A segment's exact visible cells, including the space between label and value. |
| 663 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 664 | pub struct MetricHitbox { |
| 665 | pub kind: MetricKind, |
| 666 | pub area: Rect, |
| 667 | } |
| 668 | |
| 669 | /// Codewhale's one-row model, context and session metrics line. |
| 670 | /// |
| 671 | /// Missing measurements are omitted by leaving out their segments. The |
| 672 | /// native shed ladder preserves model and context, removes secondary counts |
| 673 | /// before help, and clips at the right edge only below the identity floor. |
| 674 | #[derive(Clone, Debug)] |
| 675 | pub struct MetricsLine<'a> { |
| 676 | pub segments: Vec<MetricSegment<'a>>, |
| 677 | pub help_hint: Cow<'a, str>, |
| 678 | pub hovered: Option<MetricKind>, |
| 679 | pub compact: bool, |
| 680 | } |
| 681 | |
| 682 | impl<'a> MetricsLine<'a> { |
| 683 | #[must_use] |
| 684 | pub fn project_value(value: &str, ascii: bool) -> String { |
| 685 | project(value, ascii) |
| 686 | } |
| 687 | |
| 688 | #[must_use] |
| 689 | pub fn new(segments: Vec<MetricSegment<'a>>) -> Self { |
| 690 | Self { |
| 691 | segments, |
| 692 | help_hint: Cow::Borrowed(""), |
| 693 | hovered: None, |
| 694 | compact: false, |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | #[must_use] |
| 699 | pub fn help_hint(mut self, value: impl Into<Cow<'a, str>>) -> Self { |
| 700 | self.help_hint = value.into(); |
| 701 | self |
| 702 | } |
| 703 | |
| 704 | #[must_use] |
| 705 | pub fn hovered(mut self, kind: MetricKind) -> Self { |
| 706 | self.hovered = Some(kind); |
| 707 | self |
| 708 | } |
| 709 | |
| 710 | #[must_use] |
| 711 | pub fn compact(mut self, compact: bool) -> Self { |
| 712 | self.compact = compact; |
| 713 | self |
| 714 | } |
| 715 | |
| 716 | fn layout(&self, area: Rect, theme: &Theme) -> MetricLayout { |
| 717 | let ascii = theme.ascii(); |
| 718 | let help = project(&self.help_hint, ascii); |
| 719 | let mut kept: Vec<PreparedMetric> = self |
| 720 | .segments |
| 721 | .iter() |
| 722 | .filter(|segment| { |
| 723 | !self.compact || segment.kind.shed_priority() < MetricKind::SHED_BEFORE_HELP |
| 724 | }) |
| 725 | .map(|segment| PreparedMetric { |
| 726 | kind: segment.kind, |
| 727 | label: project(&segment.label, ascii), |
| 728 | value: project(&segment.value, ascii), |
| 729 | role: segment.role, |
| 730 | }) |
| 731 | .collect(); |
| 732 | let left_width = |segments: &[PreparedMetric]| -> usize { |
| 733 | segments.iter().map(PreparedMetric::width).sum::<usize>() |
| 734 | + text::width(ITEM_JOIN) * segments.len().saturating_sub(1) |
| 735 | }; |
| 736 | let needed = |left: usize, show_help: bool| -> usize { |
| 737 | left + if show_help && !help.is_empty() { |
| 738 | HELP_GAP + text::width(&help) |
| 739 | } else { |
| 740 | 0 |
| 741 | } |
| 742 | }; |
| 743 | let sheddable = |segments: &[PreparedMetric], min_priority: u8| -> Option<usize> { |
| 744 | segments |
| 745 | .iter() |
| 746 | .enumerate() |
| 747 | .filter(|(_, segment)| segment.kind.shed_priority() >= min_priority.max(1)) |
| 748 | .max_by_key(|(_, segment)| segment.kind.shed_priority()) |
| 749 | .map(|(index, _)| index) |
| 750 | }; |
| 751 | let mut show_help = !help.is_empty() && !self.compact; |
| 752 | while needed(left_width(&kept), show_help) > usize::from(area.width) { |
| 753 | if let Some(index) = sheddable(&kept, MetricKind::SHED_BEFORE_HELP) { |
| 754 | kept.remove(index); |
| 755 | } else if show_help { |
| 756 | show_help = false; |
| 757 | } else if let Some(index) = sheddable(&kept, 1) { |
| 758 | kept.remove(index); |
| 759 | } else { |
| 760 | break; |
| 761 | } |
| 762 | } |
| 763 | MetricLayout { |
| 764 | kept, |
| 765 | help: show_help.then_some(help), |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | /// Geometry for all surviving readings. Model, context and update acquire |
| 770 | /// hover styling; hosts choose which readings dispatch an action. |
| 771 | /// Use the same clipped area as painting when the buffer is smaller. |
| 772 | #[must_use] |
| 773 | pub fn hitboxes(&self, area: Rect, theme: &Theme) -> Vec<MetricHitbox> { |
| 774 | if area.is_empty() { |
| 775 | return Vec::new(); |
| 776 | } |
| 777 | let layout = self.layout(area, theme); |
| 778 | let mut x = usize::from(area.x); |
| 779 | let mut out = Vec::new(); |
| 780 | let right = usize::from(area.right()); |
| 781 | for (index, segment) in layout.kept.iter().enumerate() { |
| 782 | if index > 0 { |
| 783 | x += text::width(ITEM_JOIN); |
| 784 | } |
| 785 | let end = (x + segment.width()).min(right); |
| 786 | if x < end { |
| 787 | out.push(MetricHitbox { |
| 788 | kind: segment.kind, |
| 789 | area: Rect::new(x as u16, area.y, (end - x) as u16, 1), |
| 790 | }); |
| 791 | } |
| 792 | x += segment.width(); |
| 793 | } |
| 794 | out |
| 795 | } |
| 796 | |
| 797 | #[must_use] |
| 798 | pub fn context_hitbox(&self, area: Rect, theme: &Theme) -> Option<Rect> { |
| 799 | self.hitboxes(area, theme) |
| 800 | .into_iter() |
| 801 | .find(|hit| hit.kind == MetricKind::Context) |
| 802 | .map(|hit| hit.area) |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | struct PreparedMetric { |
| 807 | kind: MetricKind, |
| 808 | label: String, |
| 809 | value: String, |
| 810 | role: Role, |
| 811 | } |
| 812 | |
| 813 | impl PreparedMetric { |
| 814 | fn width(&self) -> usize { |
| 815 | text::width(&self.value) |
| 816 | + if self.label.is_empty() { |
| 817 | 0 |
| 818 | } else { |
| 819 | text::width(&self.label) + 1 |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | struct MetricLayout { |
| 825 | kept: Vec<PreparedMetric>, |
| 826 | help: Option<String>, |
| 827 | } |
| 828 | |
| 829 | impl Paint for MetricsLine<'_> { |
| 830 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 831 | let area = area.intersection(buf.area); |
| 832 | if area.is_empty() { |
| 833 | return; |
| 834 | } |
| 835 | let layout = self.layout(area, theme); |
| 836 | let mut x = usize::from(area.x); |
| 837 | for (index, segment) in layout.kept.iter().enumerate() { |
| 838 | if index > 0 { |
| 839 | put(area, buf, x, ITEM_JOIN, theme.fg(Role::Dim)); |
| 840 | x += text::width(ITEM_JOIN); |
| 841 | } |
| 842 | let hovered = matches!( |
| 843 | segment.kind, |
| 844 | MetricKind::Model | MetricKind::Context | MetricKind::Update |
| 845 | ) && self.hovered == Some(segment.kind); |
| 846 | let style = if hovered { |
| 847 | theme |
| 848 | .fg(segment.role) |
| 849 | .add_modifier(Modifier::BOLD | Modifier::UNDERLINED) |
| 850 | } else { |
| 851 | theme.fg(segment.role) |
| 852 | }; |
| 853 | if !segment.label.is_empty() { |
| 854 | let label_role = if matches!(segment.role, Role::Danger | Role::Attention) { |
| 855 | segment.role |
| 856 | } else { |
| 857 | Role::Muted |
| 858 | }; |
| 859 | put(area, buf, x, &segment.label, theme.fg(label_role)); |
| 860 | x += text::width(&segment.label) + 1; |
| 861 | } |
| 862 | put(area, buf, x, &segment.value, style); |
| 863 | x += text::width(&segment.value); |
| 864 | } |
| 865 | if let Some(help) = layout.help { |
| 866 | let x = usize::from(area.right()).saturating_sub(text::width(&help)); |
| 867 | put(area, buf, x, &help, theme.fg(Role::Hint)); |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | fn height(&self, width: u16, _theme: &Theme) -> u16 { |
| 872 | u16::from(width > 0) |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | // Native punctuation projection, with the kit's existing text sanitation. |
| 877 | // Authored glyphs change in ASCII mode; language text stays intact. |
| 878 | fn project(value: &str, ascii: bool) -> String { |
| 879 | let safe = text::display_safe(value); |
| 880 | if !ascii { |
| 881 | return safe.into_owned(); |
| 882 | } |
| 883 | if let Some(fallback) = glyphs::ascii_fallback(&safe) { |
| 884 | return fallback.to_owned(); |
| 885 | } |
| 886 | safe.chars() |
| 887 | .map(|ch| { |
| 888 | let mut bytes = [0; 4]; |
| 889 | glyphs::ascii_fallback(ch.encode_utf8(&mut bytes)) |
| 890 | .map(str::to_owned) |
| 891 | .unwrap_or_else(|| ch.to_string()) |
| 892 | }) |
| 893 | .collect() |
| 894 | } |
| 895 | |
| 896 | // phase_strip uses ui_text::truncate_line_to_width, whose ellipsis is three |
| 897 | // ASCII cells even on Unicode terminals and whose tiny budgets have no dot. |
| 898 | fn clip_native(value: &str, max: usize) -> String { |
| 899 | if text::width(value) <= max { |
| 900 | return value.to_owned(); |
| 901 | } |
| 902 | let limit = max.saturating_sub(if max > 3 { 3 } else { 0 }); |
| 903 | let mut out = String::new(); |
| 904 | let mut used = 0; |
| 905 | for grapheme in value.graphemes(true) { |
| 906 | let width = text::width(grapheme); |
| 907 | if used + width > limit { |
| 908 | break; |
| 909 | } |
| 910 | out.push_str(grapheme); |
| 911 | used += width; |
| 912 | } |
| 913 | if max > 3 { |
| 914 | out.push_str("..."); |
| 915 | } |
| 916 | out |
| 917 | } |
| 918 | |
| 919 | fn put(area: Rect, buf: &mut Buffer, x: usize, value: &str, style: Style) { |
| 920 | let right = usize::from(area.right()); |
| 921 | if x >= usize::from(area.x) && x < right && !value.is_empty() { |
| 922 | buf.set_stringn(x as u16, area.y, value, right - x, style); |
| 923 | } |
| 924 | } |
| 925 |