use crossterm::event::{KeyCode, KeyEvent};
use ratatui::Frame;
use ratatui::style::Style;
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;
use crate::app_view::AppView;
use crate::theme::Theme;
use dugix_core::app::{COMMANDS, Popup};
use super::chrome::{self, PopupFrame, hint, keys};
pub const KEYS: &[(&str, &[(&str, &str)])] = &[
(
"composer",
&[
("Enter", "send"),
("Shift/Ctrl+Enter", "new line"),
("Shift+Tab", "cycle chat / agent / agent·auto"),
("Esc", "stop the reply, or clear the composer"),
("/ @", "command menu · reference a space file"),
(
"!cmd · !tty cmd",
"run a command yourself (output joins the chat) · on the real terminal",
),
("Ctrl+A · Ctrl+X", "select all · cut"),
("Ctrl+Shift+C · Ctrl+V", "copy selection · paste"),
("Ctrl+Backspace", "delete the previous word"),
("Alt+↑/↓", "recall messages you sent this session"),
("Ctrl+C", "back out one step; twice to quit"),
],
),
(
"popups",
&[
("↑↓ PgUp PgDn Home End", "move"),
("Enter", "the main action"),
("^D ^D", "delete, then confirm"),
("Esc", "close, go back, cancel, or clear the filter"),
("type", "filter, where a popup has a filter row"),
],
),
(
"vim keys (on by default; /vim off)",
&[
("Esc · i a I A o O", "Normal mode · back to Insert"),
("h j k l · w b e · 0 ^ $", "move by char · word · line"),
("gg G · { }", "first / last line · paragraph"),
("d c y + motion · dd cc yy", "delete · change · yank"),
("x D C J r ~ · p P", "edit · put"),
("v V · u Ctrl+R · .", "select · undo / redo · repeat"),
("Enter · Esc (Normal)", "send · stop the reply or clear"),
],
),
(
"conversation",
&[
("↑/↓", "scroll (at the composer's edge)"),
("PgUp/PgDn", "scroll a page"),
("Ctrl+Home/End", "jump to the start / latest"),
("mouse wheel", "scroll"),
("drag · double/triple click", "select (copies on release)"),
("long press", "copy the message, code block, or link"),
("Ctrl+R · Ctrl+T", "expand reasoning · expand tool calls"),
("Ctrl+O", "open the selected session link"),
],
),
(
"sessions",
&[
("Ctrl+N", "new session"),
("Ctrl+Shift+N", "new incognito session"),
("Alt+1–4", "reopen a recent session (start screen)"),
("^G", "context breakdown"),
(
"^↑",
"live research view, or this chat's sub-agents (/agents)",
),
(
"Enter while it answers",
"queue the message; it sends when the reply finishes",
),
(
"↑ on an empty line",
"take the newest queued message back to edit (/queue)",
),
],
),
(
"interactive model outputs",
&[
("click controls", "interact directly in the reply"),
("F2", "focus the newest visible UI"),
("Tab/arrows · Enter", "select a control · activate"),
("type · Backspace · Ctrl+V", "edit a focused input"),
("p", "pin the focused UI as a mini-app (open it from /apps)"),
("Esc", "return to chat; keep local UI state"),
],
),
];
pub fn help_lines(theme: &Theme, width: usize) -> Vec<Line<'static>> {
let heading = |s: &str| crate::ui::style::section(theme, s, width);
let row = |left: String, right: &str, left_w: usize| {
Line::from(vec![
Span::styled(
format!(" {left:<left_w$} "),
Style::default().fg(theme.fg),
),
Span::styled(right.to_string(), Style::default().fg(theme.fg_dim)),
])
};
let key_w = KEYS
.iter()
.flat_map(|(_, rows)| rows.iter().map(|(k, _)| crate::ui::style::width(k)))
.max()
.unwrap_or(0);
let mut lines = Vec::new();
for (section, rows) in KEYS {
lines.push(heading(section));
for (keys, what) in *rows {
lines.push(row((*keys).to_string(), what, key_w));
}
lines.push(Line::from(""));
}
lines.push(Line::from(Span::styled(
" drag a popup's border to resize it · drag the column edge to widen the conversation",
Style::default().fg(theme.fg_dim),
)));
lines.push(Line::from(""));
lines.push(heading("commands"));
let cmd_w = COMMANDS
.iter()
.map(|c| crate::ui::style::width(c.name) + 1)
.max()
.unwrap_or(0);
for c in COMMANDS {
lines.push(row(format!("/{}", c.name), c.desc, cmd_w));
}
lines
}
pub fn render(f: &mut Frame, app: &mut AppView) {
let area = chrome::popup_area(app, f.area(), "help", chrome::TALL);
let inner = chrome::render_popup(
f,
area,
&PopupFrame {
title: "Help",
count: "",
hints: &[hint(keys::MOVE, "scroll"), hint(keys::ESC, "close")],
tone: chrome::Tone::Normal,
focused: true,
},
app,
);
let lines = help_lines(&app.theme, inner.width as usize);
let max = u16::try_from(lines.len())
.unwrap_or(u16::MAX)
.saturating_sub(inner.height);
app.help_scroll = app.help_scroll.min(max);
f.render_widget(
Paragraph::new(chrome::fit_lines(lines, inner.width)).scroll((app.help_scroll, 0)),
inner,
);
}
pub fn handle_key(app: &mut AppView, key: KeyEvent) {
match key.code {
KeyCode::Esc => {
app.popup = Popup::None;
}
KeyCode::Up | KeyCode::Char('k') => app.help_scroll = app.help_scroll.saturating_sub(1),
KeyCode::Down | KeyCode::Char('j') => app.help_scroll = app.help_scroll.saturating_add(1),
KeyCode::PageUp => app.help_scroll = app.help_scroll.saturating_sub(10),
KeyCode::PageDown => app.help_scroll = app.help_scroll.saturating_add(10),
KeyCode::Home => app.help_scroll = 0,
KeyCode::End => app.help_scroll = u16::MAX,
_ => {}
}
}