Skip to main content

trusty_code_tui/
layout.rs

1//! The top-level frame composition — generalized port of tagent's
2//! `repl/tui/layout.rs::draw`.
3//!
4//! Why: DOC-50 §5 Slice 4 migrates the vertical split that arranges the
5//! scrollback, input row, and statusline into one frame. Generalization
6//! dropped two tagent-only rows this crate has no generic equivalent for
7//! yet: the three-row "activity panel" (animated spinner + elapsed timer +
8//! latest thinking step, `repl/tui/layout.rs::draw_activity` /
9//! `status.rs`'s spinner helpers) and the inline choice-picker row (model/
10//! provider/persona pickers, `repl/tui/pickers.rs`) — both are Phase 2/
11//! later-slice concerns per DOC-50 §4.2/§5 Slice 7-9, not part of the MVP
12//! scrollback+input+statusline widget set this slice delivers.
13//!
14//! What: [`draw`] splits the frame into chat (sized to content height, per
15//! [`crate::widgets::scrollback::chat_line_count`]), a separator, the input
16//! row, another separator, and the statusline, with a trailing blank spacer.
17//!
18//! # Spec References
19//! - [`SPEC-TTUI-05~draft`](../../../docs/specs/DOC-50-tcode-tui-claude-code-clone.md#SPEC-TTUI-05~draft) — Slice 4 deliverable (§5, Slice 4): migrate `layout.rs`.
20
21use ratatui::layout::{Constraint, Direction, Layout, Rect};
22use ratatui::style::{Modifier, Style};
23use ratatui::widgets::Paragraph;
24
25use crate::app::ReplApp;
26use crate::widgets::banner::banner_lines;
27use crate::widgets::input_composer::draw_input;
28use crate::widgets::scrollback::{chat_line_count, draw_chat};
29use crate::widgets::status_line::draw_statusline;
30
31/// Render a dim horizontal rule across `area` — the visual divider above and
32/// below the input row.
33///
34/// Why: a single `─`-repeated row gives the borderless input a clear
35/// top/bottom demarcation without the vertical cost of a bordered `Block`.
36pub fn draw_separator(f: &mut ratatui::Frame, area: Rect) {
37    if area.height == 0 || area.width == 0 {
38        return;
39    }
40    let rule: String = "─".repeat(area.width as usize);
41    let p = Paragraph::new(rule).style(Style::default().add_modifier(Modifier::DIM));
42    f.render_widget(p, area);
43}
44
45/// Top-level draw: banner (in-scrollback) + chat (sized to content) +
46/// separator + input + separator + statusline + trailing spacer.
47///
48/// Why: sizing the chat pane to its actual content height (rather than
49/// `Constraint::Min`, which expands to fill the screen) lets the input row
50/// float up directly beneath the last message instead of always being
51/// pinned to the bottom of the terminal — matches tagent's `layout.rs`
52/// rationale (issue #337 upstream).
53/// What: reserves fixed 1-row constraints for both separators, the input
54/// row, and the statusline, then gives the chat pane `min(content_height,
55/// available_height)`. A trailing `Constraint::Min(1)` spacer absorbs
56/// leftover space so nothing gets stretched to fill the terminal.
57/// Test: pure geometry is covered by
58/// [`crate::widgets::scrollback::chat_line_count`]'s own tests; the full
59/// `Frame` composition is exercised via `ratatui::backend::TestBackend` in
60/// `tests::draw_renders_without_panicking`, which is a smoke test (no
61/// TTY, no assertions on cell contents — see that test's doc comment).
62pub fn draw(f: &mut ratatui::Frame, app: &ReplApp) {
63    let area = f.area();
64
65    let content_h = if app.chat.is_empty() && !app.show_banner {
66        0
67    } else if app.chat.is_empty() && app.show_banner {
68        banner_lines(app, area.width as usize).len()
69    } else {
70        chat_line_count(app, area.width as usize).max(1)
71    };
72
73    // Reserved rows below chat: top_sep(1) + input(1) + bot_sep(1) +
74    // statusline(1) + bottom spacer minimum(1).
75    let reserved: u16 = 5;
76    let available_h = area.height.saturating_sub(reserved) as usize;
77    let chat_h = content_h
78        .min(available_h)
79        .max(if content_h == 0 { 0 } else { 1 });
80
81    let chunks = Layout::default()
82        .direction(Direction::Vertical)
83        .constraints([
84            Constraint::Length(chat_h as u16),
85            Constraint::Length(1), // separator above input
86            Constraint::Length(1), // input
87            Constraint::Length(1), // separator below input
88            Constraint::Length(1), // statusline
89            Constraint::Min(1),    // trailing spacer
90        ])
91        .split(area);
92
93    draw_chat(f, app, chunks[0]);
94    draw_separator(f, chunks[1]);
95    draw_input(f, app, chunks[2]);
96    draw_separator(f, chunks[3]);
97    draw_statusline(f, app, chunks[4]);
98    f.render_widget(Paragraph::new(""), chunks[5]);
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104    use ratatui::Terminal;
105    use ratatui::backend::TestBackend;
106
107    /// Smoke test: `draw` must not panic across the shapes that matter most
108    /// — empty app (banner only), populated chat, and a tiny terminal where
109    /// every `Constraint::Length` competes for the same few rows. This does
110    /// NOT assert on cell contents (that's the pure-function tests' job in
111    /// each widget module); it exists purely to catch a layout arithmetic
112    /// panic (e.g. an unchecked subtraction) before it reaches a real TTY.
113    #[test]
114    fn draw_renders_without_panicking() {
115        for (w, h) in [(80u16, 24u16), (120, 40), (10, 3)] {
116            let backend = TestBackend::new(w, h);
117            let mut terminal = Terminal::new(backend).expect("construct terminal");
118            let mut app = ReplApp::new("demo", "bob");
119            terminal.draw(|f| draw(f, &app)).expect("draw empty app");
120
121            app.push_user("hello");
122            app.push_assistant("hi there", false);
123            terminal
124                .draw(|f| draw(f, &app))
125                .expect("draw populated app");
126        }
127    }
128}