use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum Action {
Up,
Down,
Left,
Right,
WordForward,
WordBack,
WordEnd,
LineStart,
LineEnd,
PageDown,
PageUp,
Top,
Bottom,
Confirm,
Back,
Quit,
Interrupt,
CycleFocus,
Send,
SendAndClose,
Approve,
ToggleTree,
Reload,
OpenInEditor,
PickMessage,
QuickOpen,
PickTarget,
ReviewMenu,
ResendAll,
FinishReview,
UndoFinish,
Archive,
ToggleHidden,
Edit,
Remove,
AttachImage,
RemoveImage,
Visual,
VisualLine,
MoveCursor,
Comment,
ClearBlock,
ClearAll,
LooksGood,
Delete,
#[serde(rename = "copy")]
CopySelection,
CopyLines,
Yes,
No,
Suggest,
FileComment,
ChangeComment,
NextHunk,
PreviousHunk,
NextFile,
PreviousFile,
ToggleLayout,
ToggleWrap,
ToggleWhitespace,
ToggleFiles,
FilterFiles,
FilterKinds,
HideViewed,
ToggleViewed,
NextUnviewed,
PreviousUnviewed,
Help,
NarrowColumn,
WidenColumn,
NarrowFiles,
WidenFiles,
NarrowNotes,
WidenNotes,
WholeFile,
WholeBase,
ToggleNotes,
Search,
NextMatch,
PreviousMatch,
GoToDefinition,
JumpBack,
JumpForward,
DiffSettings,
SplitLeft,
SplitRight,
PickChanges,
}
use Action::{
Approve, Archive, AttachImage, Back, Bottom, ClearAll, ClearBlock, Comment, Confirm, CopyLines,
CopySelection, CycleFocus, Delete, Down, Edit, FinishReview, Interrupt, Left, LineEnd, LineStart,
LooksGood, MoveCursor, No, OpenInEditor, PageDown, PageUp, PickMessage, PickTarget, QuickOpen, Quit,
Reload, Remove, RemoveImage, ResendAll, ReviewMenu, Right, Send, SendAndClose, ToggleHidden, ToggleTree,
Top, UndoFinish, Up, Visual, VisualLine, WordBack, WordEnd, WordForward, Yes,
};
use Action::{
ChangeComment, FileComment, Help, NextFile, NextHunk, NextMatch, NextUnviewed, PreviousFile,
PreviousHunk, PreviousMatch, PreviousUnviewed, Search, Suggest, ToggleFiles, ToggleLayout, ToggleViewed,
ToggleWrap, WholeBase, WholeFile,
};
use Action::{DiffSettings, PickChanges, SplitLeft, SplitRight, ToggleWhitespace};
use Action::{FilterFiles, FilterKinds, GoToDefinition, HideViewed, JumpBack, JumpForward};
use Action::{NarrowColumn, NarrowFiles, NarrowNotes, ToggleNotes, WidenColumn, WidenFiles, WidenNotes};
pub(crate) const GLOBAL: &[Action] = &[
Quit,
Interrupt,
CycleFocus,
Send,
SendAndClose,
Approve,
ReviewMenu,
ResendAll,
FinishReview,
UndoFinish,
Archive,
ToggleTree,
Reload,
OpenInEditor,
QuickOpen,
PickMessage,
PickTarget,
ToggleNotes,
];
pub(crate) const TREE: &[Action] = &[Down, Up, Confirm, Right, ToggleHidden, Back];
pub(crate) const RAIL: &[Action] = &[Down, Up, Confirm, Edit, Remove, AttachImage, RemoveImage, Back];
pub(crate) const DOCUMENT: &[Action] = &[
Back,
Visual,
VisualLine,
MoveCursor,
Down,
Up,
Left,
Right,
PageDown,
PageUp,
Top,
Bottom,
Comment,
Confirm,
ClearBlock,
ClearAll,
AttachImage,
RemoveImage,
NarrowColumn,
WidenColumn,
Search,
NextMatch,
PreviousMatch,
];
pub(crate) const BLOCK_MARKS: &[Action] = &[LooksGood, Delete];
pub(crate) const MOTION: &[Action] =
&[Left, Right, Down, Up, WordForward, WordBack, WordEnd, LineStart, LineEnd];
pub(crate) const VISUAL: &[Action] = &[Back, Confirm, Visual, VisualLine];
pub(crate) const TOOLBAR: &[Action] = &[LooksGood, Comment, Delete, CopySelection];
pub(crate) const CONFIRM_QUIT: &[Action] = &[Yes, Confirm, No, Back];
pub(crate) const CONFIRM_CLEAR: &[Action] = &[Yes, No, Back];
pub(crate) const MENU: &[Action] =
&[Down, Up, Confirm, Back, ReviewMenu, Interrupt, ResendAll, FinishReview, UndoFinish, Archive];
pub(crate) const PICK: &[Action] = &[Down, Up, PageDown, PageUp, Top, Bottom, Confirm, Back, Quit];
pub(crate) const PICK_TARGET: &[Action] = &[Down, Up, Confirm, Back, PickTarget, Interrupt];
pub(crate) const ARCHIVE: &[Action] = &[Down, Up, Confirm, Back, Archive, Quit, Interrupt];
pub(crate) const REVIEW_GLOBAL: &[Action] = &[
Quit,
Interrupt,
CycleFocus,
Send,
SendAndClose,
ReviewMenu,
ResendAll,
FinishReview,
UndoFinish,
ChangeComment,
Reload,
PickTarget,
Help,
ToggleNotes,
NarrowNotes,
WidenNotes,
DiffSettings,
PickChanges,
];
pub(crate) const DIFF: &[Action] = &[
Back,
Visual,
VisualLine,
Comment,
Suggest,
FileComment,
Down,
Up,
PageDown,
PageUp,
Top,
Bottom,
NextHunk,
PreviousHunk,
NextFile,
PreviousFile,
Left,
Right,
ToggleLayout,
ToggleWrap,
ToggleWhitespace,
ToggleFiles,
FilterFiles,
FilterKinds,
HideViewed,
ToggleViewed,
NextUnviewed,
PreviousUnviewed,
WholeFile,
WholeBase,
GoToDefinition,
JumpBack,
JumpForward,
NarrowFiles,
WidenFiles,
SplitLeft,
SplitRight,
Confirm,
CopyLines,
];
pub(crate) const FILES: &[Action] = &[
Down,
Up,
PageDown,
PageUp,
Top,
Bottom,
Confirm,
Back,
Left,
Right,
ToggleFiles,
FilterFiles,
FilterKinds,
HideViewed,
ToggleViewed,
NarrowFiles,
WidenFiles,
];
pub(crate) const KINDS: &[Action] = &[Down, Up, Confirm, Back, FilterKinds, Interrupt];
pub(crate) const NOTES: &[Action] = &[Down, Up, Confirm, Edit, Remove, Back];
pub(crate) const REVIEW_MENU: &[Action] =
&[Down, Up, Confirm, Back, ReviewMenu, Interrupt, ChangeComment, ResendAll, FinishReview, UndoFinish];
pub(crate) const SETTINGS: &[Action] = &[Down, Up, Left, Right, Confirm, Back, DiffSettings, Interrupt];
pub(crate) const PICK_CHANGES: &[Action] =
&[Down, Up, PageDown, PageUp, Top, Bottom, Confirm, Back, Reload, Help, PickChanges, Quit, Interrupt];
pub(crate) const HELP: &[Action] = &[Back, Help, Quit, Interrupt, Down, Up, PageDown, PageUp];
const SHARED: &[&[&[Action]]] = &[
&[GLOBAL, TREE],
&[GLOBAL, RAIL],
&[GLOBAL, DOCUMENT],
&[GLOBAL, MOTION],
&[GLOBAL, VISUAL, MOTION],
&[GLOBAL, TOOLBAR],
&[CONFIRM_QUIT],
&[CONFIRM_CLEAR],
&[MENU],
&[PICK],
&[ARCHIVE],
&[REVIEW_GLOBAL, DIFF],
&[REVIEW_GLOBAL, NOTES],
&[REVIEW_GLOBAL, FILES],
&[KINDS],
&[REVIEW_MENU],
&[SETTINGS],
&[PICK_CHANGES],
&[HELP],
];
const NAMED: [(&str, KeyCode); 13] = [
("Up", KeyCode::Up),
("Down", KeyCode::Down),
("Left", KeyCode::Left),
("Right", KeyCode::Right),
("Home", KeyCode::Home),
("End", KeyCode::End),
("PageUp", KeyCode::PageUp),
("PageDown", KeyCode::PageDown),
("Enter", KeyCode::Enter),
("Esc", KeyCode::Esc),
("Tab", KeyCode::Tab),
("Delete", KeyCode::Delete),
("Backspace", KeyCode::Backspace),
];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Key {
code: KeyCode,
ctrl: bool,
}
const fn ch(c: char) -> Key {
Key { code: KeyCode::Char(c), ctrl: false }
}
const fn ctrl(c: char) -> Key {
Key { code: KeyCode::Char(c), ctrl: true }
}
const fn named(code: KeyCode) -> Key {
Key { code, ctrl: false }
}
impl Key {
fn matches(self, key: &KeyEvent) -> bool {
let ctrl_or_alt = key.modifiers & (KeyModifiers::CONTROL | KeyModifiers::ALT);
let held_ctrl = ctrl_or_alt == KeyModifiers::CONTROL;
let code = match key.code {
KeyCode::Char(c) if c.is_ascii_lowercase() && key.modifiers.contains(KeyModifiers::SHIFT) => {
KeyCode::Char(c.to_ascii_uppercase())
}
code => code,
};
self.code == code && if self.ctrl { key.modifiers == KeyModifiers::CONTROL } else { !held_ctrl }
}
fn hint(self) -> String {
match self.code {
KeyCode::Char(_) => self.to_string(),
_ => self.to_string().to_lowercase(),
}
}
}
impl fmt::Display for Key {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.ctrl {
f.write_str("ctrl+")?;
}
match (self.code, NAMED.iter().find(|(_, code)| *code == self.code)) {
(_, Some((name, _))) => f.write_str(name),
(KeyCode::Char(c), None) => write!(f, "{c}"),
(other, None) => write!(f, "{other:?}"),
}
}
}
impl FromStr for Key {
type Err = anyhow::Error;
fn from_str(s: &str) -> anyhow::Result<Self> {
let (ctrl, rest) = s.strip_prefix("ctrl+").map_or((false, s), |rest| (true, rest));
if let Some((_, code)) = NAMED.iter().find(|(name, _)| name.eq_ignore_ascii_case(rest)) {
return Ok(Self { code: *code, ctrl });
}
let mut chars = rest.chars();
if let (Some(c), None) = (chars.next(), chars.next()) {
return Ok(Self { code: KeyCode::Char(c), ctrl });
}
let names: Vec<&str> = NAMED.iter().map(|(name, _)| *name).collect();
anyhow::bail!(
"unknown key {s:?}; expected one character, ctrl+<character>, or one of {}",
names.join(", ")
)
}
}
impl Serialize for Key {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.collect_str(self)
}
}
impl<'de> Deserialize<'de> for Key {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let text = String::deserialize(deserializer)?;
text.parse().map_err(serde::de::Error::custom)
}
}
const DEFAULTS: &[(Action, &[Key])] = &[
(Up, &[ch('k'), named(KeyCode::Up)]),
(Down, &[ch('j'), named(KeyCode::Down)]),
(Left, &[ch('h'), named(KeyCode::Left)]),
(Right, &[ch('l'), named(KeyCode::Right)]),
(WordForward, &[ch('w')]),
(WordBack, &[ch('b')]),
(WordEnd, &[ch('e')]),
(LineStart, &[ch('0'), named(KeyCode::Home)]),
(LineEnd, &[ch('$'), named(KeyCode::End)]),
(PageDown, &[ctrl('d'), named(KeyCode::PageDown)]),
(PageUp, &[ctrl('u'), named(KeyCode::PageUp)]),
(Top, &[ch('g'), named(KeyCode::Home)]),
(Bottom, &[ch('G'), named(KeyCode::End)]),
(Confirm, &[named(KeyCode::Enter)]),
(Back, &[named(KeyCode::Esc)]),
(Quit, &[ch('q')]),
(Interrupt, &[ctrl('c')]),
(CycleFocus, &[named(KeyCode::Tab)]),
(Send, &[ch('E')]),
(SendAndClose, &[ch('S')]),
(Approve, &[ch('A')]),
(ToggleTree, &[ch('t')]),
(Reload, &[ch('r')]),
(OpenInEditor, &[ch('o')]),
(PickMessage, &[ch('p')]),
(QuickOpen, &[ctrl('p')]),
(PickTarget, &[ch('P')]),
(ReviewMenu, &[ch('m')]),
(ResendAll, &[ch('R')]),
(FinishReview, &[ch('F')]),
(UndoFinish, &[ch('U')]),
(Archive, &[ch('H')]),
(ToggleHidden, &[ch('.')]),
(Edit, &[ch('e')]),
(Remove, &[ch('x'), named(KeyCode::Delete)]),
(AttachImage, &[ch('I')]),
(RemoveImage, &[ch('D')]),
(Visual, &[ch('v')]),
(VisualLine, &[ch('V')]),
(MoveCursor, &[ch('i')]),
(Comment, &[ch('c')]),
(ClearBlock, &[ch('x')]),
(ClearAll, &[ch('X')]),
(LooksGood, &[ch('a')]),
(Delete, &[ch('d')]),
(CopySelection, &[ch('y')]),
(CopyLines, &[ch('y')]),
(Yes, &[ch('y'), ch('Y')]),
(No, &[ch('n'), ch('N')]),
(Suggest, &[ch('s')]),
(FileComment, &[ch('C')]),
(ChangeComment, &[ch('M')]),
(NextHunk, &[ch(']')]),
(PreviousHunk, &[ch('[')]),
(NextFile, &[ch('n')]),
(PreviousFile, &[ch('N')]),
(ToggleLayout, &[ch('u')]),
(ToggleWrap, &[ch('w')]),
(ToggleWhitespace, &[ch('W')]),
(ToggleFiles, &[ch('f')]),
(FilterFiles, &[ch('/')]),
(FilterKinds, &[ch('t')]),
(HideViewed, &[ch('H')]),
(ToggleViewed, &[ch('x')]),
(NextUnviewed, &[ctrl('n')]),
(PreviousUnviewed, &[]),
(Help, &[ch('?')]),
(NarrowColumn, &[ch('<')]),
(WidenColumn, &[ch('>')]),
(NarrowFiles, &[ch('<')]),
(WidenFiles, &[ch('>')]),
(NarrowNotes, &[ch('}')]),
(WidenNotes, &[ch('{')]),
(WholeFile, &[ch('o')]),
(WholeBase, &[ch('O')]),
(ToggleNotes, &[ch('z')]),
(Search, &[ch('/')]),
(NextMatch, &[ch('n')]),
(PreviousMatch, &[ch('N')]),
(GoToDefinition, &[ch('d')]),
(JumpBack, &[ctrl('o')]),
(JumpForward, &[ctrl('i')]),
(DiffSettings, &[ch(',')]),
(SplitLeft, &[ch('(')]),
(SplitRight, &[ch(')')]),
(PickChanges, &[ch('p')]),
];
const LATE: &[Action] = &[
PickChanges,
ToggleWhitespace,
DiffSettings,
SplitLeft,
SplitRight,
CopyLines,
Approve,
CopySelection,
WordEnd,
VisualLine,
Suggest,
FileComment,
NextHunk,
PreviousHunk,
NextFile,
PreviousFile,
ToggleLayout,
ToggleWrap,
ToggleFiles,
FilterFiles,
FilterKinds,
HideViewed,
ToggleViewed,
NextUnviewed,
PreviousUnviewed,
Help,
ChangeComment,
NarrowColumn,
WidenColumn,
NarrowFiles,
WidenFiles,
NarrowNotes,
WidenNotes,
WholeFile,
WholeBase,
ToggleNotes,
Search,
NextMatch,
PreviousMatch,
GoToDefinition,
JumpBack,
JumpForward,
];
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub(crate) struct Keymap(BTreeMap<Action, Vec<Key>>);
impl Default for Keymap {
fn default() -> Self {
Self(DEFAULTS.iter().map(|(action, keys)| (*action, keys.to_vec())).collect())
}
}
impl<'de> Deserialize<'de> for Keymap {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let mut keymap = Self::default();
let entries = BTreeMap::<Action, Vec<Key>>::deserialize(deserializer)?;
let user_keys: Vec<Key> = entries.values().flatten().copied().collect();
let unset: Vec<Action> = LATE.iter().copied().filter(|late| !entries.contains_key(late)).collect();
let copy = entries.get(&CopySelection).filter(|_| !entries.contains_key(&CopyLines)).cloned();
keymap.0.extend(entries);
if let Some(keys) = copy {
keymap.0.insert(CopyLines, keys);
}
for late in unset {
let taken =
keymap.0.get(&late).is_some_and(|keys| keys.iter().any(|key| user_keys.contains(key)));
if taken && keymap.clashes(late) {
keymap.0.insert(late, Vec::new());
}
}
keymap.check().map_err(serde::de::Error::custom)?;
Ok(keymap)
}
}
impl Keymap {
pub(crate) fn action(&self, key: &KeyEvent, context: &[Action]) -> Option<Action> {
context.iter().copied().find(|&action| self.is(action, key))
}
pub(crate) fn is(&self, action: Action, key: &KeyEvent) -> bool {
self.keys(action).iter().any(|k| k.matches(key))
}
pub(crate) fn hint(&self, action: Action) -> String {
self.keys(action).first().map(|key| key.hint()).unwrap_or_default()
}
pub(crate) fn labelled(&self, action: Action, label: &str) -> Option<String> {
let key = self.hint(action);
(!key.is_empty()).then(|| format!("{key} {label}"))
}
pub(crate) fn directions_hint(&self) -> String {
[Left, Down, Up, Right].map(|action| self.hint(action)).concat()
}
fn keys(&self, action: Action) -> &[Key] {
self.0.get(&action).map_or(&[], Vec::as_slice)
}
fn clashes(&self, action: Action) -> bool {
SHARED.iter().any(|contexts| {
let actions: Vec<Action> = contexts.iter().copied().flatten().copied().collect();
actions.contains(&action)
&& self
.keys(action)
.iter()
.any(|key| actions.iter().any(|&other| other != action && self.keys(other).contains(key)))
})
}
fn check(&self) -> Result<(), String> {
for contexts in SHARED {
let mut seen: Vec<(Key, Action)> = Vec::new();
for &action in contexts.iter().copied().flatten() {
for &key in self.keys(action) {
match seen.iter().find(|(k, _)| *k == key) {
Some(&(_, other)) if other != action => {
return Err(format!(
"key {:?} is bound to both {} and {}",
key.to_string(),
other.as_str(),
action.as_str()
));
}
Some(_) => {}
None => seen.push((key, action)),
}
}
}
}
Ok(())
}
}
impl Action {
pub(crate) fn as_str(self) -> &'static str {
match self {
Self::Up => "up",
Self::Down => "down",
Self::Left => "left",
Self::Right => "right",
Self::WordForward => "word-forward",
Self::WordBack => "word-back",
Self::WordEnd => "word-end",
Self::LineStart => "line-start",
Self::LineEnd => "line-end",
Self::PageDown => "page-down",
Self::PageUp => "page-up",
Self::Top => "top",
Self::Bottom => "bottom",
Self::Confirm => "confirm",
Self::Back => "back",
Self::Quit => "quit",
Self::Interrupt => "interrupt",
Self::CycleFocus => "cycle-focus",
Self::Send => "send",
Self::SendAndClose => "send-and-close",
Self::Approve => "approve",
Self::ToggleTree => "toggle-tree",
Self::Reload => "reload",
Self::OpenInEditor => "open-in-editor",
Self::PickMessage => "pick-message",
Self::QuickOpen => "quick-open",
Self::PickTarget => "pick-target",
Self::ReviewMenu => "review-menu",
Self::ResendAll => "resend-all",
Self::FinishReview => "finish-review",
Self::UndoFinish => "undo-finish",
Self::Archive => "archive",
Self::ToggleHidden => "toggle-hidden",
Self::Edit => "edit",
Self::Remove => "remove",
Self::AttachImage => "attach-image",
Self::RemoveImage => "remove-image",
Self::Visual => "visual",
Self::VisualLine => "visual-line",
Self::MoveCursor => "move-cursor",
Self::Comment => "comment",
Self::ClearBlock => "clear-block",
Self::ClearAll => "clear-all",
Self::LooksGood => "looks-good",
Self::Delete => "delete",
Self::CopySelection => "copy",
Self::CopyLines => "copy-lines",
Self::Yes => "yes",
Self::No => "no",
Self::Suggest => "suggest",
Self::FileComment => "file-comment",
Self::ChangeComment => "change-comment",
Self::NextHunk => "next-hunk",
Self::PreviousHunk => "previous-hunk",
Self::NextFile => "next-file",
Self::PreviousFile => "previous-file",
Self::ToggleLayout => "toggle-layout",
Self::ToggleWrap => "toggle-wrap",
Self::ToggleWhitespace => "toggle-whitespace",
Self::ToggleFiles => "toggle-files",
Self::FilterFiles => "filter-files",
Self::FilterKinds => "filter-kinds",
Self::HideViewed => "hide-viewed",
Self::ToggleViewed => "toggle-viewed",
Self::NextUnviewed => "next-unviewed",
Self::PreviousUnviewed => "previous-unviewed",
Self::Help => "help",
Self::NarrowColumn => "narrow-column",
Self::WidenColumn => "widen-column",
Self::NarrowFiles => "narrow-files",
Self::WidenFiles => "widen-files",
Self::NarrowNotes => "narrow-notes",
Self::WidenNotes => "widen-notes",
Self::WholeFile => "whole-file",
Self::WholeBase => "whole-base",
Self::ToggleNotes => "toggle-notes",
Self::Search => "search",
Self::NextMatch => "next-match",
Self::PreviousMatch => "previous-match",
Self::GoToDefinition => "go-to-definition",
Self::JumpBack => "jump-back",
Self::JumpForward => "jump-forward",
Self::DiffSettings => "diff-settings",
Self::SplitLeft => "split-left",
Self::SplitRight => "split-right",
Self::PickChanges => "pick-changes",
}
}
}
#[cfg(test)]
mod tests;