diff options
author | qkzk <qu3nt1n@gmail.com> | 2022-12-04 15:28:09 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2022-12-04 15:28:09 +0100 |
commit | 83b7e88d3aadae69d76cf54533e5d83d9e60d03b (patch) | |
tree | 76c0aac3543107b7c3de93f981c79e0e99a8d6f8 | |
parent | 4e43b8c778c86ffa53955dafc81889275852c44c (diff) |
bindsmultiple_kb
-rw-r--r-- | src/actioner.rs | 43 | ||||
-rw-r--r-- | src/config.rs | 260 | ||||
-rw-r--r-- | src/event_char.rs | 79 | ||||
-rw-r--r-- | src/help.rs | 66 | ||||
-rw-r--r-- | src/keybindings.rs | 205 | ||||
-rw-r--r-- | src/main.rs | 6 |
6 files changed, 321 insertions, 338 deletions
diff --git a/src/actioner.rs b/src/actioner.rs index a2277285..372f0275 100644 --- a/src/actioner.rs +++ b/src/actioner.rs @@ -1,9 +1,7 @@ -use std::collections::HashMap; use tuikit::prelude::{Event, Key, MouseButton}; -use crate::config::Keybindings; -use crate::event_char::EventChar; use crate::fm_error::FmResult; +use crate::keybindings::Keybindings; use crate::mode::{MarkAction, Mode}; use crate::status::Status; @@ -12,48 +10,13 @@ use crate::status::Status; /// All keys are mapped to relevent events on tabs.selected(). /// Keybindings are read from `Config`. pub struct Actioner { - binds: HashMap<char, EventChar>, + binds: Keybindings, } impl Actioner { /// Creates a map of configurable keybindings to `EventChar` /// The `EventChar` is then associated to a `tabs.selected(). method. - pub fn new(keybindings: &Keybindings) -> Self { - let binds = HashMap::from([ - (keybindings.toggle_hidden, EventChar::ToggleHidden), - (keybindings.copy_paste, EventChar::CopyPaste), - (keybindings.cut_paste, EventChar::CutPaste), - (keybindings.newdir, EventChar::NewDir), - (keybindings.newfile, EventChar::NewFile), - (keybindings.chmod, EventChar::Chmod), - (keybindings.exec, EventChar::Exec), - (keybindings.goto, EventChar::Goto), - (keybindings.rename, EventChar::Rename), - (keybindings.clear_flags, EventChar::ClearFlags), - (keybindings.toggle_flag, EventChar::ToggleFlag), - (keybindings.shell, EventChar::Shell), - (keybindings.delete, EventChar::DeleteFile), - (keybindings.open_file, EventChar::OpenFile), - (keybindings.help, EventChar::Help), - (keybindings.search, EventChar::Search), - (keybindings.regex_match, EventChar::RegexMatch), - (keybindings.quit, EventChar::Quit), - (keybindings.flag_all, EventChar::FlagAll), - (keybindings.reverse_flags, EventChar::ReverseFlags), - (keybindings.jump, EventChar::Jump), - (keybindings.nvim, EventChar::NvimFilepicker), - (keybindings.sort_by, EventChar::Sort), - (keybindings.symlink, EventChar::Symlink), - (keybindings.preview, EventChar::Preview), - (keybindings.history, EventChar::History), - (keybindings.shortcut, EventChar::Shortcut), - (keybindings.bulkrename, EventChar::Bulkrename), - (keybindings.marks_new, EventChar::MarksNew), - (keybindings.marks_jump, EventChar::MarksJump), - (keybindings.filter, EventChar::Filter), - (keybindings.back, EventChar::Back), - (keybindings.home, EventChar::Home), - ]); + pub fn new(binds: Keybindings) -> Self { Self { binds } } /// Reaction to received events. diff --git a/src/config.rs b/src/config.rs index 17effbf5..c6a4f8d5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,6 +3,8 @@ use std::{fs::File, path}; use serde_yaml; use tuikit::attr::Color; +use crate::keybindings::Keybindings; + /// Holds every configurable aspect of the application. /// All attributes are hardcoded then updated from optional values /// of the config file. @@ -74,6 +76,7 @@ impl Config { fn update_from_config(&mut self, yaml: &serde_yaml::value::Value) { self.colors.update_from_config(&yaml["colors"]); self.keybindings.update_from_config(&yaml["keybindings"]); + self.keybindings.update(&yaml["bindings"]); if let Some(terminal) = yaml["terminal"].as_str().map(|s| s.to_string()) { self.terminal = terminal; } @@ -143,263 +146,6 @@ impl Default for Colors { } } -#[derive(Debug, Clone)] -pub struct Keybindings { - /// Toggling the display between hidden and not - pub toggle_hidden: char, - /// Copy and paste the currently flagged files - pub copy_paste: char, - /// Cut and paste (ie move) the currently flagged files - pub cut_paste: char, - /// Delete the currently flagged files - pub delete: char, - /// Edit the permissions of selected file. - pub chmod: char, - /// Exec a command on the selected file. - pub exec: char, - /// Creates a new directory. - pub newdir: char, - /// Creates a new file. - pub newfile: char, - /// Rename the selected file. - pub rename: char, - /// Empty the flagged files - pub clear_flags: char, - /// Toggle all the selected file as flag and move to next file. - pub toggle_flag: char, - /// Open a terminal in current directory - pub shell: char, - /// Open a file with configured file opener - pub open_file: char, - /// Display the help with default keybindings - pub help: char, - /// Search for a file - pub search: char, - /// Quit the application - pub quit: char, - /// Move to a directory - pub goto: char, - /// Set all files as flagged in current directory - pub flag_all: char, - /// Reverse all the flagged in current directory - pub reverse_flags: char, - /// Flag all files matching a regex - pub regex_match: char, - /// Jump to a flagged directory. - pub jump: char, - /// Pick the current file in NVIM. - /// The application must have been launched from NVIM for it to work. - pub nvim: char, - /// Change file sorting. - pub sort_by: char, - /// Creates asymlink - pub symlink: char, - /// Preview with bat - pub preview: char, - /// Display a stack of visited directories - pub history: char, - /// Display predefined shortcuts - pub shortcut: char, - /// Bulkrename - pub bulkrename: char, - pub marks_new: char, - pub marks_jump: char, - pub filter: char, - pub back: char, - pub home: char, -} - -impl Keybindings { - /// Updates the keybindings from a YAML content. - /// If a bind isn't present, the old value is kept. - fn update_from_config(&mut self, yaml: &serde_yaml::value::Value) { - if let Some(toggle_hidden) = yaml["toggle_hidden"].as_str().map(|s| s.to_string()) { - self.toggle_hidden = toggle_hidden.chars().next().unwrap_or('a'); - } - if let Some(copy_paste) = yaml["copy_paste"].as_str().map(|s| s.to_string()) { - self.copy_paste = copy_paste.chars().next().unwrap_or('c'); - } - if let Some(cut_paste) = yaml["cut_paste"].as_str().map(|s| s.to_string()) { - self.cut_paste = cut_paste.chars().next().unwrap_or('p'); - } - if let Some(delete) = yaml["delete"].as_str().map(|s| s.to_string()) { - self.delete = delete.chars().next().unwrap_or('x'); - } - if let Some(chmod) = yaml["chmod"].as_str().map(|s| s.to_string()) { - self.chmod = chmod.chars().next().unwrap_or('m'); - } - if let Some(exec) = yaml["exec"].as_str().map(|s| s.to_string()) { - self.exec = exec.chars().next().unwrap_or('e'); - } - if let Some(newdir) = yaml["newdir"].as_str().map(|s| s.to_string()) { - self.newdir = newdir.chars().next().unwrap_or('d'); - } - if let Some(newfile) = yaml["newfile"].as_str().map(|s| s.to_string()) { - self.newfile = newfile.chars().next().unwrap_or('n'); - } - if let Some(rename) = yaml["rename"].as_str().map(|s| s.to_string()) { - self.rename = rename.chars().next().unwrap_or('r'); - } - if let Some(clear_flags) = yaml["clear_flags"].as_str().map(|s| s.to_string()) { - self.clear_flags = clear_flags.chars().next().unwrap_or('u'); - } - if let Some(toggle_flag) = yaml["toggle_flag"].as_str().map(|s| s.to_string()) { - self.toggle_flag = toggle_flag.chars().next().unwrap_or(' '); - } - if let Some(shell) = yaml["shell"].as_str().map(|s| s.to_string()) { - self.shell = shell.chars().next().unwrap_or('s'); - } - if let Some(open_file) = yaml["open_file"].as_str().map(|s| s.to_string()) { - self.open_file = open_file.chars().next().unwrap_or('o'); - } - if let Some(help) = yaml["help"].as_str().map(|s| s.to_string()) { - self.help = help.chars().next().unwrap_or('h'); - } - if let Some(search) = yaml["search"].as_str().map(|s| s.to_string()) { - self.search = search.chars().next().unwrap_or('/'); - } - if let Some(quit) = yaml["quit"].as_str().map(|s| s.to_string()) { - self.quit = quit.chars().next().unwrap_or('q'); - } - if let Some(goto) = yaml["goto"].as_str().map(|s| s.to_string()) { - self.goto = goto.chars().next().unwrap_or('g'); - } - if let Some(flag_all) = yaml["flag_all"].as_str().map(|s| s.to_string()) { - self.flag_all = flag_all.chars().next().unwrap_or('*'); - } - if let Some(reverse_flags) = yaml["reverse_flags"].as_str().map(|s| s.to_string()) { - self.reverse_flags = reverse_flags.chars().next().unwrap_or('v'); - } - if let Some(regex_match) = yaml["regex_match"].as_str().map(|s| s.to_string()) { - self.regex_match = regex_match.chars().next().unwrap_or('w'); - } - if let Some(jump) = yaml["jump"].as_str().map(|s| s.to_string()) { - self.jump = jump.chars().next().unwrap_or('j'); - } - if let Some(nvim) = yaml["nvim"].as_str().map(|s| s.to_string()) { - self.nvim = nvim.chars().next().unwrap_or('i'); - } - if let Some(sort_by) = yaml["sort_by"].as_str().map(|s| s.to_string()) { - self.sort_by = sort_by.chars().next().unwrap_or('O'); - } - if let Some(symlink) = yaml["symlink"].as_str().map(|s| s.to_string()) { - self.symlink = symlink.chars().next().unwrap_or('S'); - } - if let Some(preview) = yaml["preview"].as_str().map(|s| s.to_string()) { - self.preview = preview.chars().next().unwrap_or('P'); - } - if let Some(history) = yaml["history"].as_str().map(|s| s.to_string()) { - self.history = history.chars().next().unwrap_or('H'); - } - if let Some(shortcut) = yaml["shortcut"].as_str().map(|s| s.to_string()) { - self.shortcut = shortcut.chars().next().unwrap_or('G'); - } - if let Some(bulkrename) = yaml["bulkrename"].as_str().map(|s| s.to_string()) { - self.bulkrename = bulkrename.chars().next().unwrap_or('B'); - } - if let Some(marks_new) = yaml["marks_new"].as_str().map(|s| s.to_string()) { - self.marks_new = marks_new.chars().next().unwrap_or('M'); - } - if let Some(marks_jump) = yaml["marks_jump"].as_str().map(|s| s.to_string()) { - self.marks_jump = marks_jump.chars().next().unwrap_or('\''); - } - if let Some(filter) = yaml["filter"].as_str().map(|s| s.to_string()) { - self.filter = filter.chars().next().unwrap_or('f'); - } - if let Some(back) = yaml["back"].as_str().map(|s| s.to_string()) { - self.back = back.chars().next().unwrap_or('-'); - } - if let Some(home) = yaml["home"].as_str().map(|s| s.to_string()) { - self.home = home.chars().next().unwrap_or('~'); - } - } - - /// Returns a new `Keybindings` instance with hardcoded values. - fn new() -> Self { - Self { - toggle_hidden: 'a', - copy_paste: 'c', - cut_paste: 'p', - delete: 'x', - chmod: 'm', - exec: 'e', - newdir: 'd', - newfile: 'n', - rename: 'r', - clear_flags: 'u', - toggle_flag: ' ', - shell: 's', - open_file: 'o', - help: 'h', - search: '/', - quit: 'q', - goto: 'g', - flag_all: '*', - reverse_flags: 'v', - regex_match: 'w', - jump: 'j', - nvim: 'i', - sort_by: 'O', - symlink: 'S', - preview: 'P', - history: 'H', - shortcut: 'G', - bulkrename: 'B', - marks_new: 'M', - marks_jump: '\'', - filter: 'f', - back: '-', - home: '~', - } - } - - pub fn to_hashmap(&self) -> std::collections::HashMap<String, String> { - let mut kb = std::collections::HashMap::new(); - - kb.insert("toggle_hidden".to_owned(), self.toggle_hidden.to_string()); - kb.insert("copy_paste".to_owned(), self.copy_paste.to_string()); - kb.insert("cut_paste".to_owned(), self.cut_paste.to_string()); - kb.insert("delete".to_owned(), self.delete.to_string()); - kb.insert("chmod".to_owned(), self.chmod.to_string()); - kb.insert("exec".to_owned(), self.exec.to_string()); - kb.insert("newdir".to_owned(), self.newdir.to_string()); - kb.insert("newfile".to_owned(), self.newfile.to_string()); - kb.insert("rename".to_owned(), self.rename.to_string()); - kb.insert("clear_flags".to_owned(), self.clear_flags.to_string()); - kb.insert("toggle_flag".to_owned(), self.toggle_flag.to_string()); - kb.insert("shell".to_owned(), self.shell.to_string()); - kb.insert("open_file".to_owned(), self.open_file.to_string()); - kb.insert("help".to_owned(), self.help.to_string()); - kb.insert("search".to_owned(), self.search.to_string()); - kb.insert("quit".to_owned(), self.quit.to_string()); - kb.insert("goto".to_owned(), self.goto.to_string()); - kb.insert("flag_all".to_owned(), self.flag_all.to_string()); - kb.insert("reverse_flags".to_owned(), self.reverse_flags.to_string()); - kb.insert("regex_match".to_owned(), self.regex_match.to_string()); - kb.insert("jump".to_owned(), self.jump.to_string()); - kb.insert("nvim".to_owned(), self.nvim.to_string()); - kb.insert("sort_by".to_owned(), self.sort_by.to_string()); - kb.insert("symlink".to_owned(), self.symlink.to_string()); - kb.insert("preview".to_owned(), self.preview.to_string()); - kb.insert("history".to_owned(), self.history.to_string()); - kb.insert("shortcut".to_owned(), self.shortcut.to_string()); - kb.insert("bulkrename".to_owned(), self.bulkrename.to_string()); - kb.insert("marks_new".to_owned(), self.marks_new.to_string()); - kb.insert("marks_jump".to_owned(), self.marks_jump.to_string()); - kb.insert("filter".to_owned(), self.filter.to_string()); - kb.insert("back".to_owned(), self.back.to_string()); - kb.insert("home".to_owned(), self.home.to_string()); - - kb - } -} - -impl Default for Keybindings { - fn default() -> Self { - Self::new() - } -} - /// Convert a string color into a `tuikit::Color` instance. pub fn str_to_tuikit(color: &str) -> Color { match color { diff --git a/src/event_char.rs b/src/event_char.rs index b028c527..5ef8026b 100644 --- a/src/event_char.rs +++ b/src/event_char.rs @@ -1,6 +1,7 @@ use crate::fm_error::FmResult; use crate::status::Status; +#[derive(Clone, Debug)] pub enum EventChar { ToggleHidden, CopyPaste, @@ -38,6 +39,44 @@ pub enum EventChar { } impl EventChar { + pub fn from(s: &str) -> Self { + match s { + "ToggleHidden" => EventChar::ToggleHidden, + "CopyPaste" => EventChar::CopyPaste, + "CutPaste" => EventChar::CutPaste, + "NewDir" => EventChar::NewDir, + "NewFile" => EventChar::NewFile, + "Chmod" => EventChar::Chmod, + "Exec" => EventChar::Exec, + "Goto" => EventChar::Goto, + "Rename" => EventChar::Rename, + "ClearFlags" => EventChar::ClearFlags, + "ToggleFlag" => EventChar::ToggleFlag, + "Shell" => EventChar::Shell, + "DeleteFile" => EventChar::DeleteFile, + "OpenFile" => EventChar::OpenFile, + "Help" => EventChar::Help, + "Search" => EventChar::Search, + "RegexMatch" => EventChar::RegexMatch, + "Quit" => EventChar::Quit, + "FlagAll" => EventChar::FlagAll, + "ReverseFlags" => EventChar::ReverseFlags, + "Jump" => EventChar::Jump, + "History" => EventChar::History, + "NvimFilepicker" => EventChar::NvimFilepicker, + "Sort" => EventChar::Sort, + "Symlink" => EventChar::Symlink, + "Preview" => EventChar::Preview, + "Shortcut" => EventChar::Shortcut, + "Bulkrename" => EventChar::Bulkrename, + "MarksNew" => EventChar::MarksNew, + "MarksJump" => EventChar::MarksJump, + "Filter" => EventChar::Filter, + "Back" => EventChar::Back, + "Home" => EventChar::Home, + _ => panic!("Unreadable command"), + } + } pub fn match_char(&self, status: &mut Status) -> FmResult<()> { let current_tab = status.selected(); match *self { @@ -134,3 +173,43 @@ impl EventChar { } } } + +impl Into<String> for EventChar { + fn into(self) -> String { + match self { + EventChar::ToggleHidden => "ToggleHidden".to_owned(), + EventChar::CopyPaste => "CopyPaste".to_owned(), + EventChar::CutPaste => "CutPaste".to_owned(), + EventChar::NewDir => "NewDir".to_owned(), + EventChar::NewFile => "NewFile".to_owned(), + EventChar::Chmod => "Chmod".to_owned(), + EventChar::Exec => "Exec".to_owned(), + EventChar::Goto => "Goto".to_owned(), + EventChar::Rename => "Rename".to_owned(), + EventChar::ClearFlags => "ClearFlags".to_owned(), + EventChar::ToggleFlag => "ToggleFlag".to_owned(), + EventChar::Shell => "Shell".to_owned(), + EventChar::DeleteFile => "DeleteFile".to_owned(), + EventChar::OpenFile => "OpenFile".to_owned(), + EventChar::Help => "Help".to_owned(), + EventChar::Search => "Search".to_owned(), + EventChar::RegexMatch => "RegexMatch".to_owned(), + EventChar::Quit => "Quit".to_owned(), + EventChar::FlagAll => "FlagAll".to_owned(), + EventChar::ReverseFlags => "ReverseFlags".to_owned(), + EventChar::Jump => "Jump".to_owned(), + EventChar::History => "History".to_owned(), + EventChar::NvimFilepicker => "NvimFilepicker".to_owned(), + EventChar::Sort => "Sort".to_owned(), + EventChar::Symlink => "Symlink".to_owned(), + EventChar::Preview => "Preview".to_owned(), + EventChar::Shortcut => "Shortcut".to_owned(), + EventChar::Bulkrename => "Bulkrename".to_owned(), + EventChar::MarksNew => "MarksNew".to_owned(), + EventChar::MarksJump => "MarksJump".to_owned(), + EventChar::Filter => "Filter".to_owned(), + EventChar::Back => "Back".to_owned(), + EventChar::Home => "Home".to_owned(), + } + } +} diff --git a/src/help.rs b/src/help.rs index 53a0a60c..8c0fe8e4 100644 --- a/src/help.rs +++ b/src/help.rs @@ -1,7 +1,7 @@ use strfmt::strfmt; -use crate::config::Keybindings; use crate::fm_error::FmResult; +use crate::keybindings::Keybindings; /// Help message to be displayed when help key is pressed. /// Default help key is `'h'`. @@ -13,8 +13,8 @@ pub struct Help { impl Help { pub fn from_keybindings(keybindings: &Keybindings) -> FmResult<Self> { let help_to_format = " -{quit}: quit -{help}: help +{Quit}: quit +{Help}: help - Navigation - ←: cd to parent directory @@ -27,47 +27,47 @@ PgUp: 10 lines up PgDown: 10 lines down Tab: Cycle tab -{toggle_hidden}: toggle hidden -{shell}: shell in current directory -{open_file}: open this file -{nvim}: open in current nvim session -{preview}: preview this file +{ToggleHidden}: toggle hidden +{Shell}: shell in current directory +{OpenFile}: open this file +{NvimFilepicker}: open in current nvim session +{Preview}: preview this file Ctrl+f: fuzzy finder Ctrl+r: refresh view Ctrl+c: copy filename to clipboard Ctrl+p: copy filepath to clipboard Ctrl+x: decompress selected file Alt+d: dragon-drop selected file -{marks_new}: Mark current path -{marks_jump}: Jump to a mark -{back}: Move back to previous dir -{home}: Move to $HOME +{MarksNew}: Mark current path +{MarksJump}: Jump to a mark +{Back}: Move back to previous dir +{Home}: Move to $HOME - Action on flagged files - space: toggle flag on a file -{flag_all}: flag all -{clear_flags}: clear flags -{reverse_flags}: reverse flags -{copy_paste}: copy to current dir -{cut_paste}: move to current dir -{delete}: delete files -{symlink}: symlink files -{bulkrename}: Bulkrename files +{FlagAll}: flag all +{ClearFlags}: clear flags +{ReverseFlags}: reverse flags +{CopyPaste}: copy to current dir +{CutPaste}: move to current dir +{DeleteFile}: delete files +{Symlink}: symlink files +{Bulkrename}: Bulkrename files - MODES - - {chmod}: CHMOD - {exec}: EXEC - {newdir}: NEWDIR - {newfile}: NEWFILE - {rename}: RENAME - {goto}: GOTO - {regex_match}: REGEXMATCH - {jump}: JUMP - {sort_by}: SORT - {history}: HISTORY - {shortcut}: SHORTCUT - {search}: SEARCH - {filter}: FILTER + {Chmod}: CHMOD + {Exec}: EXEC + {NewDir}: NEWDIR + {NewFile}: NEWFILE + {Rename}: RENAME + {Goto}: GOTO + {RegexMatch}: REGEXMATCH + {Jump}: JUMP + {Sort}: SORT + {History}: HISTORY + {Shortcut}: SHORTCUT + {Search}: SEARCH + {Filter}: FILTER (by name \"n name\", by ext \"e ext\", only directories d or all for reset) Enter: Execute mode then NORMAL Esc: NORMAL diff --git a/src/keybindings.rs b/src/keybindings.rs index 878e5806..ac1c8922 100644 --- a/src/keybindings.rs +++ b/src/keybindings.rs @@ -2,20 +2,211 @@ use std::collections::HashMap; use crate::event_char::EventChar; -pub struct KeyBinds { +#[derive(Clone, Debug)] +pub struct Keybindings { pub binds: HashMap<char, EventChar>, } -impl KeyBinds { - pub fn new(binds: HashMap<char, EventChar>) -> Self { +impl Default for Keybindings { + fn default() -> Self { + Self::new() + } +} + +impl Keybindings { + pub fn get(&self, key: &char) -> Option<&EventChar> { + self.binds.get(key) + } + + pub fn new() -> Self { + let binds = HashMap::from([ + ('a', EventChar::ToggleHidden), + ('c', EventChar::CopyPaste), + ('p', EventChar::CutPaste), + ('d', EventChar::NewDir), + ('n', EventChar::NewFile), + ('m', EventChar::Chmod), + ('e', EventChar::Exec), + ('g', EventChar::Goto), + ('r', EventChar::Rename), + ('u', EventChar::ClearFlags), + (' ', EventChar::ToggleFlag), + ('s', EventChar::Shell), + ('x', EventChar::DeleteFile), + ('o', EventChar::OpenFile), + ('h', EventChar::Help), + ('/', EventChar::Search), + ('w', EventChar::RegexMatch), + ('q', EventChar::Quit), + ('*', EventChar::FlagAll), + ('v', EventChar::ReverseFlags), + ('j', EventChar::Jump), + ('H', EventChar::History), + ('i', EventChar::NvimFilepicker), + ('O', EventChar::Sort), + ('l', EventChar::Symlink), + ('P', EventChar::Preview), + ('G', EventChar::Shortcut), + ('B', EventChar::Bulkrename), + ('M', EventChar::MarksNew), + ('\'', EventChar::MarksJump), + ('F', EventChar::Filter), + ('-', EventChar::Back), + ('~', EventChar::Home), + ]); Self { binds } } - pub fn update(&mut self, key: char, event: EventChar) { - self.binds.insert(key, event); + pub fn update_from_config(&mut self, yaml: &serde_yaml::value::Value) { + if let Some(toggle_hidden) = yaml["toggle_hidden"].as_str().map(|s| s.to_string()) { + let key = toggle_hidden.chars().next().unwrap_or('a'); + self.binds.insert(key, EventChar::ToggleHidden); + } + if let Some(copy_paste) = yaml["copy_paste"].as_str().map(|s| s.to_string()) { + let key = copy_paste.chars().next().unwrap_or('c'); + self.binds.insert(key, EventChar::CopyPaste); + } + if let Some(cut_paste) = yaml["cut_paste"].as_str().map(|s| s.to_string()) { + let key = cut_paste.chars().next().unwrap_or('p'); + self.binds.insert(key, EventChar::CutPaste); + } + if let Some(delete) = yaml["delete"].as_str().map(|s| s.to_string()) { + let key = delete.chars().next().unwrap_or('x'); + self.binds.insert(key, EventChar::DeleteFile); + } + if let Some(chmod) = yaml["chmod"].as_str().map(|s| s.to_string()) { + let key = chmod.chars().next().unwrap_or('m'); + self.binds.insert(key, EventChar::Chmod); + } + if let Some(exec) = yaml["exec"].as_str().map(|s| s.to_string()) { + let key = exec.chars().next().unwrap_or('e'); + self.binds.insert(key, EventChar::Exec); + } + if let Some(newdir) = yaml["newdir"].as_str().map(|s| s.to_string()) { + let key = newdir.chars().next().unwrap_or('d'); + self.binds.insert(key, EventChar::NewDir); + } + if let Some(newfile) = yaml["newfile"].as_str().map(|s| s.to_string()) { + let key = newfile.chars().next().unwrap_or('n'); + self.binds.insert(key, EventChar::NewFile); + } + if let Some(rename) = yaml["rename"].as_str().map(|s| s.to_string()) { + let key = rename.chars().next().unwrap_or('r'); + self.binds.insert(key, EventChar::Rename); + } + if let Some(clear_flags) = yaml["clear_flags"].as_str().map(|s| s.to_string()) { + let key = clear_flags.chars().next().unwrap_or('u'); + self.binds.insert(key, EventChar::ClearFlags); + } + if let Some(toggle_flag) = yaml["toggle_flag"].as_str().map(|s| s.to_string()) { + let key = toggle_flag.chars().next().unwrap_or(' '); + self.binds.insert(key, EventChar::ToggleFlag); + } + if let Some(shell) = yaml["shell"].as_str().map(|s| s.to_string()) { + let key = shell.chars().next().unwrap_or('s'); + self.binds.insert(key, EventChar::Shell); + } + if let Some(open_file) = yaml["open_file"].as_str().map(|s| s.to_string()) { + let key = open_file.chars().next().unwrap_or('o'); + self.binds.insert(key, EventChar::OpenFile); + } + if let Some(help) = yaml["help"].as_str().map(|s| s.to_string()) { + let key = help.chars().next().unwrap_or('h'); + self.binds.insert(key, EventChar::Help); + } + if let Some(search) = yaml["search"].as_str().map(|s| s.to_string()) { + let key = search.chars().next().unwrap_or('/'); + self.binds.insert(key, EventChar::Search); + } + if let Some(quit) = yaml["quit"].as_str().map(|s| s.to_string()) { + let key = quit.chars().next().unwrap_or('q'); + self.binds.insert(key, EventChar::Quit); + } + if let Some(goto) = yaml["goto"].as_str().map(|s| s.to_string()) { + let key = goto.chars().next().unwrap_or('g'); + self.binds.insert(key, EventChar::Goto); + } + if let Some(flag_all) = yaml["flag_all"].as_str().map(|s| s.to_string()) { + let key = flag_all.chars().next().unwrap_or('*'); + self.binds.insert(key, EventChar::FlagAll); + } + if let Some(reverse_flags) = yaml["reverse_flags"].as_str().map(|s| s.to_string()) { + let key = reverse_flags.chars().next().unwrap_or('v'); + self.binds.insert(key, EventChar::ReverseFlags); + } + if let Some(regex_match) = yaml["regex_match"].as_str().map(|s| s.to_string()) { + let key = regex_match.chars().next().unwrap_or('w'); + self.binds.insert(key, EventChar::RegexMatch); + } + if let Some(jump) = yaml["jump"].as_str().map(|s| s.to_string()) { + let key = jump.chars().next().unwrap_or('j'); + self.binds.insert(key, EventChar::Jump); + } + if let Some(nvim) = yaml["nvim"].as_str().map(|s| s.to_string()) { + let key = nvim.chars().next().unwrap_or('i'); + self.binds.insert(key, EventChar::NvimFilepicker); + } + if let Some(sort_by) = yaml["sort_by"].as_str().map(|s| s.to_string()) { + let key = sort_by.chars().next().unwrap_or('O'); + self.binds.insert(key, EventChar::Sort); + } + if let Some(symlink) = yaml["symlink"].as_str().map(|s| s.to_string()) { + let key = symlink.chars().next().unwrap_or('S'); + self.binds.insert(key, EventChar::Symlink); + } + if let Some(preview) = yaml["preview"].as_str().map(|s| s.to_string()) { + let key = preview.chars().next().unwrap_or('P'); + self.binds.insert(key, EventChar::Preview); + } + if let Some(history) = yaml["history"].as_str().map(|s| s.to_string()) { + let key = history.chars().next().unwrap_or('H'); + self.binds.insert(key, EventChar::History); + } + if let Some(shortcut) = yaml["shortcut"].as_str().map(|s| s.to_string()) { + let key = shortcut.chars().next().unwrap_or('G'); + self.binds.insert(key, EventChar::Shortcut); + } + if let Some(bulkrename) = yaml["bulkrename"].as_str().map(|s| s.to_string()) { + let key = bulkrename.chars().next().unwrap_or('B'); + self.binds.insert(key, EventChar::Bulkrename); + } + if let Some(marks_new) = yaml["marks_new"].as_str().map(|s| s.to_string()) { + let key = marks_new.chars().next().unwrap_or('M'); + self.binds.insert(key, EventChar::MarksNew); + } + if let Some(marks_jump) = yaml["marks_jump"].as_str().map(|s| s.to_string()) { + let key = marks_jump.chars().next().unwrap_or('\''); + self.binds.insert(key, EventChar::MarksJump); + } + if let Some(filter) = yaml["filter"].as_str().map(|s| s.to_string()) { + let key = filter.chars().next().unwrap_or('f'); + self.binds.insert(key, EventChar::Filter); + } + if let Some(back) = yaml["back"].as_str().map(|s| s.to_string()) { + let key = back.chars().next().unwrap_or('-'); + self.binds.insert(key, EventChar::Back); + } + if let Some(home) = yaml["home"].as_str().map(|s| s.to_string()) { + let key = home.chars().next().unwrap_or('~'); + self.binds.insert(key, EventChar::Home); + } + } + + pub fn update(&mut self, yaml: &serde_yaml::value::Value) { + for i in 32_u8..=127_u8 { + let c = i as char; + let s = c.to_string(); + if let Some(v) = yaml[s].as_str().map(|s| s.to_string()) { + self.binds.insert(c, EventChar::from(&v)); + } + } } - pub fn get(&self, key: char) -> Option<&EventChar> { - self.binds.get(&key) + pub fn to_hashmap(&self) -> HashMap<String, String> { + let mut reverse = HashMap::new(); + for (k, v) in self.binds.clone().into_iter() { + let _ = reverse.insert(v.into(), k.into()); + } + reverse } } diff --git a/src/main.rs b/src/main.rs index 10cfd209..e9f87d51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,11 +24,15 @@ fn main() -> FmResult<()> { info!("fm is starting..."); let config = load_config(CONFIG_PATH); + info!("config loaded"); let term = Arc::new(init_term()?); - let actioner = Actioner::new(&config.keybindings); + let actioner = Actioner::new(config.keybindings.clone()); + info!("{:?}", config.keybindings.binds); + info!("actioner loaded"); let event_reader = EventReader::new(term.clone()); let help = Help::from_keybindings(&config.keybindings)?.help; + info!("help loaded"); let mut display = Display::new(term.clone(), config.colors.clone()); let mut sys = System::new_all(); let mut status = Status::new(Args::parse(), config, display.height()?, term.clone(), help)?; |