Skip to main content

tui/view/
console.rs

1//! Inherent draw + intent handling for `ConsoleState`.
2//!
3//! The draw logic (scrollback + prompt) lives here; the pure state
4//! stays in `tabs::nms`. The console consumes no tab-layer intents (input is
5//! routed via `handle_console` while focused), so `handle` always ignores.
6
7use crate::event::Intent;
8use crate::hits::{Surface, Target};
9use crate::pane::PaneId;
10use crate::tabs::nms::ConsoleState;
11use crate::view::{DrawCtx, Handled};
12use ratatui::buffer::Buffer;
13use ratatui::layout::{Constraint, Direction, Layout, Rect};
14use ratatui::text::{Line, Span};
15use ratatui::widgets::{Paragraph, Widget, Wrap};
16use tvui::{Chrome, Role, Theme, inner};
17
18fn draw_scrollback(state: &ConsoleState, buf: &mut Buffer, area: Rect) {
19    let visible = usize::from(area.height);
20    let lines: Vec<Line> = state
21        .visible_scrollback(visible)
22        .iter()
23        .map(|l| Line::from(l.as_str()))
24        .collect();
25    Paragraph::new(lines)
26        .wrap(Wrap { trim: false })
27        .render(area, buf);
28}
29
30fn draw_prompt(state: &ConsoleState, buf: &mut Buffer, area: Rect, focused: bool, theme: &Theme) {
31    let marker = if state.pending.is_empty() {
32        "nms> "
33    } else {
34        "...> "
35    };
36    let content = format!("{marker}{}", state.input.buffer());
37    let style = theme.style(if focused {
38        Role::Prompt
39    } else {
40        Role::PanelDim
41    });
42    Paragraph::new(Span::styled(content, style)).render(area, buf);
43}
44
45impl ConsoleState {
46    pub fn draw(&self, buf: &mut Buffer, area: Rect, ctx: &mut DrawCtx) {
47        let theme = ctx.theme;
48        let title = match ctx.is_active && self.panes.is_focused(PaneId::Scrollback) {
49            true => format!("{} — Scrollback ↕", ctx.title),
50            false => ctx.title.clone(),
51        };
52        Chrome::new(theme, &title, ctx.frame).render(area, buf);
53        let inner = inner(area);
54
55        let chunks = Layout::default()
56            .direction(Direction::Vertical)
57            .constraints([Constraint::Min(1), Constraint::Length(1)])
58            .split(inner);
59
60        draw_scrollback(self, buf, chunks[0]);
61        ctx.hits.push(chunks[0], Target::Body(Surface::Pane));
62        draw_prompt(
63            self,
64            buf,
65            chunks[1],
66            ctx.is_active && self.panes.is_focused(PaneId::Prompt),
67            theme,
68        );
69    }
70
71    pub fn handle(&mut self, intent: Intent) -> Handled {
72        match intent {
73            Intent::ScrollLineUp => self.scroll_up(1),
74            Intent::ScrollLineDown => self.scroll_down(1),
75            _ => return Handled::Ignored,
76        }
77        Handled::Consumed(vec![])
78    }
79}