use std::collections::HashMap; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; use crate::{action::Action, buffer::{Mode, PartialAction}}; pub struct Config( pub HashMap ); pub struct ModeConfig( pub HashMap, Keybinds> ); pub struct Keybinds( pub HashMap ); #[derive(PartialEq, Eq, Hash)] pub struct Keypress { code: KeyCode, modifiers: KeyModifiers } impl From<[(Mode, ModeConfig); N]> for Config { fn from(array: [(Mode, ModeConfig); N]) -> Self { Self(array.into()) } } impl From<[(Option, Keybinds); N]> for ModeConfig { fn from(array: [(Option, Keybinds); N]) -> Self { Self(array.into()) } } impl From<[(Keypress, Action); N]> for Keybinds { fn from(array: [(Keypress, Action); N]) -> Self { Self(array.into()) } } impl From for Keypress { fn from(key_code: KeyCode) -> Self { Self { code: key_code, modifiers: KeyModifiers::NONE } } } const fn modifier_from_character(character: char) -> Option { match character { 'A' => Some(KeyModifiers::ALT), 'C' => Some(KeyModifiers::CONTROL), _ => None } } impl TryFrom<&str> for Keypress { type Error = (); fn try_from(string: &str) -> Result { match string.len() { 3 => { Ok(Self { code: KeyCode::Char( string.chars().nth(2).unwrap() ), modifiers: modifier_from_character( string.chars().nth(0).unwrap() ).ok_or(())?, }) } 1 => { Ok( KeyCode::Char( string.chars().nth(0).unwrap() ).into() ) } _ => Err(()) } } } impl From for Keypress { fn from(event: KeyEvent) -> Self { Self { code: event.code, modifiers: event.modifiers.difference(KeyModifiers::SHIFT), } } } impl Default for Config { #[allow(clippy::too_many_lines)] fn default() -> Self { [ (Mode::Normal, [ (None, [ ("q".try_into().unwrap(), Action::QuitIfSaved), ("Q".try_into().unwrap(), Action::Quit), ("v".try_into().unwrap(), Action::SelectMode), ("g".try_into().unwrap(), Action::Goto), ("z".try_into().unwrap(), Action::View), ("r".try_into().unwrap(), Action::Replace), (" ".try_into().unwrap(), Action::Space), ("i".try_into().unwrap(), Action::MoveByteUp), ("k".try_into().unwrap(), Action::MoveByteDown), ("j".try_into().unwrap(), Action::MoveByteLeft), ("l".try_into().unwrap(), Action::MoveByteRight), ("G".try_into().unwrap(), Action::GotoFileEnd), ("C-e".try_into().unwrap(), Action::ScrollDown), ("C-y".try_into().unwrap(), Action::ScrollUp), ("C-d".try_into().unwrap(), Action::PageCursorHalfDown), ("C-u".try_into().unwrap(), Action::PageCursorHalfUp), ("C-f".try_into().unwrap(), Action::PageDown), ("C-b".try_into().unwrap(), Action::PageUp), ("w".try_into().unwrap(), Action::MoveNextWordStart), ("e".try_into().unwrap(), Action::MoveNextWordEnd), ("b".try_into().unwrap(), Action::MovePreviousWordStart), (";".try_into().unwrap(), Action::CollapseSelection), ("A-;".try_into().unwrap(), Action::FlipSelections), ("x".try_into().unwrap(), Action::ExtendLineBelow), ("X".try_into().unwrap(), Action::ExtendLineAbove), ("d".try_into().unwrap(), Action::Delete), ("u".try_into().unwrap(), Action::Undo), ("U".try_into().unwrap(), Action::Redo), ("C-j".try_into().unwrap(), Action::PreviousBuffer), ("C-l".try_into().unwrap(), Action::NextBuffer), ("C".try_into().unwrap(), Action::CopySelectionOnNextLine), ("(".try_into().unwrap(), Action::RotateSelectionsBackward), (")".try_into().unwrap(), Action::RotateSelectionsForward), (",".try_into().unwrap(), Action::KeepPrimarySelection), ("A-,".try_into().unwrap(), Action::RemovePrimarySelection), ("1".try_into().unwrap(), Action::SplitSelectionsInto1s), ("2".try_into().unwrap(), Action::SplitSelectionsInto2s), ("3".try_into().unwrap(), Action::SplitSelectionsInto3s), ("4".try_into().unwrap(), Action::SplitSelectionsInto4s), ("5".try_into().unwrap(), Action::SplitSelectionsInto5s), ("6".try_into().unwrap(), Action::SplitSelectionsInto6s), ("7".try_into().unwrap(), Action::SplitSelectionsInto7s), ("8".try_into().unwrap(), Action::SplitSelectionsInto8s), ("9".try_into().unwrap(), Action::SplitSelectionsInto9s), ("J".try_into().unwrap(), Action::JumpToSelectedOffsetRelativeToMark), ("A-J".try_into().unwrap(), Action::JumpToSelectedOffset), ("m".try_into().unwrap(), Action::ToggleMark), ].into()), (Some(PartialAction::Goto), [ ("j".try_into().unwrap(), Action::GotoLineStart), ("l".try_into().unwrap(), Action::GotoLineEnd), ("g".try_into().unwrap(), Action::GotoFileStart), ].into()), (Some(PartialAction::View), [ ("z".try_into().unwrap(), Action::AlignViewCenter), ("b".try_into().unwrap(), Action::AlignViewBottom), ("t".try_into().unwrap(), Action::AlignViewTop), ].into()), (Some(PartialAction::Space), [ ("w".try_into().unwrap(), Action::Save), ].into()), ].into()), (Mode::Select, [ (None, [ ("q".try_into().unwrap(), Action::QuitIfSaved), ("Q".try_into().unwrap(), Action::Quit), ("v".try_into().unwrap(), Action::NormalMode), ("g".try_into().unwrap(), Action::Goto), ("z".try_into().unwrap(), Action::View), ("r".try_into().unwrap(), Action::Replace), (" ".try_into().unwrap(), Action::Space), ("i".try_into().unwrap(), Action::ExtendByteUp), ("k".try_into().unwrap(), Action::ExtendByteDown), ("j".try_into().unwrap(), Action::ExtendByteLeft), ("l".try_into().unwrap(), Action::ExtendByteRight), ("C-e".try_into().unwrap(), Action::ScrollDown), ("C-y".try_into().unwrap(), Action::ScrollUp), ("C-d".try_into().unwrap(), Action::PageCursorHalfDown), ("C-u".try_into().unwrap(), Action::PageCursorHalfUp), ("C-f".try_into().unwrap(), Action::PageDown), ("C-b".try_into().unwrap(), Action::PageUp), ("w".try_into().unwrap(), Action::ExtendNextWordStart), ("e".try_into().unwrap(), Action::ExtendNextWordEnd), ("b".try_into().unwrap(), Action::ExtendPreviousWordStart), (";".try_into().unwrap(), Action::CollapseSelection), ("A-;".try_into().unwrap(), Action::FlipSelections), ("x".try_into().unwrap(), Action::ExtendLineBelow), ("X".try_into().unwrap(), Action::ExtendLineAbove), ("d".try_into().unwrap(), Action::Delete), ("u".try_into().unwrap(), Action::Undo), ("U".try_into().unwrap(), Action::Redo), ("C".try_into().unwrap(), Action::CopySelectionOnNextLine), ("(".try_into().unwrap(), Action::RotateSelectionsBackward), (")".try_into().unwrap(), Action::RotateSelectionsForward), (",".try_into().unwrap(), Action::KeepPrimarySelection), ("A-,".try_into().unwrap(), Action::RemovePrimarySelection), ("1".try_into().unwrap(), Action::SplitSelectionsInto1s), ("2".try_into().unwrap(), Action::SplitSelectionsInto2s), ("3".try_into().unwrap(), Action::SplitSelectionsInto3s), ("4".try_into().unwrap(), Action::SplitSelectionsInto4s), ("5".try_into().unwrap(), Action::SplitSelectionsInto5s), ("6".try_into().unwrap(), Action::SplitSelectionsInto6s), ("7".try_into().unwrap(), Action::SplitSelectionsInto7s), ("8".try_into().unwrap(), Action::SplitSelectionsInto8s), ("9".try_into().unwrap(), Action::SplitSelectionsInto9s), ("J".try_into().unwrap(), Action::JumpToSelectedOffsetRelativeToMark), ("A-J".try_into().unwrap(), Action::JumpToSelectedOffset), ("m".try_into().unwrap(), Action::ToggleMark), ].into()), (Some(PartialAction::View), [ ("z".try_into().unwrap(), Action::AlignViewCenter), ("b".try_into().unwrap(), Action::AlignViewBottom), ("t".try_into().unwrap(), Action::AlignViewTop), ].into()), (Some(PartialAction::Space), [ ("w".try_into().unwrap(), Action::Save), ].into()), ].into()) ].into() } }