use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::widgets::Paragraph;
use crate::app::ReplApp;
use crate::widgets::banner::banner_lines;
use crate::widgets::input_composer::draw_input;
use crate::widgets::scrollback::{chat_line_count, draw_chat};
use crate::widgets::status_line::draw_statusline;
pub fn draw_separator(f: &mut ratatui::Frame, area: Rect) {
if area.height == 0 || area.width == 0 {
return;
}
let rule: String = "─".repeat(area.width as usize);
let p = Paragraph::new(rule).style(Style::default().add_modifier(Modifier::DIM));
f.render_widget(p, area);
}
pub fn draw(f: &mut ratatui::Frame, app: &ReplApp) {
let area = f.area();
let content_h = if app.chat.is_empty() && !app.show_banner {
0
} else if app.chat.is_empty() && app.show_banner {
banner_lines(app, area.width as usize).len()
} else {
chat_line_count(app, area.width as usize).max(1)
};
let reserved: u16 = 5;
let available_h = area.height.saturating_sub(reserved) as usize;
let chat_h = content_h
.min(available_h)
.max(if content_h == 0 { 0 } else { 1 });
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(chat_h as u16),
Constraint::Length(1), Constraint::Length(1), Constraint::Length(1), Constraint::Length(1), Constraint::Min(1), ])
.split(area);
draw_chat(f, app, chunks[0]);
draw_separator(f, chunks[1]);
draw_input(f, app, chunks[2]);
draw_separator(f, chunks[3]);
draw_statusline(f, app, chunks[4]);
f.render_widget(Paragraph::new(""), chunks[5]);
}
#[cfg(test)]
mod tests {
use super::*;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
#[test]
fn draw_renders_without_panicking() {
for (w, h) in [(80u16, 24u16), (120, 40), (10, 3)] {
let backend = TestBackend::new(w, h);
let mut terminal = Terminal::new(backend).expect("construct terminal");
let mut app = ReplApp::new("demo", "bob");
terminal.draw(|f| draw(f, &app)).expect("draw empty app");
app.push_user("hello");
app.push_assistant("hi there", false);
terminal
.draw(|f| draw(f, &app))
.expect("draw populated app");
}
}
}