use super::app::App;
use crate::dev::events::Status;
use ratatui::{
layout::{Constraint, Layout},
style::{Color, Modifier, Style},
text::{Line, Span},
widgets::{Block, Borders, List, ListItem, Paragraph},
Frame,
};
pub fn draw(frame: &mut Frame, app: &mut App) {
let area = frame.area();
if area.width < 42 || area.height < 9 {
frame.render_widget(
Paragraph::new("Terminal too small\nResize to at least 42 × 9 · q quit"),
area,
);
app.height = 1;
return;
}
let layout = Layout::vertical([
Constraint::Length(2),
Constraint::Min(1),
Constraint::Length(1),
Constraint::Length(1),
])
.split(area);
let title = if area.width >= 85 {
format!(" capy dev • {} {}", app.name, app.path)
} else {
format!(" capy dev • {}", app.name)
};
frame.render_widget(
Paragraph::new(title).style(
Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
),
layout[0],
);
let content = if !app.stream_mode && area.width >= 65 {
let panes =
Layout::horizontal([Constraint::Length(25), Constraint::Min(1)]).split(layout[1]);
let items = app
.contexts
.iter()
.enumerate()
.map(|(i, context)| {
let (symbol, label, color) = match context.status {
Status::Starting => ("◐", "starting".into(), Color::Yellow),
Status::Restarting => ("◐", "restart".into(), Color::Yellow),
Status::Running { .. } => ("●", "running".into(), Color::Green),
Status::Stopped => ("○", "stopped".into(), Color::DarkGray),
Status::Failed { code } => (
"✕",
code.map_or("failed".into(), |c| format!("exit {c}")),
Color::Red,
),
};
let style = if i == app.selected {
Style::default()
.bg(Color::DarkGray)
.add_modifier(Modifier::BOLD)
} else {
Style::default()
};
ListItem::new(vec![
Line::from(vec![
Span::raw(format!(" {} ", i + 1)),
Span::styled(symbol, Style::default().fg(color)),
Span::raw(format!(" {}", context.definition.name)),
]),
Line::from(format!(" {label} · {}", context.output.lines.len())),
])
.style(style)
})
.collect::<Vec<_>>();
frame.render_widget(
List::new(items).block(Block::default().borders(Borders::ALL).title(" contexts ")),
panes[0],
);
panes[1]
} else {
layout[1]
};
let context = &app.contexts[app.selected];
let title = if app.stream_mode {
" all output · stream ".into()
} else {
format!(
" {} · {} ",
context.definition.name, context.definition.command
)
};
let block = Block::default().borders(Borders::ALL).title(title);
let inner = block.inner(content);
app.height = usize::from(inner.height).max(1);
let output = app.output();
let start = output.start(app.height);
let lines = output
.lines
.iter()
.enumerate()
.skip(start)
.take(app.height)
.map(|(i, line)| {
let mut rendered = line.styled.clone();
if app.stream_mode {
let name = app
.contexts
.iter()
.find(|c| c.definition.id == line.source)
.map(|c| c.definition.name.as_str())
.unwrap_or("unknown");
rendered.spans.insert(
0,
Span::styled(
format!("{} {:9} ", line.time, name),
Style::default().fg(Color::Cyan),
),
);
}
if output.matches(&line.text) {
rendered = rendered.style(Style::default().bg(Color::DarkGray));
}
if output.is_found(i) {
rendered = rendered.style(Style::default().add_modifier(Modifier::REVERSED));
}
rendered
})
.collect::<Vec<_>>();
frame.render_widget(
Paragraph::new(lines)
.scroll((0, output.horizontal))
.block(block),
content,
);
let info = if let Some(input) = &app.search_input {
format!(" /{input}▏")
} else if !app.feedback.is_empty() {
format!(" {}", app.feedback)
} else {
let position = if output.anchor.is_none() {
"following"
} else {
"paused · G follows"
};
let url = if app.stream_mode {
""
} else {
context
.definition
.url
.as_deref()
.unwrap_or("application tracing")
};
format!(
" {url} {position} · {}–{} / {}",
if output.lines.is_empty() {
0
} else {
start + 1
},
(start + app.height).min(output.lines.len()),
output.lines.len()
)
};
frame.render_widget(
Paragraph::new(info).style(Style::default().fg(Color::DarkGray)),
layout[2],
);
let footer = if app.search_input.is_some() {
" Enter search · Esc cancel · Ctrl+C quit"
} else if area.width < 80 {
" ↑↓ scroll · 1–9 tabs · / search · s/t · q quit"
} else if app.stream_mode {
" ↑↓/PgUp/PgDn scroll · g/G ends · c clear · / search · n/N match · t tabs · q quit"
} else if context.definition.restartable {
" ↑↓ scroll · PgUp/PgDn · 1–9 tabs · r restart · c clear · / search · s stream · q quit"
} else {
" ↑↓ scroll · PgUp/PgDn · 1–9 tabs · c clear · / search · n/N match · s stream · q quit"
};
frame.render_widget(Paragraph::new(footer), layout[3]);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dev::supervisor::Definition;
#[test]
fn renderer_handles_small_and_resized_terminals() {
for (width, height) in [(0, 0), (10, 3), (42, 9), (80, 24), (120, 40)] {
let mut terminal =
ratatui::Terminal::new(ratatui::backend::TestBackend::new(width, height)).unwrap();
let mut app = App::new(
vec![Definition {
id: 0,
name: "server".into(),
command: "cargo build → serve".into(),
url: None,
restartable: true,
}],
"test".into(),
"/test".into(),
false,
);
terminal.draw(|frame| draw(frame, &mut app)).unwrap();
app.stream_mode = true;
terminal.draw(|frame| draw(frame, &mut app)).unwrap();
}
}
}