diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-10-15 17:28:01 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-10-15 17:28:01 -0400 |
commit | 1ead7cd5ecd891598d9b61c0e9e01a2c1c0f27af (patch) | |
tree | de3b4d8c179ed0a2d102165aafa40a31cbfb195c /src/config/keymap | |
parent | c0a071f81b2a7a9776dfca313b19ade350c677b2 (diff) |
remove keymapping default code
- just use a big string and parse it at runtime
Diffstat (limited to 'src/config/keymap')
-rw-r--r-- | src/config/keymap/default_keymap.rs | 97 | ||||
-rw-r--r-- | src/config/keymap/keymapping.rs | 176 | ||||
-rw-r--r-- | src/config/keymap/mod.rs | 1 |
3 files changed, 106 insertions, 168 deletions
diff --git a/src/config/keymap/default_keymap.rs b/src/config/keymap/default_keymap.rs new file mode 100644 index 0000000..a8ede07 --- /dev/null +++ b/src/config/keymap/default_keymap.rs @@ -0,0 +1,97 @@ +pub const DEFAULT_KEYMAP: &str = " +mapcommand = [ + { keys = [ \"T\" ], command = \"new_tab\" }, + { keys = [ \"ctrl+t\" ], command = \"new_tab\" }, + { keys = [ \"W\" ], command = \"close_tab\" }, + { keys = [ \"ctrl+w\" ], command = \"close_tab\" }, + { keys = [ \"q\" ], command = \"close_tab\" }, + { keys = [ \"Q\" ], command = \"quit_to_cwd\" }, + + { keys = [ \"R\" ], command = \"reload_dirlist\" }, + { keys = [ \"z\", \"h\" ], command = \"toggle_hidden\" }, + { keys = [ \"\t\" ], command = \"tab_switch 1\" }, + { keys = [ \"backtab\" ], command = \"tab_switch -1\" }, + + { keys = [ \"alt+1\" ], command = \"tab_switch_index 1\" }, + { keys = [ \"alt+2\" ], command = \"tab_switch_index 2\" }, + { keys = [ \"alt+3\" ], command = \"tab_switch_index 3\" }, + { keys = [ \"alt+4\" ], command = \"tab_switch_index 4\" }, + { keys = [ \"alt+5\" ], command = \"tab_switch_index 5\" }, + + # arrow keys + { keys = [ \"arrow_up\" ], command = \"cursor_move_up\" }, + { keys = [ \"arrow_down\" ], command = \"cursor_move_down\" }, + { keys = [ \"arrow_left\" ], command = \"cd ..\" }, + { keys = [ \"arrow_right\" ], command = \"open\" }, + { keys = [ \"\n\" ], command = \"open\" }, + { keys = [ \"end\" ], command = \"cursor_move_end\" }, + { keys = [ \"home\" ], command = \"cursor_move_home\" }, + { keys = [ \"page_up\" ], command = \"cursor_move_page_up\" }, + { keys = [ \"page_down\" ], command = \"cursor_move_page_down\" }, + + # vim-like keybindings + { keys = [ \"j\" ], command = \"cursor_move_down\" }, + { keys = [ \"k\" ], command = \"cursor_move_up\" }, + { keys = [ \"h\" ], command = \"cd ..\" }, + { keys = [ \"l\" ], command = \"open\" }, + { keys = [ \"g\", \"g\" ], command = \"cursor_move_home\" }, + { keys = [ \"G\" ], command = \"cursor_move_end\" }, + { keys = [ \"r\" ], command = \"open_with\" }, + + { keys = [ \"[\" ], command = \"parent_cursor_move_up\" }, + { keys = [ \"]\" ], command = \"parent_cursor_move_down\" }, + + { keys = [ \"c\", \"d\" ], command = \":cd \" }, + { keys = [ \"d\", \"d\" ], command = \"cut_files\" }, + { keys = [ \"y\", \"y\" ], command = \"copy_files\" }, + { keys = [ \"p\", \"p\" ], command = \"paste_files\" }, + { keys = [ \"p\", \"o\" ], command = \"paste_files --overwrite=true\" }, + + { keys = [ \"y\", \"n\" ], command = \"copy_filename\" }, + { keys = [ \"y\", \".\" ], command = \"copy_filename_without_extension\" }, + { keys = [ \"y\", \"p\" ], command = \"copy_filepath\" }, + { keys = [ \"y\", \"d\" ], command = \"copy_dirpath\" }, + + { keys = [ \"delete\" ], command = \"delete_files\" }, + { keys = [ \"d\", \"D\" ], command = \"delete_files\" }, + + { keys = [ \"a\" ], command = \"rename_append\" }, + { keys = [ \"A\" ], command = \"rename_prepend\" }, + + { keys = [ \"f\", \"t\" ], command = \":touch \" }, + + { keys = [ \" \" ], command = \"select --toggle=true\" }, + { keys = [ \"t\" ], command = \"select --all=true --toggle=true\" }, + + { keys = [ \"w\" ], command = \"show_workers --exit-key=w\" }, + { keys = [ \"b\", \"b\" ], command = \"bulk_rename\" }, + { keys = [ \"=\" ], command = \"set_mode\" }, + + { keys = [ \":\" ], command = \":\" }, + { keys = [ \";\" ], command = \":\" }, + + { keys = [ \"'\" ], command = \":shell \" }, + { keys = [ \"m\", \"k\" ], command = \":mkdir \" }, + { keys = [ \"c\", \"w\" ], command = \":rename \" }, + + { keys = [ \"/\" ], command = \":search \" }, + { keys = [ \"\\\" ], command = \":search_glob \" }, + { keys = [ \"S\" ], command = \"search_fzf\" }, + + { keys = [ \"n\" ], command = \"search_next\" }, + { keys = [ \"N\" ], command = \"search_prev\" }, + + { keys = [ \"s\", \"r\" ], command = \"sort reverse\" }, + { keys = [ \"s\", \"l\" ], command = \"sort lexical\" }, + { keys = [ \"s\", \"m\" ], command = \"sort mtime\" }, + { keys = [ \"s\", \"n\" ], command = \"sort natural\" }, + { keys = [ \"s\", \"s\" ], command = \"sort size\" }, + { keys = [ \"s\", \"e\" ], command = \"sort ext\" }, + + { keys = [ \"g\", \"r\" ], command = \"cd /\" }, + { keys = [ \"g\", \"c\" ], command = \"cd ~/.config\" }, + { keys = [ \"g\", \"d\" ], command = \"cd ~/Downloads\" }, + { keys = [ \"g\", \"e\" ], command = \"cd /etc\" }, + { keys = [ \"g\", \"h\" ], command = \"cd ~/\" }, + { keys = [ \"?\" ], command = \"help\" } +]"; diff --git a/src/config/keymap/keymapping.rs b/src/config/keymap/keymapping.rs index 9250c28..0f42e94 100644 --- a/src/config/keymap/keymapping.rs +++ b/src/config/keymap/keymapping.rs @@ -8,10 +8,13 @@ use termion::event::MouseEvent; use termion::event::{Event, Key}; use crate::config::{parse_to_config_file, ConfigStructure, Flattenable}; +use crate::error::JoshutoResult; use crate::io::IoWorkerOptions; use crate::key_command::{Command, CommandKeybind}; use crate::util::keyparse::str_to_event; +use super::default_keymap::DEFAULT_KEYMAP; + #[derive(Debug, Deserialize)] struct CommandKeymap { pub command: String, @@ -78,179 +81,16 @@ impl AppKeyMapping { } } - pub fn default_res(&mut self) -> Result<(), String> { - let mut m = self; - - let cmd = Command::CursorMoveUp(1); - let keys = [Event::Key(Key::Up)]; - insert_keycommand(&mut m, cmd, &keys)?; - let cmd = Command::CursorMoveDown(1); - let keys = [Event::Key(Key::Down)]; - insert_keycommand(&mut m, cmd, &keys)?; - let cmd = Command::ParentDirectory; - let keys = [Event::Key(Key::Left)]; - insert_keycommand(&mut m, cmd, &keys)?; - let cmd = Command::OpenFile; - let keys = [Event::Key(Key::Right)]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::OpenFile; - let keys = [Event::Key(Key::Char('\n'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::CursorMoveHome; - let keys = [Event::Key(Key::Home)]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::CursorMoveEnd; - let keys = [Event::Key(Key::End)]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::CursorMovePageUp; - let keys = [Event::Key(Key::PageUp)]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::CursorMovePageDown; - let keys = [Event::Key(Key::PageDown)]; - insert_keycommand(&mut m, cmd, &keys)?; - - // vim keys - let cmd = Command::CursorMoveUp(1); - let keys = [Event::Key(Key::Char('k'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::CursorMoveDown(1); - let keys = [Event::Key(Key::Char('j'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::ParentDirectory; - let keys = [Event::Key(Key::Char('h'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::OpenFile; - let keys = [Event::Key(Key::Char('l'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::NewTab; - let keys = [Event::Key(Key::Char('T'))]; - insert_keycommand(&mut m, cmd, &keys)?; - let cmd = Command::NewTab; - let keys = [Event::Key(Key::Ctrl('t'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::CloseTab; - let keys = [Event::Key(Key::Char('W'))]; - insert_keycommand(&mut m, cmd, &keys)?; - let cmd = Command::CloseTab; - let keys = [Event::Key(Key::Ctrl('w'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::CloseTab; - let keys = [Event::Key(Key::Char('q'))]; - insert_keycommand(&mut m, cmd, &keys)?; - let cmd = Command::ForceQuit; - let keys = [Event::Key(Key::Char('Q'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::ReloadDirList; - let keys = [Event::Key(Key::Char('R'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::ToggleHiddenFiles; - let keys = [Event::Key(Key::Char('z')), Event::Key(Key::Char('h'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::TabSwitch(1); - let keys = [Event::Key(Key::Char('\t'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::TabSwitch(-1); - let keys = [Event::Key(Key::BackTab)]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::OpenFileWith(None); - let keys = [Event::Key(Key::Char('r'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::CutFiles; - let keys = [Event::Key(Key::Char('d')), Event::Key(Key::Char('d'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::CopyFiles; - let keys = [Event::Key(Key::Char('y')), Event::Key(Key::Char('y'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::PasteFiles(IoWorkerOptions::default()); - let keys = [Event::Key(Key::Char('p')), Event::Key(Key::Char('p'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::DeleteFiles; - let keys = [Event::Key(Key::Delete)]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::DeleteFiles; - let keys = [Event::Key(Key::Char('D')), Event::Key(Key::Char('d'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::RenameFileAppend; - let keys = [Event::Key(Key::Char('a'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::RenameFilePrepend; - let keys = [Event::Key(Key::Char('A'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::CommandLine("search ".to_string(), "".to_string()); - let keys = [Event::Key(Key::Char('/'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::SearchNext; - let keys = [Event::Key(Key::Char('n'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::SearchPrev; - let keys = [Event::Key(Key::Char('N'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::BulkRename; - let keys = [Event::Key(Key::Char('b')), Event::Key(Key::Char('b'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::SetMode; - let keys = [Event::Key(Key::Char('='))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::CommandLine("".to_string(), "".to_string()); - let keys = [Event::Key(Key::Char(';'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::CommandLine("".to_string(), "".to_string()); - let keys = [Event::Key(Key::Char(':'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::CommandLine("mkdir ".to_string(), "".to_string()); - let keys = [Event::Key(Key::Char('m')), Event::Key(Key::Char('k'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::CommandLine("rename ".to_string(), "".to_string()); - let keys = [Event::Key(Key::Char('c')), Event::Key(Key::Char('w'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - let cmd = Command::Help; - let keys = [Event::Key(Key::Char('?'))]; - insert_keycommand(&mut m, cmd, &keys)?; - - Ok(()) + pub fn default_res() -> JoshutoResult<Self> { + let raw: RawAppKeyMapping = toml::from_str(DEFAULT_KEYMAP)?; + let keymapping: Self = raw.flatten(); + Ok(keymapping) } } impl std::default::Default for AppKeyMapping { fn default() -> Self { - let mut m = Self { - map: HashMap::new(), - }; - let _ = m.default_res(); - m + AppKeyMapping::default_res().unwrap() } } diff --git a/src/config/keymap/mod.rs b/src/config/keymap/mod.rs index 14ad630..3ffde21 100644 --- a/src/config/keymap/mod.rs +++ b/src/config/keymap/mod.rs @@ -1,3 +1,4 @@ mod keymapping; +mod default_keymap; pub use self::keymapping::AppKeyMapping; |