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

            
7
use crate::event::Intent;
8
use crate::hits::{Surface, Target};
9
use crate::pane::PaneId;
10
use crate::tabs::nms::ConsoleState;
11
use crate::view::{DrawCtx, Handled};
12
use ratatui::buffer::Buffer;
13
use ratatui::layout::{Constraint, Direction, Layout, Rect};
14
use ratatui::text::{Line, Span};
15
use ratatui::widgets::{Paragraph, Widget, Wrap};
16
use tvui::{Chrome, Role, Theme, inner};
17

            
18
150
fn draw_scrollback(state: &ConsoleState, buf: &mut Buffer, area: Rect) {
19
150
    let visible = usize::from(area.height);
20
150
    let lines: Vec<Line> = state
21
150
        .visible_scrollback(visible)
22
150
        .iter()
23
87
        .map(|l| Line::from(l.as_str()))
24
        .collect();
25
150
    Paragraph::new(lines)
26
150
        .wrap(Wrap { trim: false })
27
150
        .render(area, buf);
28
150
}
29

            
30
150
fn draw_prompt(state: &ConsoleState, buf: &mut Buffer, area: Rect, focused: bool, theme: &Theme) {
31
150
    let marker = if state.pending.is_empty() {
32
149
        "nms> "
33
    } else {
34
1
        "...> "
35
    };
36
150
    let content = format!("{marker}{}", state.input.buffer());
37
150
    let style = theme.style(if focused {
38
4
        Role::Prompt
39
    } else {
40
146
        Role::PanelDim
41
    });
42
150
    Paragraph::new(Span::styled(content, style)).render(area, buf);
43
150
}
44

            
45
impl ConsoleState {
46
150
    pub fn draw(&self, buf: &mut Buffer, area: Rect, ctx: &mut DrawCtx) {
47
150
        let theme = ctx.theme;
48
150
        let title = match ctx.is_active && self.panes.is_focused(PaneId::Scrollback) {
49
1
            true => format!("{} — Scrollback ↕", ctx.title),
50
149
            false => ctx.title.clone(),
51
        };
52
150
        Chrome::new(theme, &title, ctx.frame).render(area, buf);
53
150
        let inner = inner(area);
54

            
55
150
        let chunks = Layout::default()
56
150
            .direction(Direction::Vertical)
57
150
            .constraints([Constraint::Min(1), Constraint::Length(1)])
58
150
            .split(inner);
59

            
60
150
        draw_scrollback(self, buf, chunks[0]);
61
150
        ctx.hits.push(chunks[0], Target::Body(Surface::Pane));
62
150
        draw_prompt(
63
150
            self,
64
150
            buf,
65
150
            chunks[1],
66
150
            ctx.is_active && self.panes.is_focused(PaneId::Prompt),
67
150
            theme,
68
        );
69
150
    }
70

            
71
20
    pub fn handle(&mut self, intent: Intent) -> Handled {
72
20
        match intent {
73
4
            Intent::ScrollLineUp => self.scroll_up(1),
74
2
            Intent::ScrollLineDown => self.scroll_down(1),
75
14
            _ => return Handled::Ignored,
76
        }
77
6
        Handled::Consumed(vec![])
78
20
    }
79
}