| 1 | //! Codewhale's work dock: Tasks, Fleet, Jobs, Terminal, Files, Notes, Context, Git, Cost. |
| 2 | //! |
| 3 | //! Extracted from `CodeWhale/crates/tui/src/tui/work_surface/{model,input, |
| 4 | //! render/{mod,layout,rows}}.rs` at `a79ce5c4d5ed1a5f7032185710c27343a900351c`. |
| 5 | //! The host supplies rows and dispatches actions; the dock retains the native |
| 6 | //! tab fit, row columns, placement, goal header, and overflow behavior. |
| 7 | |
| 8 | use std::borrow::Cow; |
| 9 | |
| 10 | use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers}; |
| 11 | use ratatui::{ |
| 12 | buffer::Buffer, |
| 13 | layout::Rect, |
| 14 | style::{Modifier, Style}, |
| 15 | text::{Line, Span}, |
| 16 | }; |
| 17 | |
| 18 | use crate::{Paint, Role, Theme, TuiInk, glyphs, text}; |
| 19 | |
| 20 | use super::workbench::row; |
| 21 | |
| 22 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] |
| 23 | pub enum WorkbarPanel { |
| 24 | #[default] |
| 25 | Tasks, |
| 26 | Fleet, |
| 27 | Jobs, |
| 28 | Terminal, |
| 29 | Files, |
| 30 | Notes, |
| 31 | Context, |
| 32 | Git, |
| 33 | Cost, |
| 34 | } |
| 35 | |
| 36 | impl WorkbarPanel { |
| 37 | pub const ORDER: [Self; 9] = [ |
| 38 | Self::Tasks, |
| 39 | Self::Fleet, |
| 40 | Self::Jobs, |
| 41 | Self::Terminal, |
| 42 | Self::Files, |
| 43 | Self::Notes, |
| 44 | Self::Context, |
| 45 | Self::Git, |
| 46 | Self::Cost, |
| 47 | ]; |
| 48 | |
| 49 | #[must_use] |
| 50 | pub const fn label(self) -> &'static str { |
| 51 | match self { |
| 52 | Self::Tasks => "Tasks", |
| 53 | Self::Fleet => "Fleet", |
| 54 | Self::Jobs => "Jobs", |
| 55 | Self::Terminal => "Terminal", |
| 56 | Self::Files => "Files", |
| 57 | Self::Notes => "Notes", |
| 58 | Self::Context => "Context", |
| 59 | Self::Git => "Git", |
| 60 | Self::Cost => "Cost", |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | #[must_use] |
| 65 | pub const fn empty_label(self) -> &'static str { |
| 66 | match self { |
| 67 | Self::Tasks => "no to-dos yet", |
| 68 | Self::Fleet => "no agents have run this session", |
| 69 | Self::Jobs => "nothing running in the background", |
| 70 | Self::Terminal => "", |
| 71 | Self::Files => "no files touched this session", |
| 72 | Self::Notes => "/note add <text> to keep a note", |
| 73 | Self::Context => "context budget unknown", |
| 74 | Self::Git => "reading git status…", |
| 75 | Self::Cost => "no priced turns yet", |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | #[must_use] |
| 80 | pub fn next(self) -> Self { |
| 81 | Self::ORDER[(self.index() + 1) % Self::ORDER.len()] |
| 82 | } |
| 83 | |
| 84 | #[must_use] |
| 85 | pub fn prev(self) -> Self { |
| 86 | Self::ORDER[(self.index() + Self::ORDER.len() - 1) % Self::ORDER.len()] |
| 87 | } |
| 88 | |
| 89 | fn index(self) -> usize { |
| 90 | Self::ORDER |
| 91 | .iter() |
| 92 | .position(|panel| *panel == self) |
| 93 | .unwrap_or(0) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 98 | pub enum WorkbarPlacement { |
| 99 | #[default] |
| 100 | Bottom, |
| 101 | Top, |
| 102 | Left, |
| 103 | Right, |
| 104 | Off, |
| 105 | } |
| 106 | |
| 107 | impl WorkbarPlacement { |
| 108 | #[must_use] |
| 109 | pub const fn is_strip(self) -> bool { |
| 110 | matches!(self, Self::Top | Self::Bottom) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 115 | pub enum WorkbarTone { |
| 116 | Heading, |
| 117 | Live, |
| 118 | Attention, |
| 119 | Failure, |
| 120 | Success, |
| 121 | #[default] |
| 122 | Muted, |
| 123 | } |
| 124 | |
| 125 | impl WorkbarTone { |
| 126 | fn style(self, theme: &Theme) -> Style { |
| 127 | match self { |
| 128 | Self::Heading | Self::Muted => theme.fg(Role::Muted), |
| 129 | Self::Live => theme.tui_ink(TuiInk::Working), |
| 130 | Self::Success => theme.tui_ink(TuiInk::Success), |
| 131 | Self::Attention => theme.tui_ink(TuiInk::Warning), |
| 132 | Self::Failure => theme.fg(Role::Danger), |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 138 | pub struct WorkbarTab { |
| 139 | pub panel: WorkbarPanel, |
| 140 | /// `None` for fact views, matching the native unbadged Context/Git/Cost. |
| 141 | pub count: Option<usize>, |
| 142 | } |
| 143 | |
| 144 | impl WorkbarTab { |
| 145 | #[must_use] |
| 146 | pub const fn new(panel: WorkbarPanel) -> Self { |
| 147 | Self { panel, count: None } |
| 148 | } |
| 149 | |
| 150 | #[must_use] |
| 151 | pub const fn count(mut self, count: usize) -> Self { |
| 152 | self.count = if matches!( |
| 153 | self.panel, |
| 154 | WorkbarPanel::Context | WorkbarPanel::Git | WorkbarPanel::Cost |
| 155 | ) { |
| 156 | None |
| 157 | } else { |
| 158 | Some(count) |
| 159 | }; |
| 160 | self |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /// Fleet columns drop tokens, then the receipt, then identity as room shrinks. |
| 165 | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 166 | pub struct WorkbarAgent { |
| 167 | pub role: String, |
| 168 | pub status: String, |
| 169 | pub objective: String, |
| 170 | pub model: Option<String>, |
| 171 | pub elapsed_seconds: Option<u64>, |
| 172 | pub tokens: Option<u64>, |
| 173 | pub remaining: Option<u32>, |
| 174 | } |
| 175 | |
| 176 | impl WorkbarAgent { |
| 177 | #[must_use] |
| 178 | pub fn new( |
| 179 | role: impl Into<String>, |
| 180 | status: impl Into<String>, |
| 181 | objective: impl Into<String>, |
| 182 | ) -> Self { |
| 183 | Self { |
| 184 | role: role.into(), |
| 185 | status: status.into(), |
| 186 | objective: objective.into(), |
| 187 | ..Self::default() |
| 188 | } |
| 189 | } |
| 190 | #[must_use] |
| 191 | pub fn model(mut self, value: impl Into<String>) -> Self { |
| 192 | self.model = Some(value.into()); |
| 193 | self |
| 194 | } |
| 195 | #[must_use] |
| 196 | pub const fn elapsed_seconds(mut self, value: u64) -> Self { |
| 197 | self.elapsed_seconds = Some(value); |
| 198 | self |
| 199 | } |
| 200 | #[must_use] |
| 201 | pub const fn tokens(mut self, value: u64) -> Self { |
| 202 | self.tokens = Some(value); |
| 203 | self |
| 204 | } |
| 205 | #[must_use] |
| 206 | pub const fn remaining(mut self, value: u32) -> Self { |
| 207 | self.remaining = Some(value); |
| 208 | self |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 213 | pub struct WorkbarRow { |
| 214 | pub id: String, |
| 215 | pub mark: String, |
| 216 | pub label: String, |
| 217 | pub detail: String, |
| 218 | pub tone: WorkbarTone, |
| 219 | pub selectable: bool, |
| 220 | pub agent: Option<WorkbarAgent>, |
| 221 | } |
| 222 | |
| 223 | impl WorkbarRow { |
| 224 | #[must_use] |
| 225 | pub fn new(id: impl Into<String>, label: impl Into<String>) -> Self { |
| 226 | Self { |
| 227 | id: id.into(), |
| 228 | mark: "○".into(), |
| 229 | label: label.into(), |
| 230 | detail: String::new(), |
| 231 | tone: WorkbarTone::Muted, |
| 232 | selectable: true, |
| 233 | agent: None, |
| 234 | } |
| 235 | } |
| 236 | #[must_use] |
| 237 | pub fn mark(mut self, value: impl Into<String>) -> Self { |
| 238 | self.mark = value.into(); |
| 239 | self |
| 240 | } |
| 241 | #[must_use] |
| 242 | pub fn detail(mut self, value: impl Into<String>) -> Self { |
| 243 | self.detail = value.into(); |
| 244 | self |
| 245 | } |
| 246 | #[must_use] |
| 247 | pub const fn tone(mut self, value: WorkbarTone) -> Self { |
| 248 | self.tone = value; |
| 249 | self |
| 250 | } |
| 251 | #[must_use] |
| 252 | pub const fn selectable(mut self, value: bool) -> Self { |
| 253 | self.selectable = value; |
| 254 | self |
| 255 | } |
| 256 | #[must_use] |
| 257 | pub fn agent(mut self, value: WorkbarAgent) -> Self { |
| 258 | self.agent = Some(value); |
| 259 | self |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 264 | pub enum WorkbarTarget { |
| 265 | Panel(WorkbarPanel), |
| 266 | Row(String), |
| 267 | Close, |
| 268 | Divider, |
| 269 | } |
| 270 | |
| 271 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 272 | pub struct WorkbarHitbox { |
| 273 | pub target: WorkbarTarget, |
| 274 | pub area: Rect, |
| 275 | } |
| 276 | |
| 277 | /// Split the host area once, then paint the dock with this effective placement. |
| 278 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 279 | pub struct WorkbarRegions { |
| 280 | pub content: Rect, |
| 281 | pub dock: Option<Rect>, |
| 282 | pub placement: WorkbarPlacement, |
| 283 | } |
| 284 | |
| 285 | /// Geometry used by painting, pointer targets, and keyboard scrolling. |
| 286 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 287 | pub struct WorkbarLayout { |
| 288 | pub body: Rect, |
| 289 | pub content: Rect, |
| 290 | pub visible_rows: usize, |
| 291 | pub offset: usize, |
| 292 | pub overflow: bool, |
| 293 | pub more_row: bool, |
| 294 | pub goal_height: u16, |
| 295 | pub progress_height: u16, |
| 296 | } |
| 297 | |
| 298 | impl WorkbarLayout { |
| 299 | #[must_use] |
| 300 | pub const fn progress_shares_goal_row(width: u16, has_goal: bool) -> bool { |
| 301 | has_goal && width >= 72 |
| 302 | } |
| 303 | |
| 304 | /// Fit headers, visible rows, overflow reserve and offset inside the caller's |
| 305 | /// existing body viewport. Placement/focus/action policy is not inferred. |
| 306 | #[must_use] |
| 307 | pub fn for_body( |
| 308 | body: Rect, |
| 309 | total_rows: usize, |
| 310 | offset: usize, |
| 311 | has_goal: bool, |
| 312 | has_progress: bool, |
| 313 | ) -> Self { |
| 314 | let goal_height = u16::from(has_goal && body.height >= 2); |
| 315 | let fold = Self::progress_shares_goal_row(body.width, goal_height > 0); |
| 316 | let progress_height = |
| 317 | u16::from(has_progress && !fold && body.height.saturating_sub(goal_height) >= 2); |
| 318 | let header = goal_height.saturating_add(progress_height); |
| 319 | let list_height = body.height.saturating_sub(header); |
| 320 | let overflow = total_rows > usize::from(list_height); |
| 321 | let more_row = overflow && list_height >= 2; |
| 322 | let visible_rows = usize::from(list_height).saturating_sub(usize::from(more_row)); |
| 323 | let offset = offset.min(total_rows.saturating_sub(visible_rows.max(1))); |
| 324 | let inset = u16::from(body.width >= 16); |
| 325 | let content = Rect { |
| 326 | x: body.x.saturating_add(inset), |
| 327 | y: body.y.saturating_add(header), |
| 328 | width: body |
| 329 | .width |
| 330 | .saturating_sub(inset.saturating_mul(2)) |
| 331 | .saturating_sub(u16::from(overflow)), |
| 332 | height: list_height, |
| 333 | }; |
| 334 | Self { |
| 335 | body, |
| 336 | content, |
| 337 | visible_rows, |
| 338 | offset, |
| 339 | overflow, |
| 340 | more_row, |
| 341 | goal_height, |
| 342 | progress_height, |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /// The fitted body rail, over caller-supplied symbols and live styles. |
| 348 | /// Both the native Engine and Workbar use this paint path. No scroll state is |
| 349 | /// retained; the current admitted offset/row counts enter on each draw. |
| 350 | #[derive(Clone, Copy, Debug)] |
| 351 | pub struct WorkbarScrollbar<'a> { |
| 352 | pub offset: usize, |
| 353 | pub visible: usize, |
| 354 | pub total: usize, |
| 355 | pub thumb: &'a str, |
| 356 | pub track: &'a str, |
| 357 | pub thumb_style: Style, |
| 358 | pub track_style: Style, |
| 359 | } |
| 360 | |
| 361 | impl WorkbarScrollbar<'_> { |
| 362 | pub fn paint(&self, area: Rect, buf: &mut Buffer) { |
| 363 | if area.is_empty() || self.total == 0 { |
| 364 | return; |
| 365 | } |
| 366 | let height = usize::from(area.height); |
| 367 | let thumb_height = height.saturating_mul(self.visible) / self.total; |
| 368 | let thumb_height = thumb_height.max(1).min(height); |
| 369 | let start = self |
| 370 | .offset |
| 371 | .saturating_mul(height.saturating_sub(thumb_height)) |
| 372 | / self.total.saturating_sub(self.visible).max(1); |
| 373 | let x = area.right().saturating_sub(1); |
| 374 | for row in 0..height { |
| 375 | let active = row >= start && row < start.saturating_add(thumb_height); |
| 376 | if let Some(cell) = buf.cell_mut((x, area.y.saturating_add(row as u16))) { |
| 377 | cell.set_symbol(if active { self.thumb } else { self.track }) |
| 378 | .set_style(if active { |
| 379 | self.thumb_style |
| 380 | } else { |
| 381 | self.track_style |
| 382 | }); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /// The actual Codewhale dock, with application projections replaced by rows. |
| 389 | #[derive(Clone, Debug)] |
| 390 | pub struct Workbar { |
| 391 | pub panel: WorkbarPanel, |
| 392 | pub rows: Vec<WorkbarRow>, |
| 393 | pub tabs: Vec<WorkbarTab>, |
| 394 | pub placement: WorkbarPlacement, |
| 395 | pub goal: Option<String>, |
| 396 | pub goal_paused: bool, |
| 397 | pub progress: Option<String>, |
| 398 | pub selected: Option<String>, |
| 399 | pub hovered: Option<String>, |
| 400 | pub opened: Option<String>, |
| 401 | pub focused: bool, |
| 402 | pub offset: usize, |
| 403 | pub max_height: u16, |
| 404 | pub side_width: u16, |
| 405 | pub explicit: bool, |
| 406 | } |
| 407 | |
| 408 | impl Workbar { |
| 409 | #[must_use] |
| 410 | pub fn new(panel: WorkbarPanel, rows: Vec<WorkbarRow>) -> Self { |
| 411 | let tabs = WorkbarPanel::ORDER |
| 412 | .into_iter() |
| 413 | .filter(|value| { |
| 414 | *value == panel |
| 415 | || matches!( |
| 416 | value, |
| 417 | WorkbarPanel::Context | WorkbarPanel::Git | WorkbarPanel::Cost |
| 418 | ) |
| 419 | }) |
| 420 | .map(WorkbarTab::new) |
| 421 | .collect(); |
| 422 | Self { |
| 423 | panel, |
| 424 | rows, |
| 425 | tabs, |
| 426 | placement: WorkbarPlacement::Bottom, |
| 427 | goal: None, |
| 428 | goal_paused: false, |
| 429 | progress: None, |
| 430 | selected: None, |
| 431 | hovered: None, |
| 432 | opened: None, |
| 433 | focused: false, |
| 434 | offset: 0, |
| 435 | max_height: 5, |
| 436 | side_width: 30, |
| 437 | explicit: true, |
| 438 | } |
| 439 | } |
| 440 | #[must_use] |
| 441 | pub fn tabs(mut self, value: Vec<WorkbarTab>) -> Self { |
| 442 | self.tabs = value; |
| 443 | self |
| 444 | } |
| 445 | #[must_use] |
| 446 | pub const fn placement(mut self, value: WorkbarPlacement) -> Self { |
| 447 | self.placement = value; |
| 448 | self |
| 449 | } |
| 450 | #[must_use] |
| 451 | pub fn goal(mut self, value: impl Into<String>) -> Self { |
| 452 | self.goal = Some(value.into()); |
| 453 | self |
| 454 | } |
| 455 | #[must_use] |
| 456 | pub const fn paused_goal(mut self, paused: bool) -> Self { |
| 457 | self.goal_paused = paused; |
| 458 | self |
| 459 | } |
| 460 | #[must_use] |
| 461 | pub fn progress(mut self, value: impl Into<String>) -> Self { |
| 462 | self.progress = Some(value.into()); |
| 463 | self |
| 464 | } |
| 465 | #[must_use] |
| 466 | pub fn selected(mut self, value: impl Into<String>) -> Self { |
| 467 | self.selected = Some(value.into()); |
| 468 | self |
| 469 | } |
| 470 | #[must_use] |
| 471 | pub fn hovered(mut self, value: impl Into<String>) -> Self { |
| 472 | self.hovered = Some(value.into()); |
| 473 | self |
| 474 | } |
| 475 | #[must_use] |
| 476 | pub fn opened(mut self, value: impl Into<String>) -> Self { |
| 477 | self.opened = Some(value.into()); |
| 478 | self |
| 479 | } |
| 480 | #[must_use] |
| 481 | pub const fn focused(mut self, value: bool) -> Self { |
| 482 | self.focused = value; |
| 483 | self |
| 484 | } |
| 485 | #[must_use] |
| 486 | pub const fn offset(mut self, value: usize) -> Self { |
| 487 | self.offset = value; |
| 488 | self |
| 489 | } |
| 490 | #[must_use] |
| 491 | pub fn max_height(mut self, value: u16) -> Self { |
| 492 | self.max_height = value.clamp(3, 16); |
| 493 | self |
| 494 | } |
| 495 | #[must_use] |
| 496 | pub fn side_width(mut self, value: u16) -> Self { |
| 497 | self.side_width = value.clamp(26, 80); |
| 498 | self |
| 499 | } |
| 500 | /// Automatic docks disappear when there are neither rows nor a live goal. |
| 501 | #[must_use] |
| 502 | pub const fn explicit(mut self, value: bool) -> Self { |
| 503 | self.explicit = value; |
| 504 | self |
| 505 | } |
| 506 | |
| 507 | fn goal_text(&self) -> Option<String> { |
| 508 | let goal = safe(self.goal.as_deref()?); |
| 509 | if goal.trim().is_empty() { |
| 510 | return None; |
| 511 | } |
| 512 | Some(format!( |
| 513 | "Goal{}: {}", |
| 514 | if self.goal_paused { " (paused)" } else { "" }, |
| 515 | goal.trim() |
| 516 | )) |
| 517 | } |
| 518 | |
| 519 | fn requested_height(&self, width: u16) -> u16 { |
| 520 | if width == 0 |
| 521 | || self.placement == WorkbarPlacement::Off |
| 522 | || (self.rows.is_empty() && !self.explicit && self.goal_text().is_none()) |
| 523 | { |
| 524 | return 0; |
| 525 | } |
| 526 | if !self.placement.is_strip() { |
| 527 | return 0; |
| 528 | } |
| 529 | let goal = u16::from(self.goal_text().is_some()); |
| 530 | let progress = u16::from( |
| 531 | self.progress.is_some() && !WorkbarLayout::progress_shares_goal_row(width, goal > 0), |
| 532 | ); |
| 533 | (u16::try_from(self.rows.len().max(usize::from(self.explicit))) |
| 534 | .unwrap_or(u16::MAX) |
| 535 | .saturating_add(goal) |
| 536 | .saturating_add(progress) |
| 537 | .saturating_add(2)) |
| 538 | .clamp(5.min(self.max_height), self.max_height) |
| 539 | } |
| 540 | |
| 541 | /// Native auto-fit ceiling: half the terminal, the configured height, and |
| 542 | /// the rows the conversation can spare. Empty automatic docks collapse. |
| 543 | #[must_use] |
| 544 | pub fn height_for(&self, width: u16, terminal_height: u16, rail_budget: u16) -> u16 { |
| 545 | let cap = self |
| 546 | .max_height |
| 547 | .min((terminal_height / 2).clamp(5, 16)) |
| 548 | .min(rail_budget); |
| 549 | if cap < 5 && (!self.explicit || cap < 3) { |
| 550 | return 0; |
| 551 | } |
| 552 | self.requested_height(width).min(cap) |
| 553 | } |
| 554 | |
| 555 | #[must_use] |
| 556 | pub fn regions(&self, host: Rect) -> WorkbarRegions { |
| 557 | let host = representable(host); |
| 558 | let empty = self.rows.is_empty() && !self.explicit && self.goal_text().is_none(); |
| 559 | if host.is_empty() || self.placement == WorkbarPlacement::Off || empty { |
| 560 | return WorkbarRegions { |
| 561 | content: host, |
| 562 | dock: None, |
| 563 | placement: self.placement, |
| 564 | }; |
| 565 | } |
| 566 | let mut placement = self.placement; |
| 567 | if matches!(placement, WorkbarPlacement::Left | WorkbarPlacement::Right) && host.width < 72 |
| 568 | { |
| 569 | placement = WorkbarPlacement::Top; |
| 570 | } |
| 571 | if placement.is_strip() { |
| 572 | let height = self |
| 573 | .clone() |
| 574 | .placement(placement) |
| 575 | .requested_height(host.width) |
| 576 | .min(host.height / 2); |
| 577 | if height < 3 { |
| 578 | return WorkbarRegions { |
| 579 | content: host, |
| 580 | dock: None, |
| 581 | placement, |
| 582 | }; |
| 583 | } |
| 584 | let (content, dock) = if placement == WorkbarPlacement::Bottom { |
| 585 | ( |
| 586 | Rect { |
| 587 | height: host.height - height, |
| 588 | ..host |
| 589 | }, |
| 590 | Rect { |
| 591 | y: host.bottom() - height, |
| 592 | height, |
| 593 | ..host |
| 594 | }, |
| 595 | ) |
| 596 | } else { |
| 597 | ( |
| 598 | Rect { |
| 599 | y: host.y.saturating_add(height), |
| 600 | height: host.height - height, |
| 601 | ..host |
| 602 | }, |
| 603 | Rect { height, ..host }, |
| 604 | ) |
| 605 | }; |
| 606 | return WorkbarRegions { |
| 607 | content, |
| 608 | dock: Some(dock), |
| 609 | placement, |
| 610 | }; |
| 611 | } |
| 612 | let width = self.side_width.min(host.width.saturating_sub(40)); |
| 613 | if width < 26 { |
| 614 | return self.clone().placement(WorkbarPlacement::Top).regions(host); |
| 615 | } |
| 616 | let (content, dock) = if placement == WorkbarPlacement::Left { |
| 617 | ( |
| 618 | Rect { |
| 619 | x: host.x.saturating_add(width), |
| 620 | width: host.width - width, |
| 621 | ..host |
| 622 | }, |
| 623 | Rect { width, ..host }, |
| 624 | ) |
| 625 | } else { |
| 626 | ( |
| 627 | Rect { |
| 628 | width: host.width - width, |
| 629 | ..host |
| 630 | }, |
| 631 | Rect { |
| 632 | x: host.right() - width, |
| 633 | width, |
| 634 | ..host |
| 635 | }, |
| 636 | ) |
| 637 | }; |
| 638 | WorkbarRegions { |
| 639 | content, |
| 640 | dock: Some(dock), |
| 641 | placement, |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | #[must_use] |
| 646 | pub fn layout(&self, area: Rect) -> WorkbarLayout { |
| 647 | let area = representable(area); |
| 648 | let body = match self.placement { |
| 649 | WorkbarPlacement::Top => Rect { |
| 650 | y: area.y.saturating_add(1).min(area.bottom()), |
| 651 | height: area.height.saturating_sub(2), |
| 652 | ..area |
| 653 | }, |
| 654 | WorkbarPlacement::Bottom => Rect { |
| 655 | y: area.y.saturating_add(2).min(area.bottom()), |
| 656 | height: area.height.saturating_sub(2), |
| 657 | ..area |
| 658 | }, |
| 659 | WorkbarPlacement::Left => Rect { |
| 660 | width: area.width.saturating_sub(1), |
| 661 | ..area |
| 662 | }, |
| 663 | WorkbarPlacement::Right => Rect { |
| 664 | x: area.x.saturating_add(1).min(area.right()), |
| 665 | width: area.width.saturating_sub(1), |
| 666 | ..area |
| 667 | }, |
| 668 | WorkbarPlacement::Off => Rect { height: 0, ..area }, |
| 669 | }; |
| 670 | WorkbarLayout::for_body( |
| 671 | body, |
| 672 | self.rows.len(), |
| 673 | self.offset, |
| 674 | self.placement.is_strip() && self.goal_text().is_some(), |
| 675 | self.placement.is_strip() && self.progress.is_some(), |
| 676 | ) |
| 677 | } |
| 678 | |
| 679 | fn dock_tab_row(&self, area: Rect, theme: Option<&Theme>) -> super::DockTabRow<'_> { |
| 680 | let styles = theme.map_or_else(super::DockTabStyles::default, |theme| { |
| 681 | super::DockTabStyles { |
| 682 | idle: theme.fg(Role::Muted).patch(theme.bg(Role::Sidebar)), |
| 683 | active: theme |
| 684 | .fg(Role::Foreground) |
| 685 | .patch(theme.bg(Role::Selected)) |
| 686 | .add_modifier(Modifier::BOLD), |
| 687 | close: theme.fg(Role::Hint), |
| 688 | ..Default::default() |
| 689 | } |
| 690 | }); |
| 691 | let close = if self.focused && area.width >= 60 { |
| 692 | " Esc × " |
| 693 | } else { |
| 694 | " × " |
| 695 | }; |
| 696 | super::DockTabRow { |
| 697 | tabs: &self.tabs, |
| 698 | active: self.panel, |
| 699 | bottom: self.placement == WorkbarPlacement::Bottom, |
| 700 | close: theme.map_or_else(|| close.into(), |theme| decorative(close, theme).into()), |
| 701 | hovered: None, |
| 702 | pressed: None, |
| 703 | styles, |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | #[must_use] |
| 708 | /// Intersect `area` with the render buffer before querying a clipped view. |
| 709 | pub fn hitboxes(&self, area: Rect) -> Vec<WorkbarHitbox> { |
| 710 | let area = representable(area); |
| 711 | if area.is_empty() || self.placement == WorkbarPlacement::Off { |
| 712 | return Vec::new(); |
| 713 | } |
| 714 | let layout = self.layout(area); |
| 715 | let mut hitboxes = Vec::new(); |
| 716 | if self.placement.is_strip() && area.height >= 2 { |
| 717 | hitboxes.extend( |
| 718 | self.dock_tab_row(area, None) |
| 719 | .plan(area) |
| 720 | .hitboxes() |
| 721 | .into_iter() |
| 722 | .map(|(target, area)| WorkbarHitbox { |
| 723 | target: match target { |
| 724 | super::DockTabTarget::Panel(panel) => WorkbarTarget::Panel(panel), |
| 725 | super::DockTabTarget::Close => WorkbarTarget::Close, |
| 726 | }, |
| 727 | area, |
| 728 | }), |
| 729 | ); |
| 730 | } |
| 731 | if layout.content.width > 0 { |
| 732 | hitboxes.extend( |
| 733 | self.rows |
| 734 | .iter() |
| 735 | .skip(layout.offset) |
| 736 | .take(layout.visible_rows) |
| 737 | .enumerate() |
| 738 | .filter(|(_, row)| row.selectable) |
| 739 | .map(|(index, value)| WorkbarHitbox { |
| 740 | target: WorkbarTarget::Row(value.id.clone()), |
| 741 | area: Rect::new( |
| 742 | layout.content.x, |
| 743 | layout.content.y.saturating_add(index as u16), |
| 744 | layout.content.width, |
| 745 | 1, |
| 746 | ), |
| 747 | }), |
| 748 | ); |
| 749 | } |
| 750 | hitboxes.push(WorkbarHitbox { |
| 751 | target: WorkbarTarget::Divider, |
| 752 | area: self.divider(area), |
| 753 | }); |
| 754 | hitboxes |
| 755 | } |
| 756 | |
| 757 | #[must_use] |
| 758 | pub fn target_at(&self, area: Rect, column: u16, row: u16) -> Option<WorkbarTarget> { |
| 759 | self.hitboxes(area) |
| 760 | .into_iter() |
| 761 | .find(|hitbox| hitbox.area.contains((column, row).into())) |
| 762 | .map(|hitbox| hitbox.target) |
| 763 | } |
| 764 | |
| 765 | fn divider(&self, area: Rect) -> Rect { |
| 766 | match self.placement { |
| 767 | WorkbarPlacement::Top => Rect { |
| 768 | y: area.bottom().saturating_sub(1), |
| 769 | height: 1, |
| 770 | ..area |
| 771 | }, |
| 772 | WorkbarPlacement::Bottom => Rect { height: 1, ..area }, |
| 773 | WorkbarPlacement::Left => Rect { |
| 774 | x: area.right().saturating_sub(1), |
| 775 | width: 1, |
| 776 | ..area |
| 777 | }, |
| 778 | WorkbarPlacement::Right => Rect { width: 1, ..area }, |
| 779 | WorkbarPlacement::Off => Rect { height: 0, ..area }, |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | fn row_style(&self, value: &WorkbarRow, theme: &Theme) -> Style { |
| 784 | let mut style = value.tone.style(theme).patch(theme.bg(Role::Sidebar)); |
| 785 | if value.tone == WorkbarTone::Heading { |
| 786 | style = style.add_modifier(Modifier::BOLD); |
| 787 | } |
| 788 | if !value.selectable { |
| 789 | return style; |
| 790 | } |
| 791 | if self.opened.as_deref() == Some(&value.id) { |
| 792 | style = style |
| 793 | .patch(theme.fg(Role::Accent)) |
| 794 | .add_modifier(Modifier::BOLD | Modifier::UNDERLINED); |
| 795 | } |
| 796 | if self.focused && self.selected.as_deref() == Some(&value.id) { |
| 797 | style = style |
| 798 | .patch(theme.bg(Role::Selected)) |
| 799 | .add_modifier(Modifier::BOLD); |
| 800 | } else if self.hovered.as_deref() == Some(&value.id) { |
| 801 | style = style.patch(theme.bg(Role::Hover)); |
| 802 | } |
| 803 | style |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | impl Paint for Workbar { |
| 808 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 809 | let area = area.intersection(buf.area); |
| 810 | if area.is_empty() |
| 811 | || self.placement == WorkbarPlacement::Off |
| 812 | || (self.rows.is_empty() && !self.explicit && self.goal_text().is_none()) |
| 813 | { |
| 814 | return; |
| 815 | } |
| 816 | let layout = self.layout(area); |
| 817 | for y in area.y..area.bottom() { |
| 818 | row( |
| 819 | Rect::new(area.x, y, area.width, 1), |
| 820 | buf, |
| 821 | &Line::styled( |
| 822 | " ".repeat(usize::from(area.width)), |
| 823 | theme.fg(Role::Foreground).patch(theme.bg(Role::Sidebar)), |
| 824 | ), |
| 825 | ); |
| 826 | } |
| 827 | if self.placement.is_strip() && area.height >= 2 { |
| 828 | self.dock_tab_row(area, Some(theme)).plan(area).paint(buf); |
| 829 | } |
| 830 | if let Some(goal) = self.goal_text().filter(|_| layout.goal_height > 0) { |
| 831 | let goal = decorative(&goal, theme); |
| 832 | let receipt = self |
| 833 | .progress |
| 834 | .as_deref() |
| 835 | .filter(|_| { |
| 836 | WorkbarLayout::progress_shares_goal_row( |
| 837 | layout.body.width, |
| 838 | layout.goal_height > 0, |
| 839 | ) |
| 840 | }) |
| 841 | .map(|value| decorative(value, theme)); |
| 842 | let reserved = receipt |
| 843 | .as_deref() |
| 844 | .map(|value| text::width(value) + 2) |
| 845 | .unwrap_or(0); |
| 846 | let width = usize::from(layout.content.width); |
| 847 | let goal = |
| 848 | text::truncate(&goal, width.saturating_sub(reserved), theme.ascii()).into_owned(); |
| 849 | let mut spans = vec![Span::styled( |
| 850 | goal.clone(), |
| 851 | theme |
| 852 | .tui_ink(if self.goal_paused { |
| 853 | TuiInk::Warning |
| 854 | } else { |
| 855 | TuiInk::Working |
| 856 | }) |
| 857 | .add_modifier(Modifier::BOLD), |
| 858 | )]; |
| 859 | if let Some(receipt) = receipt { |
| 860 | let gap = width |
| 861 | .saturating_sub(text::width(&goal)) |
| 862 | .saturating_sub(text::width(&receipt)); |
| 863 | spans.push(Span::styled( |
| 864 | format!("{}{receipt}", " ".repeat(gap)), |
| 865 | theme.fg(Role::Muted), |
| 866 | )); |
| 867 | } |
| 868 | row( |
| 869 | Rect { |
| 870 | y: layout.body.y, |
| 871 | height: 1, |
| 872 | ..layout.content |
| 873 | }, |
| 874 | buf, |
| 875 | &Line::from(spans), |
| 876 | ); |
| 877 | } |
| 878 | if let Some(progress) = self |
| 879 | .progress |
| 880 | .as_deref() |
| 881 | .filter(|_| layout.progress_height > 0) |
| 882 | { |
| 883 | row( |
| 884 | Rect { |
| 885 | y: layout.body.y.saturating_add(layout.goal_height), |
| 886 | height: 1, |
| 887 | ..layout.content |
| 888 | }, |
| 889 | buf, |
| 890 | &Line::styled( |
| 891 | text::truncate( |
| 892 | &decorative(progress, theme), |
| 893 | usize::from(layout.content.width), |
| 894 | theme.ascii(), |
| 895 | ) |
| 896 | .into_owned(), |
| 897 | theme.fg(Role::Muted), |
| 898 | ), |
| 899 | ); |
| 900 | } |
| 901 | let visible: Vec<_> = self |
| 902 | .rows |
| 903 | .iter() |
| 904 | .skip(layout.offset) |
| 905 | .take(layout.visible_rows) |
| 906 | .collect(); |
| 907 | let identity_cap = usize::from(layout.content.width) * 2 / 5; |
| 908 | let identity_column = visible |
| 909 | .iter() |
| 910 | .map(|value| text::width(&agent_identity(value, identity_cap))) |
| 911 | .max() |
| 912 | .unwrap_or(0); |
| 913 | let status_column = visible |
| 914 | .iter() |
| 915 | .filter_map(|value| value.agent.as_ref()) |
| 916 | .map(|facts| text::width(&safe(&facts.status))) |
| 917 | .max() |
| 918 | .unwrap_or(0); |
| 919 | let ordinal_count = self |
| 920 | .rows |
| 921 | .iter() |
| 922 | .filter(|value| value.id.starts_with("graph:")) |
| 923 | .count(); |
| 924 | let ordinal_width = ordinal_count.max(1).to_string().len(); |
| 925 | for (index, value) in visible.iter().enumerate() { |
| 926 | let target = Rect::new( |
| 927 | layout.content.x, |
| 928 | layout.content.y.saturating_add(index as u16), |
| 929 | layout.content.width, |
| 930 | 1, |
| 931 | ); |
| 932 | let ordinal = if self.placement.is_strip() && value.id.starts_with("graph:") { |
| 933 | let number = self |
| 934 | .rows |
| 935 | .iter() |
| 936 | .take(layout.offset + index + 1) |
| 937 | .filter(|value| value.id.starts_with("graph:")) |
| 938 | .count(); |
| 939 | format!("{number:>ordinal_width$} · ") |
| 940 | } else { |
| 941 | String::new() |
| 942 | }; |
| 943 | let mark = if self.opened.as_deref() == Some(&value.id) && value.selectable { |
| 944 | "▾" |
| 945 | } else { |
| 946 | &value.mark |
| 947 | }; |
| 948 | let prefix = format!( |
| 949 | "{}{} ", |
| 950 | decorative(&ordinal, theme), |
| 951 | decorative(mark, theme) |
| 952 | ); |
| 953 | let width = usize::from(target.width); |
| 954 | let style = self.row_style(value, theme); |
| 955 | if let Some(facts) = &value.agent { |
| 956 | let selected = self.focused && self.selected.as_deref() == Some(&value.id); |
| 957 | let mut normal = theme.fg(Role::Foreground).patch(theme.bg(if selected { |
| 958 | Role::Selected |
| 959 | } else if self.hovered.as_deref() == Some(&value.id) { |
| 960 | Role::Hover |
| 961 | } else { |
| 962 | Role::Sidebar |
| 963 | })); |
| 964 | let mut muted = theme.fg(Role::Muted).patch(normal); |
| 965 | // Secondary columns keep their own muted ink. |
| 966 | muted = muted.patch(theme.fg(Role::Muted)); |
| 967 | if selected { |
| 968 | normal = normal.add_modifier(Modifier::BOLD); |
| 969 | muted = muted.add_modifier(Modifier::BOLD); |
| 970 | } |
| 971 | if self.opened.as_deref() == Some(&value.id) { |
| 972 | normal = normal |
| 973 | .patch(theme.fg(Role::Accent)) |
| 974 | .add_modifier(Modifier::UNDERLINED); |
| 975 | muted = muted |
| 976 | .patch(theme.fg(Role::Accent)) |
| 977 | .add_modifier(Modifier::UNDERLINED); |
| 978 | } |
| 979 | let columns = agent_columns( |
| 980 | facts, |
| 981 | width, |
| 982 | text::width(&prefix), |
| 983 | &agent_identity(value, identity_cap), |
| 984 | identity_column, |
| 985 | status_column, |
| 986 | theme, |
| 987 | ); |
| 988 | let prefix_style = match value.tone { |
| 989 | WorkbarTone::Live |
| 990 | | WorkbarTone::Success |
| 991 | | WorkbarTone::Attention |
| 992 | | WorkbarTone::Failure => normal.patch(value.tone.style(theme)), |
| 993 | WorkbarTone::Heading | WorkbarTone::Muted => normal, |
| 994 | }; |
| 995 | row( |
| 996 | target, |
| 997 | buf, |
| 998 | &Line::from(vec![ |
| 999 | Span::styled(prefix, prefix_style), |
| 1000 | Span::styled(columns.0, muted), |
| 1001 | Span::styled(columns.1, normal), |
| 1002 | Span::styled(columns.2, muted), |
| 1003 | ]), |
| 1004 | ); |
| 1005 | } else { |
| 1006 | let label = decorative(&value.label, theme); |
| 1007 | let label = text::truncate( |
| 1008 | &label, |
| 1009 | width.saturating_sub(text::width(&prefix)).max(1), |
| 1010 | theme.ascii(), |
| 1011 | ); |
| 1012 | let detail_candidate = if value.tone != WorkbarTone::Heading && target.width >= 44 { |
| 1013 | format!(" {}", decorative(&value.detail, theme)) |
| 1014 | } else { |
| 1015 | String::new() |
| 1016 | }; |
| 1017 | let detail_budget = |
| 1018 | width.saturating_sub(text::width(&prefix) + text::width(&label)); |
| 1019 | let detail = if detail_budget >= 4 { |
| 1020 | text::truncate(&detail_candidate, detail_budget, theme.ascii()).into_owned() |
| 1021 | } else { |
| 1022 | String::new() |
| 1023 | }; |
| 1024 | let gap = width.saturating_sub( |
| 1025 | text::width(&prefix) + text::width(&label) + text::width(&detail), |
| 1026 | ); |
| 1027 | row( |
| 1028 | target, |
| 1029 | buf, |
| 1030 | &Line::styled(format!("{prefix}{label}{}{detail}", " ".repeat(gap)), style), |
| 1031 | ); |
| 1032 | } |
| 1033 | } |
| 1034 | if visible.is_empty() && self.explicit && layout.content.height > 0 { |
| 1035 | row( |
| 1036 | Rect { |
| 1037 | height: 1, |
| 1038 | ..layout.content |
| 1039 | }, |
| 1040 | buf, |
| 1041 | &Line::styled( |
| 1042 | text::truncate( |
| 1043 | &decorative(self.panel.empty_label(), theme), |
| 1044 | usize::from(layout.content.width), |
| 1045 | theme.ascii(), |
| 1046 | ) |
| 1047 | .into_owned(), |
| 1048 | theme.fg(Role::Muted), |
| 1049 | ), |
| 1050 | ); |
| 1051 | } |
| 1052 | if layout.more_row { |
| 1053 | let remaining = self |
| 1054 | .rows |
| 1055 | .len() |
| 1056 | .saturating_sub(layout.offset + visible.len()); |
| 1057 | let value = if remaining == 0 { |
| 1058 | String::new() |
| 1059 | } else { |
| 1060 | format!("{} {remaining} more", if theme.ascii() { "v" } else { "↓" }) |
| 1061 | }; |
| 1062 | let value = text::truncate(&value, usize::from(layout.content.width), theme.ascii()); |
| 1063 | let pad = usize::from(layout.content.width).saturating_sub(text::width(&value)); |
| 1064 | row( |
| 1065 | Rect::new( |
| 1066 | layout.content.x, |
| 1067 | layout.content.bottom().saturating_sub(1), |
| 1068 | layout.content.width, |
| 1069 | 1, |
| 1070 | ), |
| 1071 | buf, |
| 1072 | &Line::styled(format!("{}{value}", " ".repeat(pad)), theme.fg(Role::Muted)), |
| 1073 | ); |
| 1074 | } |
| 1075 | let divider = self.divider(area); |
| 1076 | let mark = if self.placement.is_strip() { |
| 1077 | if theme.ascii() { "-" } else { "─" } |
| 1078 | } else if theme.ascii() { |
| 1079 | "|" |
| 1080 | } else { |
| 1081 | "│" |
| 1082 | }; |
| 1083 | for y in divider.y..divider.bottom() { |
| 1084 | row( |
| 1085 | Rect::new(divider.x, y, divider.width, 1), |
| 1086 | buf, |
| 1087 | &Line::styled( |
| 1088 | mark.repeat(usize::from(divider.width)), |
| 1089 | theme.fg(Role::Border), |
| 1090 | ), |
| 1091 | ); |
| 1092 | } |
| 1093 | if layout.overflow && layout.content.height > 0 && layout.body.width > 0 { |
| 1094 | WorkbarScrollbar { |
| 1095 | offset: layout.offset, |
| 1096 | visible: layout.visible_rows, |
| 1097 | total: self.rows.len(), |
| 1098 | thumb: if theme.ascii() { "|" } else { "┃" }, |
| 1099 | track: if theme.ascii() { "|" } else { "│" }, |
| 1100 | thumb_style: theme.tui_ink(TuiInk::Working), |
| 1101 | track_style: theme.fg(Role::Border), |
| 1102 | } |
| 1103 | .paint( |
| 1104 | Rect::new( |
| 1105 | layout.body.right().saturating_sub(1), |
| 1106 | layout.content.y, |
| 1107 | 1, |
| 1108 | layout.content.height, |
| 1109 | ), |
| 1110 | buf, |
| 1111 | ); |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | fn height(&self, width: u16, _theme: &Theme) -> u16 { |
| 1116 | self.requested_height(width) |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | fn safe(value: &str) -> String { |
| 1121 | text::display_safe(&value.replace(['\n', '\r', '\t'], " ")).into_owned() |
| 1122 | } |
| 1123 | |
| 1124 | fn representable(area: Rect) -> Rect { |
| 1125 | Rect { |
| 1126 | width: area.width.min(u16::MAX.saturating_sub(area.x)), |
| 1127 | height: area.height.min(u16::MAX.saturating_sub(area.y)), |
| 1128 | ..area |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | fn decorative(value: &str, theme: &Theme) -> String { |
| 1133 | let value = safe(value); |
| 1134 | if !theme.ascii() { |
| 1135 | return value; |
| 1136 | } |
| 1137 | value |
| 1138 | .chars() |
| 1139 | .map(|value| { |
| 1140 | match value { |
| 1141 | '✎' => return "*".to_owned(), |
| 1142 | '⎇' => return "@".to_owned(), |
| 1143 | '◔' => return "o".to_owned(), |
| 1144 | '±' => return "+/-".to_owned(), |
| 1145 | '−' => return "-".to_owned(), |
| 1146 | _ => {} |
| 1147 | } |
| 1148 | let mut bytes = [0; 4]; |
| 1149 | let glyph = value.encode_utf8(&mut bytes); |
| 1150 | glyphs::ascii_fallback(glyph) |
| 1151 | .map(str::to_owned) |
| 1152 | .unwrap_or_else(|| glyph.to_owned()) |
| 1153 | }) |
| 1154 | .collect() |
| 1155 | } |
| 1156 | |
| 1157 | fn agent_identity(value: &WorkbarRow, cap: usize) -> String { |
| 1158 | let Some(facts) = &value.agent else { |
| 1159 | return String::new(); |
| 1160 | }; |
| 1161 | for candidate in [&value.label, &facts.role] { |
| 1162 | let candidate = safe(candidate); |
| 1163 | if !candidate.is_empty() && text::width(&candidate) <= cap { |
| 1164 | return candidate; |
| 1165 | } |
| 1166 | } |
| 1167 | String::new() |
| 1168 | } |
| 1169 | |
| 1170 | fn elapsed(seconds: u64) -> String { |
| 1171 | if seconds >= 60 { |
| 1172 | format!("{}m {:02}s", seconds / 60, seconds % 60) |
| 1173 | } else { |
| 1174 | format!("{seconds}s") |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | fn tokens(value: u64) -> String { |
| 1179 | if value >= 1_000_000 { |
| 1180 | format!("{:.1}M", value as f64 / 1_000_000.0) |
| 1181 | } else if value >= 1_000 { |
| 1182 | format!("{:.1}k", value as f64 / 1_000.0) |
| 1183 | } else { |
| 1184 | value.to_string() |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | fn agent_receipt(facts: &WorkbarAgent, tier: usize, theme: &Theme) -> String { |
| 1189 | if tier >= 2 { |
| 1190 | return String::new(); |
| 1191 | } |
| 1192 | let mut parts = Vec::new(); |
| 1193 | if let Some(value) = &facts.model { |
| 1194 | let value = safe(value); |
| 1195 | if !value.is_empty() { |
| 1196 | parts.push(value); |
| 1197 | } |
| 1198 | } |
| 1199 | if let Some(value) = facts.elapsed_seconds { |
| 1200 | parts.push(elapsed(value)); |
| 1201 | } |
| 1202 | if tier == 0 |
| 1203 | && let Some(value) = facts.tokens |
| 1204 | { |
| 1205 | parts.push(format!( |
| 1206 | "{} {} tokens", |
| 1207 | if theme.ascii() { "v" } else { "↓" }, |
| 1208 | tokens(value) |
| 1209 | )); |
| 1210 | } |
| 1211 | if let Some(value) = facts.remaining.filter(|value| *value > 0) { |
| 1212 | parts.push(format!("{value} left")); |
| 1213 | } |
| 1214 | parts.join(if theme.ascii() { " . " } else { " · " }) |
| 1215 | } |
| 1216 | |
| 1217 | fn agent_columns( |
| 1218 | facts: &WorkbarAgent, |
| 1219 | width: usize, |
| 1220 | prefix: usize, |
| 1221 | identity: &str, |
| 1222 | identity_column: usize, |
| 1223 | status_column: usize, |
| 1224 | theme: &Theme, |
| 1225 | ) -> (String, String, String) { |
| 1226 | for tier in 0..4 { |
| 1227 | let receipt = agent_receipt(facts, tier, theme); |
| 1228 | let identity = if tier == 3 || identity_column == 0 { |
| 1229 | String::new() |
| 1230 | } else { |
| 1231 | format!("{} ", text::pad(identity, identity_column, theme.ascii())) |
| 1232 | }; |
| 1233 | let status = if tier == 3 || status_column == 0 { |
| 1234 | String::new() |
| 1235 | } else { |
| 1236 | format!( |
| 1237 | "{} ", |
| 1238 | text::pad(&safe(&facts.status), status_column, theme.ascii()) |
| 1239 | ) |
| 1240 | }; |
| 1241 | let secondary = format!("{identity}{status}"); |
| 1242 | let receipt_cost = if receipt.is_empty() { |
| 1243 | 0 |
| 1244 | } else { |
| 1245 | text::width(&receipt) + 2 |
| 1246 | }; |
| 1247 | let budget = width |
| 1248 | .saturating_sub(prefix) |
| 1249 | .saturating_sub(text::width(&secondary)) |
| 1250 | .saturating_sub(receipt_cost); |
| 1251 | if budget < 24 && tier != 3 { |
| 1252 | continue; |
| 1253 | } |
| 1254 | let objective = safe(&facts.objective); |
| 1255 | let objective = text::truncate(&objective, budget, theme.ascii()).into_owned(); |
| 1256 | let gap = width |
| 1257 | .saturating_sub(prefix) |
| 1258 | .saturating_sub(text::width(&secondary)) |
| 1259 | .saturating_sub(text::width(&objective)) |
| 1260 | .saturating_sub(text::width(&receipt)); |
| 1261 | return ( |
| 1262 | secondary, |
| 1263 | objective, |
| 1264 | format!("{}{receipt}", " ".repeat(gap)), |
| 1265 | ); |
| 1266 | } |
| 1267 | (String::new(), String::new(), String::new()) |
| 1268 | } |
| 1269 | |
| 1270 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 1271 | pub enum WorkbarOutcome { |
| 1272 | Ignored, |
| 1273 | Changed, |
| 1274 | Panel(WorkbarPanel), |
| 1275 | Activate(String), |
| 1276 | Close, |
| 1277 | ReleaseFocus, |
| 1278 | } |
| 1279 | |
| 1280 | /// Optional native keyboard state. Enter returns a row id for host dispatch. |
| 1281 | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 1282 | pub struct WorkbarState { |
| 1283 | pub panel: WorkbarPanel, |
| 1284 | pub selected: Option<String>, |
| 1285 | pub focused: bool, |
| 1286 | pub offset: usize, |
| 1287 | } |
| 1288 | |
| 1289 | impl WorkbarState { |
| 1290 | #[must_use] |
| 1291 | pub fn apply(&self, mut workbar: Workbar) -> Workbar { |
| 1292 | workbar.panel = self.panel; |
| 1293 | workbar.selected = self.selected.clone(); |
| 1294 | workbar.focused = self.focused; |
| 1295 | workbar.offset = self.offset; |
| 1296 | workbar |
| 1297 | } |
| 1298 | |
| 1299 | pub fn handle_key( |
| 1300 | &mut self, |
| 1301 | key: KeyEvent, |
| 1302 | rows: &[WorkbarRow], |
| 1303 | visible_rows: usize, |
| 1304 | ) -> WorkbarOutcome { |
| 1305 | if key.kind == KeyEventKind::Release { |
| 1306 | return WorkbarOutcome::Ignored; |
| 1307 | } |
| 1308 | let control = key.modifiers.contains(KeyModifiers::CONTROL); |
| 1309 | if control |
| 1310 | && matches!( |
| 1311 | key.code, |
| 1312 | KeyCode::Tab | KeyCode::BackTab | KeyCode::Char(']') |
| 1313 | ) |
| 1314 | { |
| 1315 | self.panel = |
| 1316 | if key.code == KeyCode::BackTab || key.modifiers.contains(KeyModifiers::SHIFT) { |
| 1317 | self.panel.prev() |
| 1318 | } else { |
| 1319 | self.panel.next() |
| 1320 | }; |
| 1321 | self.selected = None; |
| 1322 | self.offset = 0; |
| 1323 | return WorkbarOutcome::Panel(self.panel); |
| 1324 | } |
| 1325 | if visible_rows == 0 { |
| 1326 | self.focused = false; |
| 1327 | return WorkbarOutcome::Ignored; |
| 1328 | } |
| 1329 | if !self.focused { |
| 1330 | if key.code == KeyCode::Char('w') && key.modifiers.contains(KeyModifiers::ALT) { |
| 1331 | self.focused = true; |
| 1332 | if !rows |
| 1333 | .iter() |
| 1334 | .any(|value| value.selectable && self.selected.as_deref() == Some(&value.id)) |
| 1335 | { |
| 1336 | self.selected = rows |
| 1337 | .iter() |
| 1338 | .find(|value| value.selectable) |
| 1339 | .map(|value| value.id.clone()); |
| 1340 | } |
| 1341 | return WorkbarOutcome::Changed; |
| 1342 | } |
| 1343 | return WorkbarOutcome::Ignored; |
| 1344 | } |
| 1345 | if matches!(key.code, KeyCode::Char(_)) |
| 1346 | && !key |
| 1347 | .modifiers |
| 1348 | .intersects(KeyModifiers::CONTROL | KeyModifiers::ALT | KeyModifiers::SUPER) |
| 1349 | { |
| 1350 | self.focused = false; |
| 1351 | return WorkbarOutcome::ReleaseFocus; |
| 1352 | } |
| 1353 | match key.code { |
| 1354 | KeyCode::Esc => { |
| 1355 | self.focused = false; |
| 1356 | self.selected = None; |
| 1357 | WorkbarOutcome::Close |
| 1358 | } |
| 1359 | KeyCode::Left | KeyCode::Right => { |
| 1360 | self.panel = if key.code == KeyCode::Left { |
| 1361 | self.panel.prev() |
| 1362 | } else { |
| 1363 | self.panel.next() |
| 1364 | }; |
| 1365 | self.selected = None; |
| 1366 | self.offset = 0; |
| 1367 | WorkbarOutcome::Panel(self.panel) |
| 1368 | } |
| 1369 | KeyCode::Enter => self |
| 1370 | .selected |
| 1371 | .as_ref() |
| 1372 | .filter(|id| { |
| 1373 | rows.iter() |
| 1374 | .any(|value| &value.id == *id && value.selectable) |
| 1375 | }) |
| 1376 | .map(|id| WorkbarOutcome::Activate(id.clone())) |
| 1377 | .unwrap_or(WorkbarOutcome::Ignored), |
| 1378 | KeyCode::Up | KeyCode::Down | KeyCode::Home | KeyCode::End => { |
| 1379 | let selectable: Vec<_> = rows |
| 1380 | .iter() |
| 1381 | .enumerate() |
| 1382 | .filter(|(_, value)| value.selectable) |
| 1383 | .collect(); |
| 1384 | if selectable.is_empty() { |
| 1385 | self.selected = None; |
| 1386 | return WorkbarOutcome::Ignored; |
| 1387 | } |
| 1388 | let current = selectable |
| 1389 | .iter() |
| 1390 | .position(|(_, value)| self.selected.as_deref() == Some(&value.id)) |
| 1391 | .unwrap_or(0); |
| 1392 | let next = match key.code { |
| 1393 | KeyCode::Up => current.saturating_sub(1), |
| 1394 | KeyCode::Down => (current + 1).min(selectable.len() - 1), |
| 1395 | KeyCode::Home => 0, |
| 1396 | _ => selectable.len() - 1, |
| 1397 | }; |
| 1398 | let (index, value) = selectable[next]; |
| 1399 | self.selected = Some(value.id.clone()); |
| 1400 | if index < self.offset { |
| 1401 | self.offset = index; |
| 1402 | } else if index >= self.offset.saturating_add(visible_rows.max(1)) { |
| 1403 | self.offset = index.saturating_add(1).saturating_sub(visible_rows.max(1)); |
| 1404 | } |
| 1405 | WorkbarOutcome::Changed |
| 1406 | } |
| 1407 | KeyCode::PageUp => { |
| 1408 | self.offset = self.offset.saturating_sub(visible_rows.max(1)); |
| 1409 | WorkbarOutcome::Changed |
| 1410 | } |
| 1411 | KeyCode::PageDown => { |
| 1412 | self.offset = self |
| 1413 | .offset |
| 1414 | .saturating_add(visible_rows.max(1)) |
| 1415 | .min(rows.len().saturating_sub(visible_rows.max(1))); |
| 1416 | WorkbarOutcome::Changed |
| 1417 | } |
| 1418 | _ => WorkbarOutcome::Ignored, |
| 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | /// Compact `Tasks · done/total` header with live-green completed beads and muted open beads. |
| 1424 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 1425 | pub struct TaskProgressHeader<'a> { |
| 1426 | /// Section label shown before the completion ratio (defaults to `"Tasks"`). |
| 1427 | pub label: Cow<'a, str>, |
| 1428 | /// Number of completed tasks. |
| 1429 | pub done: usize, |
| 1430 | /// Total number of tasks. |
| 1431 | pub total: usize, |
| 1432 | /// Optional active step title rendered after the beads when room permits. |
| 1433 | pub active: Option<Cow<'a, str>>, |
| 1434 | } |
| 1435 | |
| 1436 | impl<'a> TaskProgressHeader<'a> { |
| 1437 | /// Construct a task progress header for `done` of `total` tasks. |
| 1438 | #[must_use] |
| 1439 | pub fn new(done: usize, total: usize) -> Self { |
| 1440 | Self { |
| 1441 | label: Cow::Borrowed("Tasks"), |
| 1442 | done, |
| 1443 | total, |
| 1444 | active: None, |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | /// Override the leading label (for localization or custom headings). |
| 1449 | #[must_use] |
| 1450 | pub fn label(mut self, label: impl Into<Cow<'a, str>>) -> Self { |
| 1451 | self.label = label.into(); |
| 1452 | self |
| 1453 | } |
| 1454 | |
| 1455 | /// Attach the currently active task title to display after the progress beads. |
| 1456 | #[must_use] |
| 1457 | pub fn active(mut self, active: impl Into<Cow<'a, str>>) -> Self { |
| 1458 | self.active = Some(active.into()); |
| 1459 | self |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | impl Paint for TaskProgressHeader<'_> { |
| 1464 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 1465 | let area = area.intersection(buf.area); |
| 1466 | if area.is_empty() { |
| 1467 | return; |
| 1468 | } |
| 1469 | let ascii = theme.ascii(); |
| 1470 | let sep = if ascii { " - " } else { " · " }; |
| 1471 | let done_bead = if ascii { "x" } else { "✓" }; |
| 1472 | let open_bead = if ascii { "o" } else { "○" }; |
| 1473 | let label = text::display_safe(self.label.as_ref()); |
| 1474 | let done = self.done.min(self.total); |
| 1475 | let head = format!("{label}{sep}{done}/{}", self.total); |
| 1476 | let width = usize::from(area.width); |
| 1477 | let head_truncated = text::truncate(&head, width, ascii).into_owned(); |
| 1478 | let mut used = text::width(&head_truncated); |
| 1479 | let mut spans = vec![Span::styled( |
| 1480 | head_truncated, |
| 1481 | theme.fg(Role::Foreground).add_modifier(Modifier::BOLD), |
| 1482 | )]; |
| 1483 | let shown_beads = self.total.min(12); |
| 1484 | if shown_beads > 0 && used + 2 < width { |
| 1485 | let done_beads = done |
| 1486 | .saturating_mul(shown_beads) |
| 1487 | .checked_div(self.total) |
| 1488 | .unwrap_or(0); |
| 1489 | let beads_avail = width.saturating_sub(used + 2); |
| 1490 | let fit_beads = shown_beads.min(beads_avail); |
| 1491 | if fit_beads > 0 { |
| 1492 | spans.push(Span::raw(" ")); |
| 1493 | used += 2; |
| 1494 | let done_shown = done_beads.min(fit_beads); |
| 1495 | let open_shown = fit_beads.saturating_sub(done_shown); |
| 1496 | if done_shown > 0 { |
| 1497 | spans.push(Span::styled( |
| 1498 | done_bead.repeat(done_shown), |
| 1499 | theme.tui_ink(TuiInk::Success), |
| 1500 | )); |
| 1501 | used += done_shown; |
| 1502 | } |
| 1503 | if open_shown > 0 { |
| 1504 | spans.push(Span::styled( |
| 1505 | open_bead.repeat(open_shown), |
| 1506 | theme.fg(Role::Muted), |
| 1507 | )); |
| 1508 | used += open_shown; |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | if let Some(active) = &self.active { |
| 1513 | let clean = text::display_safe(active.as_ref()); |
| 1514 | let sep_w = text::width(sep); |
| 1515 | if !clean.is_empty() && used + sep_w + 1 < width { |
| 1516 | let rem = width.saturating_sub(used + sep_w); |
| 1517 | spans.push(Span::styled(sep, theme.fg(Role::Muted))); |
| 1518 | spans.push(Span::styled( |
| 1519 | text::truncate(&clean, rem, ascii).into_owned(), |
| 1520 | theme.fg(Role::Muted), |
| 1521 | )); |
| 1522 | } |
| 1523 | } |
| 1524 | row(area, buf, &Line::from(spans)); |
| 1525 | } |
| 1526 | } |
| 1527 |