use super::*;
#[derive(Clone, Copy, PartialEq)]
pub(super) enum Target {
Window,
Text,
Tree,
Claude,
Output,
}
pub(super) const NONE: KeyModifiers = KeyModifiers::NONE;
pub(super) const CTRL: KeyModifiers = KeyModifiers::CONTROL;
pub(super) const ALT: KeyModifiers = KeyModifiers::ALT;
#[derive(Clone, Copy, PartialEq)]
pub(super) enum Do {
Key(KeyCode, KeyModifiers),
Help,
Git(usize),
Look(Theme),
}
pub(super) const fn key(c: char, modifiers: KeyModifiers) -> Do {
Do::Key(KeyCode::Char(c), modifiers)
}
pub(super) const COMMANDS: &[(&str, &str, &str, Target, Do)] = &[
("Help", "Show every keyboard shortcut", "F1 twice or Alt+?", Target::Window, Do::Help),
("File", "Open a file by name", "Ctrl+P or Alt+P", Target::Window, key('p', CTRL)),
("File", "Search the whole project", "Alt+S", Target::Window, key('s', ALT)),
("File", "Save", "Ctrl+S", Target::Text, key('s', CTRL)),
("File", "Close this file", "Ctrl+W or Alt+W", Target::Window, key('w', CTRL)),
("File", "Next tab", "Alt+.", Target::Window, key('.', ALT)),
("File", "Previous tab", "Alt+,", Target::Window, key(',', ALT)),
("File", "Go to the first tab (2, 3 ... likewise)", "Alt+1 ... Alt+9", Target::Window, key('1', ALT)),
("File", "Quit", "Ctrl+Q", Target::Window, key('q', CTRL)),
("Tree", "Show or hide the file tree", "Ctrl+B or Alt+B", Target::Window, key('b', CTRL)),
("Tree", "Move between the tree and the text", "Alt+E", Target::Window, key('e', ALT)),
("Tree", "New file", "n in the tree", Target::Tree, key('n', NONE)),
("Tree", "New folder", "f in the tree", Target::Tree, key('f', NONE)),
("Tree", "Rename the selected file or folder", "r or F2 in the tree", Target::Tree, key('r', NONE)),
("Tree", "Delete the selected file or folder", "d or Delete there", Target::Tree, key('d', NONE)),
("Edit", "Undo", "Ctrl+Z", Target::Text, key('z', CTRL)),
("Edit", "Redo", "Ctrl+Y", Target::Text, key('y', CTRL)),
("Edit", "Select text", "Shift+arrows or drag", Target::Text, Do::Key(KeyCode::Right, KeyModifiers::SHIFT)),
("Edit", "Select everything", "Ctrl+A", Target::Text, key('a', CTRL)),
("Edit", "Copy (the line, if nothing is selected)", "Ctrl+C", Target::Text, key('c', CTRL)),
("Edit", "Cut (the line, if nothing is selected)", "Ctrl+X", Target::Text, key('x', CTRL)),
("Edit", "Paste", "Ctrl+V", Target::Text, key('v', CTRL)),
("Edit", "Comment or uncomment lines", "Alt+C or Ctrl+/", Target::Text, key('c', ALT)),
("Edit", "Duplicate the line", "Alt+D", Target::Text, key('d', ALT)),
("Edit", "Move the line up", "Alt+Up", Target::Text, Do::Key(KeyCode::Up, ALT)),
("Edit", "Move the line down", "Alt+Down", Target::Text, Do::Key(KeyCode::Down, ALT)),
("Edit", "Indent the selected lines", "Tab", Target::Text, Do::Key(KeyCode::Tab, NONE)),
("Edit", "Unindent the line or selected lines", "Shift+Tab", Target::Text, Do::Key(KeyCode::BackTab, NONE)),
("Find", "Find in this file", "Ctrl+F or Alt+F", Target::Text, key('f', CTRL)),
("Find", "Find the next match", "F3 or Alt+N", Target::Text, Do::Key(KeyCode::F(3), NONE)),
("Find", "Replace in this file", "Ctrl+R or Alt+R", Target::Text, key('r', CTRL)),
("Find", "Go to a line by number", "Ctrl+L or Alt+L", Target::Text, key('l', CTRL)),
("Find", "Go to the start of the file", "Ctrl+Home", Target::Text, Do::Key(KeyCode::Home, CTRL)),
("Find", "Go to the end of the file", "Ctrl+End", Target::Text, Do::Key(KeyCode::End, CTRL)),
("Code", "Fold or unfold the block here", "Alt+K or Ctrl+K", Target::Text, key('k', ALT)),
("Code", "Fold or unfold every block", "Alt+A", Target::Text, key('a', ALT)),
("Code", "Suggestions from the language server", "Ctrl+Space or Alt+/", Target::Text, key(' ', CTRL)),
("Code", "Explain what is under the cursor", "Alt+H or F2", Target::Text, key('h', ALT)),
("Code", "Go to the definition", "Alt+G or F12", Target::Text, key('g', ALT)),
("Run", "Run a command (build, test, start)", "Ctrl+T", Target::Window, key('t', CTRL)),
("Run", "Run the last command again", "F5", Target::Window, Do::Key(KeyCode::F(5), NONE)),
("Run", "Show or hide what it printed", "Alt+T", Target::Window, key('t', ALT)),
("Run", "Go to the next error it printed", "F8", Target::Window, Do::Key(KeyCode::F(8), NONE)),
("Run", "Go to the error before", "Shift+F8", Target::Window, Do::Key(KeyCode::F(8), KeyModifiers::SHIFT)),
("Run", "Stop the running command", "Ctrl+C in its output", Target::Output, key('c', CTRL)),
("Git", "Open the git menu", "Ctrl+G or Alt+V", Target::Window, key('g', CTRL)),
("Git", "Show what changed in this file", "F4", Target::Window, Do::Git(10)),
("Git", "Show every change in the project", "Shift+F4", Target::Window, Do::Git(11)),
("Git", "Fetch", "F6", Target::Window, Do::Git(9)),
("Git", "Stage this file", "in the git menu", Target::Window, Do::Git(0)),
("Git", "Unstage this file", "in the git menu", Target::Window, Do::Git(1)),
("Git", "Stage all changes", "in the git menu", Target::Window, Do::Git(2)),
("Git", "Unstage everything", "in the git menu", Target::Window, Do::Git(3)),
("Git", "Commit what is staged", "in the git menu", Target::Window, Do::Git(4)),
("Git", "Switch to another branch", "F10, or click it", Target::Window, Do::Git(5)),
("Git", "Make a new branch", "in the git menu", Target::Window, Do::Git(6)),
("Git", "Pull", "F7", Target::Window, Do::Git(7)),
("Git", "Push", "F9", Target::Window, Do::Git(8)),
("Claude", "Ask Claude (show its panel)", "Alt+I", Target::Claude, Do::Key(KeyCode::Null, NONE)),
("Claude", "The panel's own commands", "/ there", Target::Claude, key('/', NONE)),
("Claude", "Attach a file to the question", "@ there", Target::Claude, key('@', NONE)),
("Claude", "New line in the question", "Alt+Enter there", Target::Claude, Do::Key(KeyCode::Enter, ALT)),
("Claude", "What it may do", "Alt+M there", Target::Claude, key('m', ALT)),
("Claude", "Earlier conversations", "Ctrl+R there", Target::Claude, key('r', CTRL)),
("Claude", "New conversation", "Ctrl+N there", Target::Claude, key('n', CTRL)),
("Claude", "Undo its changes to files", "Alt+U there", Target::Claude, key('u', ALT)),
("Claude", "Open the files it changed", "Alt+O there", Target::Claude, key('o', ALT)),
("Claude", "Copy the last answer", "Alt+C there", Target::Claude, key('c', ALT)),
("Claude", "Make the panel as big as it goes", "Alt+Z there", Target::Claude, key('z', ALT)),
("Claude", "Make the panel bigger", "Alt+Up there", Target::Claude, Do::Key(KeyCode::Up, ALT)),
("Claude", "Make the panel smaller", "Alt+Down there", Target::Claude, Do::Key(KeyCode::Down, ALT)),
("Claude", "Stop it, or go back to the text", "Esc there", Target::Claude, Do::Key(KeyCode::Esc, NONE)),
("Claude", "The next assistant's panel", "Alt+Right there", Target::Claude, Do::Key(KeyCode::Right, ALT)),
("Claude", "The assistant before's panel", "Alt+Left there", Target::Claude, Do::Key(KeyCode::Left, ALT)),
("Look", "Dark colours", "CURD_THEME=dark", Target::Window, Do::Look(Theme::DARK)),
("Look", "Light colours", "CURD_THEME=light", Target::Window, Do::Look(Theme::LIGHT)),
("Look", "The terminal's own colours", "CURD_THEME=classic", Target::Window, Do::Look(Theme::CLASSIC)),
];
pub(super) const AGENT_COMMANDS: [(&str, &str); 4] = [
("Open its panel", "Alt+Left / Right there"),
("Sign in", "/login there"),
("Sign out", "/logout there"),
("Say who is signed in", "/account there"),
];
pub(super) const OTHER_KEYS: &[(&str, &str, &str)] = &[
("In any list", "Narrow it down", "type part of a name"),
("In any list", "Choose", "Up / Down, the wheel"),
("In any list", "Pick", "Enter, or a click"),
("In any list", "Close", "Esc, or click outside"),
("Mouse", "Put the cursor there", "click in the text"),
("Mouse", "Select the word", "double click"),
("Mouse", "Fold or unfold a block", "click its - or +"),
("Mouse", "Close a tab", "click x, or middle click"),
("Mouse", "Go through the tabs", "wheel over the tab bar"),
("Mouse", "Go to an error", "click it in the output"),
("Mouse", "Switch branch", "click the branch's name"),
("Mouse", "Another assistant's panel", "click its tab"),
("Mouse", "Resize the Claude panel", "drag its edge"),
];
pub(super) const HELP_COLUMN: usize = 68;
pub(super) const HELP_KEYS: usize = 28;
impl Workspace {
pub(super) fn list_commands(&mut self) {
let mut lines: Vec<String> = COMMANDS
.iter()
.map(|(group, name, keys, ..)| format!("{:<48}{keys}", format!("{group}: {name}")))
.collect();
for spec in &self.agents {
for (what, keys) in AGENT_COMMANDS {
lines.push(format!("{:<48}{keys}", format!("{}: {what}", spec.name)));
}
}
self.pick(Picker::Commands, lines);
}
pub(super) fn run_listed(&mut self, index: usize) {
if let Some(own) = index.checked_sub(COMMANDS.len()) {
self.open_agent(own / AGENT_COMMANDS.len());
return match own % AGENT_COMMANDS.len() {
1 => self.sign(false),
2 => self.sign(true),
3 => self.show_account(),
_ => {}
};
}
let (_, _, _, target, action) = COMMANDS[index];
match target {
Target::Window => {}
Target::Text if self.has_files() => self.focus = Focus::Editor,
Target::Text => return self.say("Open a file first".into()),
Target::Tree => self.show_folder(),
Target::Claude => {
self.show_agent = true;
self.focus = Focus::Agent;
}
Target::Output if self.show_output => self.focus = Focus::Output,
Target::Output => return self.say("No command is running".into()),
}
match action {
Do::Key(KeyCode::Null, _) => {}
Do::Key(code, modifiers) => self.handle_key(KeyEvent::new(code, modifiers)),
Do::Help => self.help = Some(0),
Do::Git(_) if self.git.is_none() => self.say("This folder is not in git".into()),
Do::Git(line) => self.git_command(line),
Do::Look(theme) => self.set_theme(theme),
}
}
pub(super) fn help_lines() -> Vec<(&'static str, &'static str)> {
let commands = COMMANDS.iter().map(|(group, name, keys, ..)| (*group, *name, *keys));
let mut lines = Vec::new();
let mut last = "";
for (group, name, keys) in commands.chain(OTHER_KEYS.iter().copied()) {
if group != last {
if !lines.is_empty() {
lines.push(("", ""));
}
lines.push((group, ""));
last = group;
}
lines.push((name, keys));
}
lines
}
pub(super) fn help_shape(width: usize) -> (usize, usize) {
let columns = (width / HELP_COLUMN).max(1);
(columns, Self::help_lines().len().div_ceil(columns))
}
pub(super) fn help_page(width: usize, height: usize, top: usize) -> Vec<Row> {
let lines = Self::help_lines();
let (columns, rows) = Self::help_shape(width);
let title = " Keyboard shortcuts · Up/Down scroll · Esc closes · F1 lists them to pick from";
let mut page = vec![vec![Span::new(Style::Title, fit(title, width))]];
for row in top..top + height.saturating_sub(1) {
let mut spans = Row::new();
for column in 0..columns {
let cells = if column + 1 == columns { width - column * HELP_COLUMN.min(width) } else { HELP_COLUMN };
match lines.get(column * rows + row).filter(|_| row < rows) {
Some((group, "")) => spans.push(Span::new(Style::Accent, fit(&format!(" {group}"), cells))),
Some((name, keys)) => {
let keys_width = HELP_KEYS.min(cells);
spans.push(Span::new(Style::Bold, fit(&format!(" {keys}"), keys_width)));
spans.push(Span::new(Style::Plain, fit(name, cells - keys_width)));
}
None => spans.push(Span::new(Style::Plain, fit("", cells))),
}
}
page.push(spans);
}
page.truncate(height);
place(&mut page[1.min(height)..], Zone::Panel);
page
}
pub(super) fn help_key(&mut self, key: KeyEvent) {
let Some(top) = self.help else {
return;
};
let page = self.size.1.saturating_sub(2).max(1);
let (_, rows) = Self::help_shape(self.size.0);
let last = rows.saturating_sub(self.size.1.saturating_sub(1));
self.help = match key.code {
KeyCode::Up => Some(top.saturating_sub(1)),
KeyCode::Down => Some((top + 1).min(last)),
KeyCode::PageUp => Some(top.saturating_sub(page)),
KeyCode::PageDown => Some((top + page).min(last)),
KeyCode::Home => Some(0),
KeyCode::End => Some(last),
KeyCode::F(1) => {
self.help = None;
return self.list_commands();
}
_ => None,
};
}
}