| 1 | //! Transcript, composer and fleet surfaces over caller-owned data. |
| 2 | //! |
| 3 | //! These are renderers, not another session or input runtime. Hosts own the |
| 4 | //! author, task, route, state, elapsed words and text editing. The visual |
| 5 | //! grammar comes from the Engine's `history/message.rs`, `tool_card.rs`, |
| 6 | //! `agent_card.rs` and `composer_chrome.rs`: a speaker anchor, continuation |
| 7 | //! rails, semantic tool summaries, agent identity and a quiet input ledge. |
| 8 | |
| 9 | use std::borrow::Cow; |
| 10 | |
| 11 | use ratatui::{ |
| 12 | buffer::Buffer, |
| 13 | layout::Rect, |
| 14 | style::Modifier, |
| 15 | text::{Line, Span}, |
| 16 | }; |
| 17 | use unicode_segmentation::UnicodeSegmentation; |
| 18 | |
| 19 | use crate::{ |
| 20 | Depth, HorizonRule, KeyHints, Paint, Panel, Role, State, StatusMark, Theme, glyphs, text, |
| 21 | }; |
| 22 | |
| 23 | /// A plain-text transcript entry. The host supplies its author and marker; |
| 24 | /// this component neither parses Markdown nor guesses who sent the message. |
| 25 | /// [`Message::native`] uses the current TUI's inline speaker mark and dim |
| 26 | /// continuation rail. A nonempty author adds the optional heading variation. |
| 27 | #[derive(Clone, Debug)] |
| 28 | pub struct Message<'a> { |
| 29 | pub author: Cow<'a, str>, |
| 30 | pub body: Cow<'a, str>, |
| 31 | pub author_role: Role, |
| 32 | pub marker: &'static str, |
| 33 | } |
| 34 | |
| 35 | impl<'a> Message<'a> { |
| 36 | #[must_use] |
| 37 | pub fn new(author: impl Into<Cow<'a, str>>, body: impl Into<Cow<'a, str>>) -> Self { |
| 38 | Self { |
| 39 | author: author.into(), |
| 40 | body: body.into(), |
| 41 | author_role: Role::Foreground, |
| 42 | marker: glyphs::CURRENT, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /// The current Codewhale transcript's plain-text message decoration: |
| 47 | /// `● body` first, then `▏ ` before each continuation. The host can choose |
| 48 | /// another speaker with [`Self::marker`] and its ink with [`Self::role`]. |
| 49 | /// Whitespace-only assistant entries render nothing. No Markdown parser, |
| 50 | /// streaming clock, or session state is introduced by this constructor. |
| 51 | #[must_use] |
| 52 | pub fn native(body: impl Into<Cow<'a, str>>) -> Self { |
| 53 | Self::new("", body).role(Role::Accent) |
| 54 | } |
| 55 | |
| 56 | #[must_use] |
| 57 | pub fn role(mut self, role: Role) -> Self { |
| 58 | self.author_role = role; |
| 59 | self |
| 60 | } |
| 61 | |
| 62 | /// Use a charter mark such as [`glyphs::USER`] for a user-authored entry. |
| 63 | #[must_use] |
| 64 | pub fn marker(mut self, marker: &'static str) -> Self { |
| 65 | self.marker = marker; |
| 66 | self |
| 67 | } |
| 68 | |
| 69 | fn lines(&self, width: u16, theme: &Theme) -> Vec<Line<'static>> { |
| 70 | if width == 0 { |
| 71 | return Vec::new(); |
| 72 | } |
| 73 | if self.author.is_empty() { |
| 74 | return self.native_lines(width, theme); |
| 75 | } |
| 76 | let mut lines = vec![Line::from(vec![ |
| 77 | Span::styled( |
| 78 | safe(glyphs::pick(self.marker, theme.ascii())), |
| 79 | theme.fg(self.author_role), |
| 80 | ), |
| 81 | Span::raw(" "), |
| 82 | Span::styled( |
| 83 | safe(&self.author), |
| 84 | theme.fg(self.author_role).add_modifier(Modifier::BOLD), |
| 85 | ), |
| 86 | ])]; |
| 87 | let rail = width >= 3; |
| 88 | let body = wrapped( |
| 89 | &self.body, |
| 90 | width.saturating_sub(if rail { 2 } else { 0 }), |
| 91 | theme.ascii(), |
| 92 | ); |
| 93 | lines.extend(body_lines(&body, rail, theme, Role::Foreground)); |
| 94 | lines |
| 95 | } |
| 96 | |
| 97 | fn native_lines(&self, width: u16, theme: &Theme) -> Vec<Line<'static>> { |
| 98 | // The native assistant skips stray blank streaming cells, including |
| 99 | // caller text whose only visible content disappears during sanitizing. |
| 100 | if self.marker == glyphs::CURRENT |
| 101 | && self |
| 102 | .body |
| 103 | .split('\n') |
| 104 | .all(|line| text::display_safe(line).trim().is_empty()) |
| 105 | { |
| 106 | return Vec::new(); |
| 107 | } |
| 108 | let marker = safe(glyphs::pick(self.marker, theme.ascii())); |
| 109 | let marker_width = text::width(&marker); |
| 110 | // Native message.rs reserves prefix + two cells before wrapping, |
| 111 | // while the painted prefix itself occupies prefix + one cell. |
| 112 | let reserved = u16::try_from(marker_width.saturating_add(2)).unwrap_or(u16::MAX); |
| 113 | let mut body = wrapped( |
| 114 | &self.body, |
| 115 | width.saturating_sub(reserved).max(1), |
| 116 | theme.ascii(), |
| 117 | ); |
| 118 | if body.lines.is_empty() { |
| 119 | body.lines.push(String::new()); |
| 120 | } |
| 121 | body.lines |
| 122 | .into_iter() |
| 123 | .enumerate() |
| 124 | .map(|(index, body)| { |
| 125 | let mut spans = Vec::new(); |
| 126 | if !marker.is_empty() { |
| 127 | if index == 0 { |
| 128 | spans.push(Span::styled( |
| 129 | marker.clone(), |
| 130 | theme.fg(self.author_role).add_modifier(Modifier::BOLD), |
| 131 | )); |
| 132 | spans.push(Span::raw(" ")); |
| 133 | } else { |
| 134 | let rail = glyphs::pick(glyphs::TRANSCRIPT_RAIL, theme.ascii()); |
| 135 | spans.push(Span::styled( |
| 136 | format!("{}{}", rail.trim_end(), " ".repeat(marker_width)), |
| 137 | theme.fg(Role::Dim), |
| 138 | )); |
| 139 | } |
| 140 | } |
| 141 | spans.push(Span::styled(body, theme.fg(Role::Foreground))); |
| 142 | Line::from(spans) |
| 143 | }) |
| 144 | .collect() |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | impl Paint for Message<'_> { |
| 149 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 150 | let area = area.intersection(buf.area); |
| 151 | for (index, line) in self |
| 152 | .lines(area.width, theme) |
| 153 | .iter() |
| 154 | .take(usize::from(area.height)) |
| 155 | .enumerate() |
| 156 | { |
| 157 | super::workbench::row( |
| 158 | Rect { |
| 159 | y: area.y.saturating_add(rows(index)), |
| 160 | height: 1, |
| 161 | ..area |
| 162 | }, |
| 163 | buf, |
| 164 | line, |
| 165 | ); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | fn height(&self, width: u16, theme: &Theme) -> u16 { |
| 170 | rows(self.lines(width, theme).len()) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /// A tool preview with a semantic title and subject supplied by the host. |
| 175 | /// |
| 176 | /// `omitted_lines` counts logical output lines the host removed before |
| 177 | /// supplying `output`. If the viewport clips output, the footer also counts |
| 178 | /// logical lines whose full content is not visible (including a partially |
| 179 | /// visible final line). A wrapped line is never counted twice. |
| 180 | #[derive(Clone, Debug)] |
| 181 | pub struct ToolCard<'a> { |
| 182 | pub title: Cow<'a, str>, |
| 183 | pub subject: Cow<'a, str>, |
| 184 | pub status: StatusMark, |
| 185 | pub elapsed: Option<Cow<'a, str>>, |
| 186 | pub output: Cow<'a, str>, |
| 187 | pub omitted_lines: usize, |
| 188 | /// Words after the count. Hosts can supply translated copy. |
| 189 | pub omission_label: Cow<'a, str>, |
| 190 | /// Optional `+added −removed` diff summary shown on the header row. |
| 191 | pub diff_outcome: Option<(usize, usize)>, |
| 192 | /// Style `subject` as a file path (`dir/` muted, `basename` bold foreground). |
| 193 | pub path_subject: bool, |
| 194 | } |
| 195 | |
| 196 | impl<'a> ToolCard<'a> { |
| 197 | #[must_use] |
| 198 | pub fn new( |
| 199 | title: impl Into<Cow<'a, str>>, |
| 200 | subject: impl Into<Cow<'a, str>>, |
| 201 | state: State, |
| 202 | ) -> Self { |
| 203 | Self { |
| 204 | title: title.into(), |
| 205 | subject: subject.into(), |
| 206 | status: StatusMark::new(state), |
| 207 | elapsed: None, |
| 208 | output: Cow::Borrowed(""), |
| 209 | omitted_lines: 0, |
| 210 | omission_label: Cow::Borrowed("output lines not fully shown"), |
| 211 | diff_outcome: None, |
| 212 | path_subject: false, |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /// Attach a compact `+added −removed` diff summary to the card header. |
| 217 | #[must_use] |
| 218 | pub fn diff_outcome(mut self, added: usize, removed: usize) -> Self { |
| 219 | self.diff_outcome = Some((added, removed)); |
| 220 | self |
| 221 | } |
| 222 | |
| 223 | /// Style the subject as a file path (`dir/` in [`Role::Muted`] and |
| 224 | /// `basename` in bold [`Role::Foreground`]). |
| 225 | #[must_use] |
| 226 | pub fn path_subject(mut self, enabled: bool) -> Self { |
| 227 | self.path_subject = enabled; |
| 228 | self |
| 229 | } |
| 230 | |
| 231 | #[must_use] |
| 232 | pub fn output(mut self, output: impl Into<Cow<'a, str>>) -> Self { |
| 233 | self.output = output.into(); |
| 234 | self |
| 235 | } |
| 236 | |
| 237 | #[must_use] |
| 238 | pub fn elapsed(mut self, elapsed: impl Into<Cow<'a, str>>) -> Self { |
| 239 | self.elapsed = Some(elapsed.into()); |
| 240 | self |
| 241 | } |
| 242 | |
| 243 | #[must_use] |
| 244 | pub fn omitted_lines(mut self, count: usize) -> Self { |
| 245 | self.omitted_lines = count; |
| 246 | self |
| 247 | } |
| 248 | |
| 249 | #[must_use] |
| 250 | pub fn omission_label(mut self, label: impl Into<Cow<'a, str>>) -> Self { |
| 251 | self.omission_label = label.into(); |
| 252 | self |
| 253 | } |
| 254 | |
| 255 | #[must_use] |
| 256 | pub fn status_word(mut self, word: impl Into<Cow<'static, str>>) -> Self { |
| 257 | self.status = self.status.word(word); |
| 258 | self |
| 259 | } |
| 260 | |
| 261 | fn heading(&self, width: u16, theme: &Theme) -> Vec<Line<'static>> { |
| 262 | let mut header = activity_header(&self.title, &self.status, self.elapsed.as_deref(), theme); |
| 263 | if let Some((added, removed)) = self.diff_outcome { |
| 264 | let minus = if theme.ascii() { "-" } else { "\u{2212}" }; |
| 265 | header.spans.push(Span::raw(" ")); |
| 266 | header |
| 267 | .spans |
| 268 | .push(Span::styled(format!("+{added}"), theme.fg(Role::Live))); |
| 269 | header.spans.push(Span::raw(" ")); |
| 270 | header.spans.push(Span::styled( |
| 271 | format!("{minus}{removed}"), |
| 272 | theme.fg(Role::Danger), |
| 273 | )); |
| 274 | } |
| 275 | let mut lines = vec![header]; |
| 276 | let subject = wrapped(&self.subject, width, theme.ascii()); |
| 277 | if self.path_subject { |
| 278 | lines.extend( |
| 279 | subject |
| 280 | .lines |
| 281 | .into_iter() |
| 282 | .map(|line| path_subject_line(&line, theme)), |
| 283 | ); |
| 284 | } else { |
| 285 | lines.extend( |
| 286 | subject |
| 287 | .lines |
| 288 | .into_iter() |
| 289 | .map(|line| Line::from(Span::styled(line, theme.fg(Role::Accent)))), |
| 290 | ); |
| 291 | } |
| 292 | lines |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | impl Paint for ToolCard<'_> { |
| 297 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 298 | let area = area.intersection(buf.area); |
| 299 | if area.is_empty() { |
| 300 | return; |
| 301 | } |
| 302 | let inner = Panel::new(Depth::Raised).draw(area, buf, theme); |
| 303 | if inner.is_empty() { |
| 304 | return; |
| 305 | } |
| 306 | let heading = self.heading(inner.width, theme); |
| 307 | paint_lines(&heading, inner, buf); |
| 308 | let output_area = below(inner, rows(heading.len())); |
| 309 | if output_area.is_empty() { |
| 310 | return; |
| 311 | } |
| 312 | let rail = output_area.width >= 3; |
| 313 | let output = wrapped( |
| 314 | &self.output, |
| 315 | output_area.width.saturating_sub(if rail { 2 } else { 0 }), |
| 316 | theme.ascii(), |
| 317 | ); |
| 318 | let clipped = output.lines.len() > usize::from(output_area.height); |
| 319 | let needs_footer = |
| 320 | self.omitted_lines > 0 || clipped || output.hidden_lines(output.lines.len()) > 0; |
| 321 | let visible = usize::from(output_area.height.saturating_sub(u16::from(needs_footer))); |
| 322 | let shown = visible.min(output.lines.len()); |
| 323 | let omitted = self |
| 324 | .omitted_lines |
| 325 | .saturating_add(output.hidden_lines(shown)); |
| 326 | let output_lines = body_lines(&output, rail, theme, Role::Muted); |
| 327 | paint_lines( |
| 328 | &output_lines, |
| 329 | Rect { |
| 330 | height: rows(shown), |
| 331 | ..output_area |
| 332 | }, |
| 333 | buf, |
| 334 | ); |
| 335 | if needs_footer && omitted > 0 { |
| 336 | let raw = format!("{omitted} {}", safe(&self.omission_label)); |
| 337 | let clipped = text::truncate(&raw, usize::from(output_area.width), theme.ascii()); |
| 338 | let footer = Line::from(Span::styled(clipped.into_owned(), theme.fg(Role::Hint))); |
| 339 | super::workbench::row( |
| 340 | Rect { |
| 341 | y: output_area.y.saturating_add(rows(shown)), |
| 342 | height: 1, |
| 343 | ..output_area |
| 344 | }, |
| 345 | buf, |
| 346 | &footer, |
| 347 | ); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | fn height(&self, width: u16, theme: &Theme) -> u16 { |
| 352 | let inner = panel_width(width, theme); |
| 353 | if inner == 0 { |
| 354 | return panel_height(0, width, theme); |
| 355 | } |
| 356 | let output = wrapped( |
| 357 | &self.output, |
| 358 | inner.saturating_sub(if inner >= 3 { 2 } else { 0 }), |
| 359 | theme.ascii(), |
| 360 | ); |
| 361 | let footer = |
| 362 | usize::from(self.omitted_lines > 0 || output.hidden_lines(output.lines.len()) > 0); |
| 363 | panel_height( |
| 364 | rows( |
| 365 | self.heading(inner, theme) |
| 366 | .len() |
| 367 | .saturating_add(output.lines.len()) |
| 368 | .saturating_add(footer), |
| 369 | ), |
| 370 | width, |
| 371 | theme, |
| 372 | ) |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /// Display-only input chrome. Text, placeholder, context and hint words are |
| 377 | /// caller-owned. The host continues to own editing, cursor placement, history, |
| 378 | /// completion, submission predicates and input handling. |
| 379 | /// |
| 380 | /// ```no_run |
| 381 | /// use codewhale_ratatui::{Composer, Paint, Theme}; |
| 382 | /// # fn draw(frame: &mut ratatui::Frame<'_>) { |
| 383 | /// let theme = Theme::detect(); |
| 384 | /// let composer = Composer::new("Review the changes") |
| 385 | /// .context("codewhale-ratatui / main"); |
| 386 | /// frame.render_widget(composer.themed(&theme), frame.area()); |
| 387 | /// # } |
| 388 | /// ``` |
| 389 | #[derive(Clone, Debug)] |
| 390 | pub struct Composer<'a> { |
| 391 | pub text: Cow<'a, str>, |
| 392 | pub placeholder: Cow<'a, str>, |
| 393 | pub context: Option<Cow<'a, str>>, |
| 394 | pub hints: Option<&'a KeyHints>, |
| 395 | pub focused: bool, |
| 396 | pub prompt: &'static str, |
| 397 | } |
| 398 | |
| 399 | impl<'a> Composer<'a> { |
| 400 | #[must_use] |
| 401 | pub fn new(text: impl Into<Cow<'a, str>>) -> Self { |
| 402 | Self { |
| 403 | text: text.into(), |
| 404 | placeholder: Cow::Borrowed("Ask Codewhale to do something"), |
| 405 | context: None, |
| 406 | hints: None, |
| 407 | focused: false, |
| 408 | prompt: "›", |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | #[must_use] |
| 413 | pub fn placeholder(mut self, placeholder: impl Into<Cow<'a, str>>) -> Self { |
| 414 | self.placeholder = placeholder.into(); |
| 415 | self |
| 416 | } |
| 417 | |
| 418 | /// A host-provided repository, branch, mode or route summary. Nothing is |
| 419 | /// discovered or inferred by the component. |
| 420 | #[must_use] |
| 421 | pub fn context(mut self, context: impl Into<Cow<'a, str>>) -> Self { |
| 422 | self.context = Some(context.into()); |
| 423 | self |
| 424 | } |
| 425 | |
| 426 | #[must_use] |
| 427 | pub fn hints(mut self, hints: &'a KeyHints) -> Self { |
| 428 | self.hints = Some(hints); |
| 429 | self |
| 430 | } |
| 431 | |
| 432 | #[must_use] |
| 433 | pub fn focused(mut self, focused: bool) -> Self { |
| 434 | self.focused = focused; |
| 435 | self |
| 436 | } |
| 437 | |
| 438 | #[must_use] |
| 439 | pub fn prompt(mut self, prompt: &'static str) -> Self { |
| 440 | self.prompt = prompt; |
| 441 | self |
| 442 | } |
| 443 | |
| 444 | fn input_lines(&self, width: u16, theme: &Theme) -> Vec<Line<'static>> { |
| 445 | if width == 0 { |
| 446 | return Vec::new(); |
| 447 | } |
| 448 | let prompt = safe(glyphs::pick(self.prompt, theme.ascii())); |
| 449 | let lead = rows(text::width(&prompt).saturating_add(1)).min(width.saturating_sub(1)); |
| 450 | let placeholder = self.text.is_empty(); |
| 451 | let content = if placeholder { |
| 452 | &self.placeholder |
| 453 | } else { |
| 454 | &self.text |
| 455 | }; |
| 456 | let input = wrapped(content, width.saturating_sub(lead), theme.ascii()); |
| 457 | let body_role = if placeholder { |
| 458 | Role::Hint |
| 459 | } else { |
| 460 | Role::Foreground |
| 461 | }; |
| 462 | let prompt_role = if self.focused { |
| 463 | Role::Accent |
| 464 | } else { |
| 465 | Role::Muted |
| 466 | }; |
| 467 | let mut lines: Vec<Line<'static>> = input |
| 468 | .lines |
| 469 | .into_iter() |
| 470 | .enumerate() |
| 471 | .map(|(index, line)| { |
| 472 | let prefix = if index == 0 { |
| 473 | text::pad(&prompt, usize::from(lead), theme.ascii()) |
| 474 | } else { |
| 475 | " ".repeat(usize::from(lead)) |
| 476 | }; |
| 477 | Line::from(vec![ |
| 478 | Span::styled(prefix, theme.fg(prompt_role)), |
| 479 | Span::styled(line, theme.fg(body_role)), |
| 480 | ]) |
| 481 | }) |
| 482 | .collect(); |
| 483 | if lines.is_empty() { |
| 484 | lines.push(Line::from("")); |
| 485 | } |
| 486 | lines |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | impl Paint for Composer<'_> { |
| 491 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 492 | let area = area.intersection(buf.area); |
| 493 | if area.is_empty() { |
| 494 | return; |
| 495 | } |
| 496 | let mut input_area = area; |
| 497 | // Compact viewports give their sole row to text before chrome. |
| 498 | if area.height >= 2 { |
| 499 | let mut horizon = HorizonRule::new(); |
| 500 | if let Some(context) = &self.context { |
| 501 | horizon = horizon.label(context.clone()); |
| 502 | } |
| 503 | horizon.paint(Rect { height: 1, ..area }, buf, theme); |
| 504 | input_area = below(area, 1); |
| 505 | } |
| 506 | let hints = self |
| 507 | .hints |
| 508 | .map(|hints| hints.lines(area.width, theme)) |
| 509 | .unwrap_or_default(); |
| 510 | let input = self.input_lines(input_area.width, theme); |
| 511 | // Reserve a hints rail only when a row of caller text still fits. |
| 512 | let hints_height = rows(hints.len()).min(input_area.height.saturating_sub(1)); |
| 513 | let text_area = Rect { |
| 514 | height: input_area.height.saturating_sub(hints_height), |
| 515 | ..input_area |
| 516 | }; |
| 517 | paint_lines(&input, text_area, buf); |
| 518 | if hints_height > 0 { |
| 519 | paint_lines( |
| 520 | &hints, |
| 521 | Rect { |
| 522 | y: text_area.bottom(), |
| 523 | height: hints_height, |
| 524 | ..input_area |
| 525 | }, |
| 526 | buf, |
| 527 | ); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | fn height(&self, width: u16, theme: &Theme) -> u16 { |
| 532 | if width == 0 { |
| 533 | return 0; |
| 534 | } |
| 535 | let hints = self.hints.map_or(0, |hints| hints.height(width, theme)); |
| 536 | 1_u16 |
| 537 | .saturating_add(rows(self.input_lines(width, theme).len())) |
| 538 | .saturating_add(hints) |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /// One agent's own identity and reported work. The component has no mailbox, |
| 543 | /// timers, provider routing, state transitions or inherited parent task. |
| 544 | #[derive(Clone, Debug)] |
| 545 | pub struct AgentCard<'a> { |
| 546 | pub title: Cow<'a, str>, |
| 547 | pub status: StatusMark, |
| 548 | pub role: Option<Cow<'a, str>>, |
| 549 | pub route: Option<Cow<'a, str>>, |
| 550 | pub task: Cow<'a, str>, |
| 551 | pub elapsed: Option<Cow<'a, str>>, |
| 552 | } |
| 553 | |
| 554 | impl<'a> AgentCard<'a> { |
| 555 | #[must_use] |
| 556 | pub fn new(title: impl Into<Cow<'a, str>>, state: State) -> Self { |
| 557 | Self { |
| 558 | title: title.into(), |
| 559 | status: StatusMark::new(state), |
| 560 | role: None, |
| 561 | route: None, |
| 562 | task: Cow::Borrowed(""), |
| 563 | elapsed: None, |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | #[must_use] |
| 568 | pub fn role(mut self, role: impl Into<Cow<'a, str>>) -> Self { |
| 569 | self.role = Some(role.into()); |
| 570 | self |
| 571 | } |
| 572 | |
| 573 | #[must_use] |
| 574 | pub fn route(mut self, route: impl Into<Cow<'a, str>>) -> Self { |
| 575 | self.route = Some(route.into()); |
| 576 | self |
| 577 | } |
| 578 | |
| 579 | #[must_use] |
| 580 | pub fn task(mut self, task: impl Into<Cow<'a, str>>) -> Self { |
| 581 | self.task = task.into(); |
| 582 | self |
| 583 | } |
| 584 | |
| 585 | #[must_use] |
| 586 | pub fn elapsed(mut self, elapsed: impl Into<Cow<'a, str>>) -> Self { |
| 587 | self.elapsed = Some(elapsed.into()); |
| 588 | self |
| 589 | } |
| 590 | |
| 591 | #[must_use] |
| 592 | pub fn status_word(mut self, word: impl Into<Cow<'static, str>>) -> Self { |
| 593 | self.status = self.status.word(word); |
| 594 | self |
| 595 | } |
| 596 | |
| 597 | fn lines(&self, width: u16, theme: &Theme) -> Vec<Line<'static>> { |
| 598 | if width == 0 { |
| 599 | return Vec::new(); |
| 600 | } |
| 601 | let mut lines = vec![activity_header( |
| 602 | &self.title, |
| 603 | &self.status, |
| 604 | self.elapsed.as_deref(), |
| 605 | theme, |
| 606 | )]; |
| 607 | let identity: Vec<_> = [self.role.as_deref(), self.route.as_deref()] |
| 608 | .into_iter() |
| 609 | .flatten() |
| 610 | .map(safe) |
| 611 | .collect(); |
| 612 | if !identity.is_empty() { |
| 613 | let identity = wrapped(&identity.join(" / "), width, theme.ascii()); |
| 614 | lines.extend( |
| 615 | identity |
| 616 | .lines |
| 617 | .into_iter() |
| 618 | .map(|line| Line::from(Span::styled(line, theme.fg(Role::Muted)))), |
| 619 | ); |
| 620 | } |
| 621 | let rail = width >= 3; |
| 622 | let task = wrapped( |
| 623 | &self.task, |
| 624 | width.saturating_sub(if rail { 2 } else { 0 }), |
| 625 | theme.ascii(), |
| 626 | ); |
| 627 | lines.extend(body_lines(&task, rail, theme, Role::Foreground)); |
| 628 | lines |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | impl Paint for AgentCard<'_> { |
| 633 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 634 | let area = area.intersection(buf.area); |
| 635 | if area.is_empty() { |
| 636 | return; |
| 637 | } |
| 638 | let inner = Panel::new(Depth::Raised).draw(area, buf, theme); |
| 639 | paint_lines(&self.lines(inner.width, theme), inner, buf); |
| 640 | } |
| 641 | |
| 642 | fn height(&self, width: u16, theme: &Theme) -> u16 { |
| 643 | panel_height( |
| 644 | rows(self.lines(panel_width(width, theme), theme).len()), |
| 645 | width, |
| 646 | theme, |
| 647 | ) |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /// A compact fleet rail. Each entry retains its own reported status, role, |
| 652 | /// route, task and elapsed text; no fleet-wide success is inferred. |
| 653 | #[derive(Clone, Debug)] |
| 654 | pub struct Fleet<'a> { |
| 655 | pub title: Cow<'a, str>, |
| 656 | pub agents: Vec<AgentCard<'a>>, |
| 657 | pub empty_label: Cow<'a, str>, |
| 658 | pub omission_label: Cow<'a, str>, |
| 659 | } |
| 660 | |
| 661 | impl<'a> Fleet<'a> { |
| 662 | #[must_use] |
| 663 | pub fn new(agents: Vec<AgentCard<'a>>) -> Self { |
| 664 | Self { |
| 665 | title: Cow::Borrowed("Fleet"), |
| 666 | agents, |
| 667 | empty_label: Cow::Borrowed("No agents reported"), |
| 668 | omission_label: Cow::Borrowed("agents not fully shown"), |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | #[must_use] |
| 673 | pub fn title(mut self, title: impl Into<Cow<'a, str>>) -> Self { |
| 674 | self.title = title.into(); |
| 675 | self |
| 676 | } |
| 677 | |
| 678 | #[must_use] |
| 679 | pub fn empty_label(mut self, label: impl Into<Cow<'a, str>>) -> Self { |
| 680 | self.empty_label = label.into(); |
| 681 | self |
| 682 | } |
| 683 | |
| 684 | #[must_use] |
| 685 | pub fn omission_label(mut self, label: impl Into<Cow<'a, str>>) -> Self { |
| 686 | self.omission_label = label.into(); |
| 687 | self |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | impl Paint for Fleet<'_> { |
| 692 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 693 | let area = area.intersection(buf.area); |
| 694 | if area.is_empty() { |
| 695 | return; |
| 696 | } |
| 697 | let inner = Panel::new(Depth::Deep).draw(area, buf, theme); |
| 698 | if inner.is_empty() { |
| 699 | return; |
| 700 | } |
| 701 | let title = safe(&self.title); |
| 702 | let title_clipped = text::truncate_words(&title, usize::from(inner.width), theme.ascii()); |
| 703 | super::workbench::row( |
| 704 | Rect { height: 1, ..inner }, |
| 705 | buf, |
| 706 | &Line::from(Span::styled( |
| 707 | title_clipped.into_owned(), |
| 708 | theme.fg(Role::Foreground).add_modifier(Modifier::BOLD), |
| 709 | )), |
| 710 | ); |
| 711 | let body = below(inner, 1); |
| 712 | if body.is_empty() { |
| 713 | return; |
| 714 | } |
| 715 | if self.agents.is_empty() { |
| 716 | let empty = safe(&self.empty_label); |
| 717 | let empty_clipped = |
| 718 | text::truncate_words(&empty, usize::from(body.width), theme.ascii()); |
| 719 | super::workbench::row( |
| 720 | Rect { height: 1, ..body }, |
| 721 | buf, |
| 722 | &Line::from(Span::styled( |
| 723 | empty_clipped.into_owned(), |
| 724 | theme.fg(Role::Hint), |
| 725 | )), |
| 726 | ); |
| 727 | return; |
| 728 | } |
| 729 | let mut lines = Vec::new(); |
| 730 | let mut ends = Vec::new(); |
| 731 | for agent in &self.agents { |
| 732 | if !lines.is_empty() { |
| 733 | lines.push(Line::from("")); |
| 734 | } |
| 735 | lines.extend(agent.lines(body.width, theme)); |
| 736 | ends.push(lines.len()); |
| 737 | } |
| 738 | let clipped = lines.len() > usize::from(body.height); |
| 739 | let visible = usize::from(body.height.saturating_sub(u16::from(clipped))); |
| 740 | paint_lines( |
| 741 | &lines, |
| 742 | Rect { |
| 743 | height: rows(visible), |
| 744 | ..body |
| 745 | }, |
| 746 | buf, |
| 747 | ); |
| 748 | if clipped { |
| 749 | let hidden = ends |
| 750 | .len() |
| 751 | .saturating_sub(ends.partition_point(|end| *end <= visible)); |
| 752 | let raw = format!("{hidden} {}", safe(&self.omission_label)); |
| 753 | let footer_clipped = text::truncate_words(&raw, usize::from(body.width), theme.ascii()); |
| 754 | super::workbench::row( |
| 755 | Rect { |
| 756 | y: body.y.saturating_add(rows(visible)), |
| 757 | height: 1, |
| 758 | ..body |
| 759 | }, |
| 760 | buf, |
| 761 | &Line::from(Span::styled( |
| 762 | footer_clipped.into_owned(), |
| 763 | theme.fg(Role::Hint), |
| 764 | )), |
| 765 | ); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | fn height(&self, width: u16, theme: &Theme) -> u16 { |
| 770 | if width == 0 { |
| 771 | return 0; |
| 772 | } |
| 773 | let inner_width = width.saturating_sub(if width >= 8 { 2 } else { 0 }); |
| 774 | let content = if self.agents.is_empty() { |
| 775 | 1 |
| 776 | } else { |
| 777 | self.agents |
| 778 | .iter() |
| 779 | .map(|agent| agent.lines(inner_width, theme).len()) |
| 780 | .sum::<usize>() |
| 781 | .saturating_add(self.agents.len().saturating_sub(1)) |
| 782 | }; |
| 783 | rows(content.saturating_add(1)) |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | // Splitting logical lines before display_safe preserves authored line breaks |
| 788 | // while rejecting terminal controls and bidi overrides within each line. |
| 789 | fn safe(value: &str) -> String { |
| 790 | text::display_safe(value).into_owned() |
| 791 | } |
| 792 | |
| 793 | #[derive(Default)] |
| 794 | struct Wrapped { |
| 795 | lines: Vec<String>, |
| 796 | /// End-exclusive row index and whether every grapheme fitted. |
| 797 | ends: Vec<(usize, bool)>, |
| 798 | } |
| 799 | |
| 800 | impl Wrapped { |
| 801 | fn hidden_lines(&self, visible: usize) -> usize { |
| 802 | self.ends |
| 803 | .iter() |
| 804 | .filter(|(end, complete)| *end > visible || !complete) |
| 805 | .count() |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | fn wrapped(value: &str, width: u16, ascii: bool) -> Wrapped { |
| 810 | let mut result = Wrapped::default(); |
| 811 | if value.is_empty() || width == 0 { |
| 812 | return result; |
| 813 | } |
| 814 | let width = usize::from(width); |
| 815 | for source in value.split('\n') { |
| 816 | let source = safe(source); |
| 817 | let mut current = String::new(); |
| 818 | let mut cells = 0usize; |
| 819 | let mut complete = true; |
| 820 | for grapheme in source.graphemes(true) { |
| 821 | let size = text::width(grapheme); |
| 822 | if cells.saturating_add(size) > width && !current.is_empty() { |
| 823 | // Prefer an authored whitespace boundary. Long words still |
| 824 | // wrap between whole graphemes; indentation stays intact. |
| 825 | let boundary = current |
| 826 | .grapheme_indices(true) |
| 827 | .filter(|(_, cluster)| cluster.chars().all(char::is_whitespace)) |
| 828 | .map(|(index, cluster)| index + cluster.len()) |
| 829 | .next_back() |
| 830 | .filter(|end| { |
| 831 | current[..*end] |
| 832 | .chars() |
| 833 | .any(|character| !character.is_whitespace()) |
| 834 | }); |
| 835 | if let Some(end) = boundary { |
| 836 | let tail = current.split_off(end); |
| 837 | result.lines.push(std::mem::replace(&mut current, tail)); |
| 838 | cells = text::width(¤t); |
| 839 | } else { |
| 840 | result.lines.push(std::mem::take(&mut current)); |
| 841 | cells = 0; |
| 842 | } |
| 843 | if cells.saturating_add(size) > width && !current.is_empty() { |
| 844 | result.lines.push(std::mem::take(&mut current)); |
| 845 | cells = 0; |
| 846 | } |
| 847 | } |
| 848 | if size > width { |
| 849 | // A wide grapheme cannot fit in a one-cell viewport. Show an |
| 850 | // explicit cut mark, never split it or overwrite the neighbor. |
| 851 | result |
| 852 | .lines |
| 853 | .push(text::truncate(grapheme, width, ascii).into_owned()); |
| 854 | complete = false; |
| 855 | } else { |
| 856 | current.push_str(grapheme); |
| 857 | cells = cells.saturating_add(size); |
| 858 | } |
| 859 | } |
| 860 | if !current.is_empty() || source.is_empty() { |
| 861 | result.lines.push(current); |
| 862 | } |
| 863 | result.ends.push((result.lines.len(), complete)); |
| 864 | } |
| 865 | result |
| 866 | } |
| 867 | |
| 868 | fn body_lines(body: &Wrapped, rail: bool, theme: &Theme, role: Role) -> Vec<Line<'static>> { |
| 869 | body.lines |
| 870 | .iter() |
| 871 | .map(|line| { |
| 872 | let mut spans = Vec::new(); |
| 873 | if rail { |
| 874 | spans.push(Span::styled( |
| 875 | glyphs::pick(glyphs::TRANSCRIPT_RAIL, theme.ascii()), |
| 876 | theme.fg(Role::Border), |
| 877 | )); |
| 878 | } |
| 879 | spans.push(Span::styled(line.clone(), theme.fg(role))); |
| 880 | Line::from(spans) |
| 881 | }) |
| 882 | .collect() |
| 883 | } |
| 884 | |
| 885 | fn activity_header( |
| 886 | title: &str, |
| 887 | status: &StatusMark, |
| 888 | elapsed: Option<&str>, |
| 889 | theme: &Theme, |
| 890 | ) -> Line<'static> { |
| 891 | let mut spans = vec![ |
| 892 | Span::styled(status.glyph(theme), theme.fg(status.state.role())), |
| 893 | Span::raw(" "), |
| 894 | Span::styled( |
| 895 | safe(title), |
| 896 | theme.fg(Role::Foreground).add_modifier(Modifier::BOLD), |
| 897 | ), |
| 898 | Span::raw(" "), |
| 899 | Span::styled(safe(&status.word), theme.fg(status.state.role())), |
| 900 | ]; |
| 901 | if let Some(elapsed) = elapsed { |
| 902 | spans.push(Span::raw(" ")); |
| 903 | spans.push(Span::styled(safe(elapsed), theme.fg(Role::Muted))); |
| 904 | } |
| 905 | Line::from(spans) |
| 906 | } |
| 907 | |
| 908 | fn path_subject_line(line: &str, theme: &Theme) -> Line<'static> { |
| 909 | if let Some(cut) = line.rfind(['/', '\\']) { |
| 910 | let dir = &line[..=cut]; |
| 911 | let base = &line[cut + 1..]; |
| 912 | if base.is_empty() { |
| 913 | return Line::from(Span::styled(dir.to_owned(), theme.fg(Role::Muted))); |
| 914 | } |
| 915 | return Line::from(vec![ |
| 916 | Span::styled(dir.to_owned(), theme.fg(Role::Muted)), |
| 917 | Span::styled( |
| 918 | base.to_owned(), |
| 919 | theme.fg(Role::Foreground).add_modifier(Modifier::BOLD), |
| 920 | ), |
| 921 | ]); |
| 922 | } |
| 923 | Line::from(Span::styled( |
| 924 | line.to_owned(), |
| 925 | theme.fg(Role::Foreground).add_modifier(Modifier::BOLD), |
| 926 | )) |
| 927 | } |
| 928 | |
| 929 | fn paint_lines(lines: &[Line<'_>], area: Rect, buf: &mut Buffer) { |
| 930 | if area.is_empty() { |
| 931 | return; |
| 932 | } |
| 933 | for (index, line) in lines.iter().take(usize::from(area.height)).enumerate() { |
| 934 | super::workbench::row( |
| 935 | Rect { |
| 936 | y: area.y.saturating_add(rows(index)), |
| 937 | height: 1, |
| 938 | ..area |
| 939 | }, |
| 940 | buf, |
| 941 | line, |
| 942 | ); |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | fn below(area: Rect, used: u16) -> Rect { |
| 947 | let used = used.min(area.height); |
| 948 | Rect { |
| 949 | y: area.y.saturating_add(used), |
| 950 | height: area.height.saturating_sub(used), |
| 951 | ..area |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | fn rows(count: usize) -> u16 { |
| 956 | u16::try_from(count).unwrap_or(u16::MAX) |
| 957 | } |
| 958 | |
| 959 | fn panel_width(width: u16, theme: &Theme) -> u16 { |
| 960 | let inner = width.saturating_sub(if raised_edged(theme) { 2 } else { 0 }); |
| 961 | inner.saturating_sub(if inner >= 8 { 2 } else { 0 }) |
| 962 | } |
| 963 | |
| 964 | fn panel_height(content: u16, width: u16, theme: &Theme) -> u16 { |
| 965 | if width == 0 { |
| 966 | return 0; |
| 967 | } |
| 968 | if !raised_edged(theme) { |
| 969 | return content; |
| 970 | } |
| 971 | let bordered = content.saturating_add(2); |
| 972 | bordered.saturating_add(if bordered >= 6 { 2 } else { 0 }) |
| 973 | } |
| 974 | |
| 975 | fn raised_edged(theme: &Theme) -> bool { |
| 976 | Panel::new(Depth::Raised).edged(theme) |
| 977 | } |
| 978 |