diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-05-01 12:55:50 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-05-01 12:58:36 -0400 |
commit | 6b013bb91969d7e269aa5a08da729a8370a31900 (patch) | |
tree | 3a1173b052e5abb7826509c380668d3e2e9c4be5 | |
parent | ef98d3d408f9a741433523ccf557b26aa7380dbc (diff) |
add ConfigStructure trait
- make JoshutoKeymap just an alias for HashMap<i32, CommandKeybind>
- change keymap.toml to use ascii values rather than strings that
needs reparsing
-rw-r--r-- | config/keymap.toml | 138 | ||||
-rw-r--r-- | src/config/config.rs | 6 | ||||
-rw-r--r-- | src/config/keymap.rs | 91 | ||||
-rw-r--r-- | src/config/mimetype.rs | 14 | ||||
-rw-r--r-- | src/config/mod.rs | 4 | ||||
-rw-r--r-- | src/config/preview.rs | 19 | ||||
-rw-r--r-- | src/config/theme.rs | 6 | ||||
-rw-r--r-- | src/history.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | src/run.rs | 6 |
10 files changed, 128 insertions, 160 deletions
diff --git a/config/keymap.toml b/config/keymap.toml index 854b69b..29c1f01 100644 --- a/config/keymap.toml +++ b/config/keymap.toml @@ -1,213 +1,183 @@ [[mapcommand]] -keys = [ "Q" ] +keys = [ 81 ] command = "force_quit" - [[mapcommand]] -keys = [ "R" ] +keys = [ 113 ] +command = "close_tab" +[[mapcommand]] +keys = [ 82 ] command = "reload_dir_list" [[mapcommand]] -keys = [ "Up" ] +keys = [ 259 ] command = "cursor_move_up" args = [ "1" ] - [[mapcommand]] -keys = [ "k" ] +keys = [ 107 ] command = "cursor_move_up" args = [ "1" ] [[mapcommand]] -keys = [ "Down" ] +keys = [ 258 ] command = "cursor_move_down" args = [ "1" ] - [[mapcommand]] -keys = [ "j" ] +keys = [ 106 ] command = "cursor_move_down" args = [ "1" ] [[mapcommand]] -keys = [ "Left" ] +keys = [ 260 ] command = "parent_directory" - [[mapcommand]] -keys = [ "h" ] +keys = [ 104 ] command = "parent_directory" [[mapcommand]] -keys = [ "Right" ] +keys = [ 261 ] command = "open_file" - [[mapcommand]] -keys = [ "l" ] +keys = [ 108 ] command = "open_file" - [[mapcommand]] -keys = [ "Enter" ] +keys = [ 10 ] command = "open_file" - [[mapcommand]] -keys = [ "Home" ] +keys = [ 262 ] command = "cursor_move_home" - [[mapcommand]] -keys = [ "g", "g" ] +keys = [ 103, 103 ] command = "cursor_move_home" - [[mapcommand]] -keys = [ "End" ] +keys = [ 360 ] command = "cursor_move_end" - [[mapcommand]] -keys = [ "G" ] +keys = [ 71 ] command = "cursor_move_end" - - -[[mapcommand]] -keys = [ "PageUp" ] -command = "cursor_move_page_up" - [[mapcommand]] -keys = [ "PageDown" ] +keys = [ 338 ] command = "cursor_move_page_down" +[[mapcommand]] +keys = [ 339 ] +command = "cursor_move_page_up" [[mapcommand]] -keys = [ "r" ] +keys = [ 114 ] command = "open_file_with" - - [[mapcommand]] -keys = [ "z", "h" ] +keys = [ 122, 104 ] command = "toggle_hidden" [[mapcommand]] -keys = [ "d", "d" ] +keys = [ 100, 100 ] command = "cut_files" - [[mapcommand]] -keys = [ "y", "y" ] +keys = [ 121, 121 ] command = "copy_files" - [[mapcommand]] -keys = [ "p", "p" ] +keys = [ 112, 112 ] command = "paste_files" - [[mapcommand]] -keys = [ "p", "o" ] +keys = [ 112, 111 ] command = "paste_files" args = [ "overwrite=true" ] [[mapcommand]] -keys = [ "a" ] +keys = [ 97 ] command = "rename_file" args = [ "append" ] - [[mapcommand]] -keys = [ "A" ] +keys = [ 65 ] command = "rename_file" args = [ "prepend" ] - [[mapcommand]] -keys = [ "c", "w" ] +keys = [ 99, 119 ] command = "rename_file" args = [ "overwrite" ] [[mapcommand]] -keys = [ "d", "D" ] +keys = [ 100, 68 ] command = "delete_files" - [[mapcommand]] -keys = [ "Delete" ] +keys = [ 330 ] command = "delete_files" [[mapcommand]] -keys = [ "Space" ] +keys = [ 32 ] command = "select_files" args = [ "toggle=true" ] [[mapcommand]] -keys = [ "m", "k" ] +keys = [ 109, 107 ] command = "mkdir" - [[mapcommand]] -keys = [ ";", "m", "k" ] +keys = [ 59, 109, 107 ] command = "mkdir" [[mapcommand]] -keys = [ "g", "P" ] +keys = [ 103, 80 ] command = "cd" args = [ "~/Pictures" ] - [[mapcommand]] -keys = [ "g", "r" ] +keys = [ 103, 114 ] command = "cd" args = [ "/" ] - [[mapcommand]] -keys = [ "g", "e" ] +keys = [ 103, 101 ] command = "cd" args = [ "/etc" ] - [[mapcommand]] -keys = [ "g", "h" ] +keys = [ 103, 104 ] command = "cd" args = [ "~/" ] - [[mapcommand]] -keys = [ "g", "d" ] +keys = [ 103, 100 ] command = "cd" args = [ "~/Downloads" ] -[[mapcommand]] -keys = [ "g", "p" ] -command = "cd" -args = [ "~/Pictures" ] - [[mapcommand]] -keys = [ "/" ] +keys = [ 47 ] command = "search" - -[[mapcommand]] -keys = [ "n" ] -command = "search_next" - [[mapcommand]] -keys = [ "N" ] +keys = [ 78 ] command = "search_prev" +[[mapcommand]] +keys = [ 110 ] +command = "search_next" [[mapcommand]] -keys = [ "T" ] +keys = [ 20 ] command = "new_tab" [[mapcommand]] -keys = [ "q" ] +keys = [ 23 ] command = "close_tab" [[mapcommand]] -keys = [ "Tab" ] +keys = [ 9 ] command = "tab_switch" args = [ "1" ] [[mapcommand]] -keys = [ "ShiftTab" ] +keys = [ 353 ] command = "tab_switch" args = [ "-1" ] [[mapcommand]] -keys = [ "=" ] +keys = [ 45 ] command = "set_mode" ## Features not yet implemented -## [[mapcommand]] -## keys = [ "b", "b" ] -## command = "bulk_rename" +[[mapcommand]] +keys = [ 98, 98 ] +command = "bulk_rename" diff --git a/src/config/config.rs b/src/config/config.rs index ab1e56f..d28f837 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -1,6 +1,6 @@ use serde_derive::Deserialize; -use crate::config::{parse_config_file, Flattenable}; +use super::{parse_config_file, ConfigStructure, Flattenable}; use crate::sort; use crate::CONFIG_FILE; @@ -93,8 +93,8 @@ pub struct JoshutoConfig { pub column_ratio: (usize, usize, usize), } -impl JoshutoConfig { - pub fn get_config() -> JoshutoConfig { +impl ConfigStructure for JoshutoConfig { + fn get_config() -> Self { parse_config_file::<JoshutoRawConfig, JoshutoConfig>(CONFIG_FILE) .unwrap_or_else(JoshutoConfig::default) } diff --git a/src/config/keymap.rs b/src/config/keymap.rs index e38bd4c..1dc84c0 100644 --- a/src/config/keymap.rs +++ b/src/config/keymap.rs @@ -3,7 +3,7 @@ use std::collections::{hash_map::Entry, HashMap}; use std::process::exit; use crate::commands::{self, CommandKeybind, JoshutoCommand}; -use crate::config::{parse_config_file, Flattenable}; +use super::{parse_config_file, ConfigStructure, Flattenable}; use crate::KEYMAP_FILE; pub const BACKSPACE: i32 = 0x7F; @@ -15,7 +15,7 @@ pub const ESCAPE: i32 = 0x1B; #[derive(Debug, Deserialize)] struct JoshutoMapCommand { - pub keys: Vec<String>, + pub keys: Vec<i32>, pub command: String, pub args: Option<Vec<String>>, } @@ -27,76 +27,63 @@ struct JoshutoRawKeymap { impl Flattenable<JoshutoKeymap> for JoshutoRawKeymap { fn flatten(self) -> JoshutoKeymap { - let mut keymaps: HashMap<i32, CommandKeybind> = HashMap::new(); - if let Some(maps) = self.mapcommand { - for mapcommand in maps { - match commands::from_args(mapcommand.command.as_str(), mapcommand.args.as_ref()) { - Ok(command) => insert_keycommand(&mut keymaps, command, &mapcommand.keys[..]), - Err(e) => eprintln!("{}", e), - } + match self.mapcommand { + None => JoshutoKeymap::new(), + Some(maps) => { + let mut keymaps = JoshutoKeymap::new(); + maps.iter().for_each(|m| { + match commands::from_args(m.command.as_str(), m.args.as_ref()) { + Ok(command) => insert_keycommand(&mut keymaps, command, &m.keys[..]), + Err(e) => eprintln!("{}", e), + } + }); + keymaps } } - JoshutoKeymap { keymaps } } } -#[derive(Debug)] -pub struct JoshutoKeymap { - pub keymaps: HashMap<i32, CommandKeybind>, -} +pub type JoshutoKeymap = HashMap<i32, CommandKeybind>; -impl JoshutoKeymap { - pub fn get_config() -> JoshutoKeymap { +impl ConfigStructure for JoshutoKeymap { + fn get_config() -> Self { parse_config_file::<JoshutoRawKeymap, JoshutoKeymap>(KEYMAP_FILE) .unwrap_or_else(JoshutoKeymap::default) } } -impl std::default::Default for JoshutoKeymap { - fn default() -> Self { - let keymaps = HashMap::new(); - JoshutoKeymap { keymaps } - } -} - fn insert_keycommand( - map: &mut HashMap<i32, CommandKeybind>, + map: &mut JoshutoKeymap, keycommand: Box<JoshutoCommand>, - keys: &[String], + keys: &[i32], ) { match keys.len() { 0 => {} - 1 => match key_to_i32(&keys[0]) { - Some(s) => match map.entry(s) { - Entry::Occupied(_) => { + 1 => match map.entry(keys[0]) { + Entry::Occupied(_) => { + eprintln!("Error: Keybindings ambiguous"); + exit(1); + } + Entry::Vacant(entry) => { + entry.insert(CommandKeybind::SimpleKeybind(keycommand)); + } + }, + _ => match map.entry(keys[0]) { + Entry::Occupied(mut entry) => match entry.get_mut() { + CommandKeybind::CompositeKeybind(ref mut m) => { + insert_keycommand(m, keycommand, &keys[1..]) + } + _ => { eprintln!("Error: Keybindings ambiguous"); exit(1); } - Entry::Vacant(entry) => { - entry.insert(CommandKeybind::SimpleKeybind(keycommand)); - } - }, - None => eprintln!("Error: Failed to parse keycode: {}", keys[0]), - }, - _ => match key_to_i32(&keys[0]) { - Some(s) => match map.entry(s) { - Entry::Occupied(mut entry) => match entry.get_mut() { - CommandKeybind::CompositeKeybind(ref mut m) => { - insert_keycommand(m, keycommand, &keys[1..]) - } - _ => { - eprintln!("Error: Keybindings ambiguous"); - exit(1); - } - }, - Entry::Vacant(entry) => { - let mut new_map = HashMap::new(); - insert_keycommand(&mut new_map, keycommand, &keys[1..]); - let composite_command = CommandKeybind::CompositeKeybind(new_map); - entry.insert(composite_command); - } }, - None => eprintln!("Error: Failed to parse keycode: {}", keys[0]), + Entry::Vacant(entry) => { + let mut new_map = HashMap::new(); + insert_keycommand(&mut new_map, keycommand, &keys[1..]); + let composite_command = CommandKeybind::CompositeKeybind(new_map); + entry.insert(composite_command); + } }, } } diff --git a/src/config/mimetype.rs b/src/config/mimetype.rs index 15560db..40d64dc 100644 --- a/src/config/mimetype.rs +++ b/src/config/mimetype.rs @@ -2,7 +2,7 @@ use serde_derive::Deserialize; use std::collections::HashMap; use std::fmt; -use crate::config::{parse_config_file, Flattenable}; +use super::{parse_config_file, ConfigStructure, Flattenable}; use crate::MIMETYPE_FILE; #[derive(Debug, Deserialize)] @@ -68,11 +68,6 @@ pub struct JoshutoMimetype { } impl JoshutoMimetype { - pub fn get_config() -> JoshutoMimetype { - parse_config_file::<JoshutoRawMimetype, JoshutoMimetype>(MIMETYPE_FILE) - .unwrap_or_else(JoshutoMimetype::default) - } - pub fn get_entries_for_ext(&self, extension: &str) -> Vec<&JoshutoMimetypeEntry> { let mut vec = Vec::new(); if let Some(entry_ids) = self.extension.get(extension) { @@ -97,6 +92,13 @@ impl JoshutoMimetype { } } +impl ConfigStructure for JoshutoMimetype { + fn get_config() -> Self { + parse_config_file::<JoshutoRawMimetype, JoshutoMimetype>(MIMETYPE_FILE) + .unwrap_or_else(JoshutoMimetype::default) + } +} + impl std::default::Default for JoshutoMimetype { fn default() -> Self { JoshutoMimetype { diff --git a/src/config/mod.rs b/src/config/mod.rs index ba6017f..255878e 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -16,6 +16,10 @@ use std::path::{Path, PathBuf}; use crate::CONFIG_HIERARCHY; +pub trait ConfigStructure { + fn get_config() -> Self; +} + // implemented by config file implementations to turn a RawConfig into a Config trait Flattenable<T> { fn flatten(self) -> T; diff --git a/src/config/preview.rs b/src/config/preview.rs index 24175b8..dd6665d 100644 --- a/src/config/preview.rs +++ b/src/config/preview.rs @@ -1,7 +1,7 @@ use serde_derive::Deserialize; use std::collections::HashMap; -use crate::config::{parse_config_file, Flattenable}; +use super::{parse_config_file, ConfigStructure, Flattenable}; use crate::PREVIEW_FILE; #[derive(Debug, Deserialize)] @@ -43,16 +43,19 @@ pub struct JoshutoPreview { pub mimetype: HashMap<String, JoshutoPreviewEntry>, } -impl JoshutoPreview { - pub fn new() -> Self { +impl ConfigStructure for JoshutoPreview { + fn get_config() -> Self { + parse_config_file::<JoshutoRawPreview, JoshutoPreview>(PREVIEW_FILE) + .unwrap_or_else(JoshutoPreview::default) + } +} + +impl std::default::Default for JoshutoPreview { + fn default() -> Self { JoshutoPreview { extension: HashMap::new(), mimetype: HashMap::new(), } } - - pub fn get_config() -> JoshutoPreview { - parse_config_file::<JoshutoRawPreview, JoshutoPreview>(PREVIEW_FILE) - .unwrap_or_else(JoshutoPreview::new) - } } + diff --git a/src/config/theme.rs b/src/config/theme.rs index 77cae37..217f6c1 100644 --- a/src/config/theme.rs +++ b/src/config/theme.rs @@ -1,7 +1,7 @@ use serde_derive::Deserialize; use std::collections::HashMap; -use crate::config::{parse_config_file, Flattenable}; +use super::{parse_config_file, ConfigStructure, Flattenable}; #[derive(Debug, Deserialize, Clone)] pub struct JoshutoColorPair { @@ -164,8 +164,8 @@ pub struct JoshutoTheme { pub ext: HashMap<String, JoshutoColorTheme>, } -impl JoshutoTheme { - pub fn get_config() -> JoshutoTheme { +impl ConfigStructure for JoshutoTheme { + fn get_config() -> Self { parse_config_file::<JoshutoRawTheme, JoshutoTheme>(crate::THEME_FILE) .unwrap_or_else(JoshutoTheme::default) } diff --git a/src/history.rs b/src/history.rs index c29fefe..9d85322 100644 --- a/src/history.rs +++ b/src/history.rs @@ -74,9 +74,11 @@ impl DirectoryHistory for HashMap<PathBuf, JoshutoDirList> { match self.entry(path.to_path_buf().clone()) { Entry::Occupied(mut entry) => { let dir_entry = entry.get_mut(); +/* if dir_entry.need_update() { dir_entry.update_contents(&sort_option)?; } +*/ Ok(entry.into_mut()) } Entry::Vacant(entry) => { diff --git a/src/main.rs b/src/main.rs index 31f9679..6afe6b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ use lazy_static::lazy_static; use std::path::PathBuf; use structopt::StructOpt; -use config::{JoshutoConfig, JoshutoKeymap, JoshutoMimetype, JoshutoPreview, JoshutoTheme}; +use config::{ConfigStructure, JoshutoConfig, JoshutoKeymap, JoshutoMimetype, JoshutoPreview, JoshutoTheme}; use run::run; const PROGRAM_NAME: &str = "joshuto"; @@ -3,7 +3,7 @@ use std::process; use std::time; use crate::commands::{CommandKeybind, FileOperationThread, JoshutoCommand}; -use crate::config; +use crate::config::{self, JoshutoConfig, JoshutoKeymap}; use crate::context::JoshutoContext; use crate::error::JoshutoError; use crate::tab::JoshutoTab; @@ -157,7 +157,7 @@ fn init_context(context: &mut JoshutoContext, view: &JoshutoView) { } } -pub fn run(config_t: config::JoshutoConfig, keymap_t: config::JoshutoKeymap) { +pub fn run(config_t: JoshutoConfig, keymap_t: JoshutoKeymap) { ui::init_ncurses(); let mut context = JoshutoContext::new(config_t); @@ -190,7 +190,7 @@ pub fn run(config_t: config::JoshutoConfig, keymap_t: config::JoshutoKeymap) { let keycommand: &Box<JoshutoCommand>; - match keymap_t.keymaps.get(&ch) { + match keymap_t.get(&ch) { Some(CommandKeybind::CompositeKeybind(m)) => match recurse_get_keycommand(&m) { Some(s) => keycommand = s, None => continue, |