cuj-tui 0.1.0

cui — a read-only TUI browser for cuj vaults
Documentation
//! The bottom line: the ':' editor in Command mode, else the last
//! message or a hint.

use ratatui::Frame;
use ratatui::layout::{Position, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::widgets::Paragraph;

use crate::state::{AppState, Level, Mode};

pub fn draw(f: &mut Frame, area: Rect, state: &AppState) {
    if state.mode == Mode::Command {
        let text = format!(":{}", state.cmdline.buffer);
        f.render_widget(Paragraph::new(text), area);
        let x = area
            .x
            .saturating_add(1 + state.cmdline.cursor as u16)
            .min(area.right().saturating_sub(1));
        f.set_cursor_position(Position::new(x, area.y));
        return;
    }
    match &state.message {
        Some((level, text)) => {
            let style = match level {
                Level::Info => Style::new(),
                Level::Warn => Style::new().fg(Color::Yellow),
                Level::Error => Style::new().fg(Color::Red),
            };
            f.render_widget(Paragraph::new(text.as_str()).style(style), area);
        }
        None => f.render_widget(
            Paragraph::new(" type : for commands, ? for help")
                .style(Style::new().add_modifier(Modifier::DIM)),
            area,
        ),
    }
}