use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use crate::app::ReplApp;
use crate::text::elide_middle;
const MAX_RECENT_ACTIVITY: usize = 3;
const MAX_COMMANDS: usize = 4;
const SPLASH_CONTINUATION_INDENT: &str = " ";
const MIN_SPLASH_WIDTH: usize = 12;
fn fit_splash_line(line: &str, width: usize) -> Vec<String> {
if width == 0 {
return vec![String::new()];
}
let mut rows: Vec<String> = Vec::new();
let mut current = String::new();
for word in line.split_whitespace() {
let word = elide_middle(word, width);
let current_len = current.chars().count();
if current_len > 0 && current_len + 1 + word.chars().count() > width {
rows.push(std::mem::take(&mut current));
}
if !current.is_empty() {
current.push(' ');
}
current.push_str(&word);
}
if !current.is_empty() || rows.is_empty() {
rows.push(current);
}
rows
}
pub fn banner_lines(app: &ReplApp, width: usize) -> Vec<Line<'static>> {
let art_width = app
.banner_art
.iter()
.map(|l| l.spans.iter().map(|s| s.content.chars().count()).sum())
.max()
.unwrap_or(0usize);
let total_w = width.max(40);
let left_w = (total_w / 4).max(art_width + 2);
let div_w = 1usize;
let right_w = total_w.saturating_sub(left_w + div_w + 2);
let mut left_rows: Vec<Vec<Span<'static>>> =
app.banner_art.iter().map(|l| l.spans.clone()).collect();
left_rows.push(Vec::new());
left_rows.push(vec![Span::raw(format!(
"{} · {}",
app.user_label, app.label
))]);
let header = Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD);
let mut right_rows: Vec<(String, Style)> = Vec::new();
if app.splash.is_empty() {
right_rows.push((format!(" {} v{}", app.banner_title, app.version), header));
} else {
let wrap_w = right_w
.saturating_sub(SPLASH_CONTINUATION_INDENT.len())
.max(MIN_SPLASH_WIDTH);
for (idx, line) in app.splash.iter().enumerate() {
let style = if idx == 0 { header } else { Style::default() };
for (row, text) in fit_splash_line(line, wrap_w).into_iter().enumerate() {
let indent = if row == 0 {
" "
} else {
SPLASH_CONTINUATION_INDENT
};
right_rows.push((format!("{indent}{text}"), style));
}
}
}
right_rows.push((String::new(), Style::default()));
right_rows.push((
" Recent activity".to_string(),
Style::default().add_modifier(Modifier::BOLD),
));
for entry in app.recent_activity.iter().take(MAX_RECENT_ACTIVITY) {
right_rows.push((format!(" {}", entry), Style::default()));
}
while right_rows.len() < 7 {
right_rows.push((String::new(), Style::default()));
}
right_rows.push((
" Commands".to_string(),
Style::default().add_modifier(Modifier::BOLD),
));
for cmd in app.commands.iter().take(MAX_COMMANDS) {
right_rows.push((
format!(" /{:<12} - {}", cmd.name, cmd.summary),
Style::default(),
));
}
let row_count = left_rows.len().max(right_rows.len());
let mut out: Vec<Line<'static>> = Vec::with_capacity(row_count + 2);
let title = format!("╭─── {} v{} ", app.banner_title, app.version);
let mut top = String::with_capacity(total_w);
top.push_str(&title);
while top.chars().count() + 1 < total_w {
top.push('─');
}
top.push('╮');
out.push(Line::from(Span::styled(
top,
Style::default().fg(Color::Cyan),
)));
for i in 0..row_count {
let left_spans = left_rows.get(i).cloned().unwrap_or_default();
let (right_raw, right_style) = right_rows
.get(i)
.cloned()
.unwrap_or_else(|| (String::new(), Style::default()));
let left_chars: usize = left_spans.iter().map(|s| s.content.chars().count()).sum();
let pad_total = left_w.saturating_sub(left_chars);
let pad_left = pad_total / 2;
let pad_right = pad_total - pad_left;
let mut spans: Vec<Span<'static>> = Vec::with_capacity(left_spans.len() + 4);
if pad_left > 0 {
spans.push(Span::raw(" ".repeat(pad_left)));
}
spans.extend(left_spans);
if pad_right > 0 {
spans.push(Span::raw(" ".repeat(pad_right)));
}
spans.push(Span::styled(" ", Style::default()));
spans.push(Span::styled("│", Style::default().fg(Color::DarkGray)));
spans.push(Span::styled(" ", Style::default()));
let right_truncated: String = right_raw.chars().take(right_w).collect();
spans.push(Span::styled(right_truncated, right_style));
out.push(Line::from(spans));
}
let mut bottom = String::with_capacity(total_w);
bottom.push('╰');
while bottom.chars().count() + 1 < total_w {
bottom.push('─');
}
bottom.push('╯');
out.push(Line::from(Span::styled(
bottom,
Style::default().fg(Color::DarkGray),
)));
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::{CommandDescriptor, CommandRouting};
fn line_text(line: &Line<'_>) -> String {
line.spans.iter().map(|s| s.content.as_ref()).collect()
}
#[test]
fn banner_lines_top_and_bottom_rules_present() {
let app = ReplApp::new("demo-project", "bob");
let lines = banner_lines(&app, 120);
let top = line_text(lines.first().expect("banner must have a top rule"));
let bottom = line_text(lines.last().expect("banner must have a bottom rule"));
assert!(top.starts_with("╭─── demo-project"));
assert!(bottom.starts_with('╰'));
}
#[test]
fn banner_lines_includes_identity_and_title() {
let app = ReplApp::new("demo-project", "bob");
let text: String = banner_lines(&app, 120)
.iter()
.map(line_text)
.collect::<Vec<_>>()
.join("\n");
assert!(text.contains("bob · demo-project"));
assert!(text.contains("demo-project v"));
}
#[test]
fn banner_lines_lists_recent_activity_and_commands() {
let mut app = ReplApp::new("demo-project", "bob");
app.recent_activity = vec!["fixed the bug".to_string()];
app.commands = vec![CommandDescriptor {
name: "workstream".to_string(),
summary: "List or activate a workstream".to_string(),
routing: CommandRouting::Engine,
args_hint: None,
}];
let text: String = banner_lines(&app, 120)
.iter()
.map(line_text)
.collect::<Vec<_>>()
.join("\n");
assert!(text.contains("fixed the bug"));
assert!(text.contains("/workstream"));
assert!(text.contains("List or activate a workstream"));
}
#[test]
fn banner_lines_splash_replaces_the_identity_row() {
let mut app = ReplApp::new("tcode", "bob");
app.version = "9.9.9".to_string();
app.splash = vec![
"🤖🤖🤖 tcode v0.7.0 (ea6a1a9e 2026-09-16)".to_string(),
"project /repo/x".to_string(),
];
let text: String = banner_lines(&app, 120)
.iter()
.map(line_text)
.collect::<Vec<_>>()
.join("\n");
assert!(text.contains("🤖🤖🤖 tcode v0.7.0"), "{text}");
assert!(text.contains("project /repo/x"), "{text}");
let rows: Vec<&str> = text.lines().skip(1).collect();
assert!(
!rows.iter().any(|r| r.contains("tcode v9.9.9")),
"the generic identity row must be gone: {text}"
);
}
#[test]
fn banner_lines_keep_the_mismatch_warning_readable_at_80_columns() {
let mut app = ReplApp::new("tcode", "masa");
app.splash = vec![
"🤖🤖🤖 tcode v0.7.0 (ea6a1a9e 2026-09-16)".to_string(),
"project /Users/masa/trusty-mpm-projects/bobmatnyc/trusty-tools/.claude/worktrees/agent-af14d84dd34577cee".to_string(),
"warning: daemon build deadbeef ≠ client ea6a1a9e — restart the daemon".to_string(),
];
let lines = banner_lines(&app, 80);
let text: String = lines.iter().map(line_text).collect::<Vec<_>>().join("\n");
assert!(
text.contains("deadbeef"),
"the daemon sha must survive: {text}"
);
assert!(
text.contains("ea6a1a9e"),
"the client sha must survive: {text}"
);
assert!(
text.contains("restart the daemon"),
"the remedy must survive: {text}"
);
assert!(
text.contains("worktrees/agent-af14d84dd34577cee"),
"the worktree's own name must survive whole: {text}"
);
for line in &lines {
assert!(
line_text(line).chars().count() <= 80,
"a wrapped row must not overflow the frame: {}",
line_text(line)
);
}
}
#[test]
fn banner_lines_keeps_splash_words_when_the_right_column_collapses() {
let mut app = ReplApp::new("tcode", "masa");
app.banner_art = vec![Line::from("x".repeat(27))];
app.splash = vec!["tcode v0.7.0".to_string(), "project /repo".to_string()];
let text: String = banner_lines(&app, 40)
.iter()
.map(line_text)
.collect::<Vec<_>>()
.join("\n");
assert!(
text.contains("project"),
"a splash word must survive whole: {text}"
);
assert!(
text.contains("tcode v"),
"the header row must stay readable: {text}"
);
assert!(
!text.contains('…'),
"words that fit 12 columns must not be elided: {text}"
);
}
#[test]
fn fit_splash_line_wraps_rather_than_clipping() {
let rows = fit_splash_line("alpha bravo charlie delta", 12);
assert!(rows.len() > 1, "{rows:?}");
assert_eq!(rows.join(" "), "alpha bravo charlie delta");
for row in &rows {
assert!(row.chars().count() <= 12, "{row:?}");
}
}
#[test]
fn fit_splash_line_elides_the_middle_of_an_unbreakable_token() {
let rows = fit_splash_line("/Users/masa/trusty-mpm-projects/bobmatnyc/bakeoff-l1", 24);
assert_eq!(rows.len(), 1, "{rows:?}");
assert!(rows[0].chars().count() <= 24, "{rows:?}");
assert!(rows[0].ends_with("bobmatnyc/bakeoff-l1"), "{rows:?}");
assert!(rows[0].contains('…'), "{rows:?}");
}
#[test]
fn banner_lines_renders_with_no_art_or_commands() {
let app = ReplApp::new("demo-project", "bob");
let lines = banner_lines(&app, 60);
assert!(
lines.len() > 2,
"must render more than just top/bottom rules"
);
}
}