summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2019-05-31 16:13:54 -0400
committerJiayi Zhao <jeff.no.zhao@gmail.com>2019-05-31 16:13:54 -0400
commit3cba2ba36e826985c160e58faf556a6b2d17fbcb (patch)
treec6d95dbc3021b027326def5aae90aeea747ee071 /src
parent6f1271f457d3201b269f99bc3c6571280f0f99e1 (diff)
add support for specifying certain keycodes for certain keyboard actions
Diffstat (limited to 'src')
-rw-r--r--src/config/config.rs4
-rw-r--r--src/config/keymap.rs137
-rw-r--r--src/config/mimetype.rs4
-rw-r--r--src/config/mod.rs6
-rw-r--r--src/config/preview.rs4
-rw-r--r--src/config/theme.rs4
-rw-r--r--src/main.rs7
-rw-r--r--src/run.rs6
-rw-r--r--src/textfield.rs24
9 files changed, 157 insertions, 39 deletions
diff --git a/src/config/config.rs b/src/config/config.rs
index 14b4d67..f093d79 100644
--- a/src/config/config.rs
+++ b/src/config/config.rs
@@ -1,6 +1,6 @@
use serde_derive::Deserialize;
-use super::{parse_config_file, ConfigStructure, Flattenable};
+use super::{parse_to_config_file, ConfigStructure, Flattenable};
use crate::sort;
use crate::CONFIG_FILE;
@@ -108,7 +108,7 @@ pub struct JoshutoConfig {
impl ConfigStructure for JoshutoConfig {
fn get_config() -> Self {
- parse_config_file::<JoshutoRawConfig, JoshutoConfig>(CONFIG_FILE)
+ parse_to_config_file::<JoshutoRawConfig, JoshutoConfig>(CONFIG_FILE)
.unwrap_or_else(JoshutoConfig::default)
}
}
diff --git a/src/config/keymap.rs b/src/config/keymap.rs
index 1befdfe..f5c8eb1 100644
--- a/src/config/keymap.rs
+++ b/src/config/keymap.rs
@@ -2,7 +2,7 @@ use serde_derive::Deserialize;
use std::collections::{hash_map::Entry, HashMap};
use std::process::exit;
-use super::{parse_config_file, ConfigStructure, Flattenable};
+use super::{parse_config_file, parse_to_config_file, ConfigStructure, Flattenable};
use crate::commands::{self, CommandKeybind, JoshutoCommand};
use crate::KEYMAP_FILE;
@@ -13,6 +13,121 @@ pub const ESCAPE: i32 = 0x1B;
/* #define KEY_ALT(x) KEY_F(60) + (x - 'A') */
+const fn default_up() -> i32 {
+ ncurses::KEY_UP
+}
+
+const fn default_down() -> i32 {
+ ncurses::KEY_DOWN
+}
+
+const fn default_left() -> i32 {
+ ncurses::KEY_LEFT
+}
+
+const fn default_right() -> i32 {
+ ncurses::KEY_RIGHT
+}
+
+const fn default_home() -> i32 {
+ ncurses::KEY_HOME
+}
+
+const fn default_end() -> i32 {
+ ncurses::KEY_END
+}
+
+const fn default_backspace() -> i32 {
+ BACKSPACE
+}
+
+const fn default_delete() -> i32 {
+ ncurses::KEY_DC
+}
+
+const fn default_enter() -> i32 {
+ ENTER
+}
+
+const fn default_escape() -> i32 {
+ ESCAPE
+}
+
+const fn default_tab() -> i32 {
+ TAB
+}
+
+#[derive(Debug, Deserialize)]
+struct JoshutoRawKeymapping {
+ #[serde(default)]
+ keymaps: JoshutoKeyMapping,
+ #[serde(skip)]
+ mapcommand: Vec<JoshutoMapCommand>,
+}
+
+#[derive(Debug, Deserialize)]
+pub struct JoshutoKeyMapping {
+ #[serde(default = "default_up")]
+ pub up: i32,
+ #[serde(default = "default_down")]
+ pub down: i32,
+ #[serde(default = "default_left")]
+ pub left: i32,
+ #[serde(default = "default_right")]
+ pub right: i32,
+ #[serde(default = "default_home")]
+ pub home: i32,
+ #[serde(default = "default_end")]
+ pub end: i32,
+/*
+ #[serde(default = "default_up")]
+ pub page_up: i32,
+ #[serde(default = "default_up")]
+ pub page_down: i32,
+*/
+ #[serde(default = "default_backspace")]
+ pub backspace: i32,
+ #[serde(default = "default_delete")]
+ pub delete: i32,
+ #[serde(default = "default_enter")]
+ pub enter: i32,
+ #[serde(default = "default_escape")]
+ pub escape: i32,
+ #[serde(default = "default_tab")]
+ pub tab: i32,
+}
+
+impl std::default::Default for JoshutoKeyMapping {
+ fn default() -> Self {
+ JoshutoKeyMapping {
+ up: default_up(),
+ down: default_down(),
+ left: default_left(),
+ right: default_right(),
+ home: default_home(),
+ end: default_end(),
+ backspace: default_backspace(),
+ delete: default_delete(),
+ enter: default_enter(),
+ escape: default_escape(),
+ tab: default_tab(),
+ }
+ }
+}
+
+impl Flattenable<JoshutoKeyMapping> for JoshutoRawKeymapping {
+ fn flatten(self) -> JoshutoKeyMapping {
+ self.keymaps
+ }
+}
+
+impl ConfigStructure for JoshutoKeyMapping {
+ fn get_config() -> Self {
+ parse_to_config_file::<JoshutoRawKeymapping, JoshutoKeyMapping>(KEYMAP_FILE)
+ .unwrap_or_else(JoshutoKeyMapping::default)
+ }
+}
+
#[derive(Debug, Deserialize)]
struct JoshutoMapCommand {
pub keys: Vec<i32>,
@@ -22,14 +137,16 @@ struct JoshutoMapCommand {
}
#[derive(Debug, Deserialize)]
-struct JoshutoRawKeymap {
+struct JoshutoRawCommandMapping {
+ #[serde(skip)]
+ keymaps: JoshutoKeyMapping,
#[serde(default)]
mapcommand: Vec<JoshutoMapCommand>,
}
-impl Flattenable<JoshutoKeymap> for JoshutoRawKeymap {
- fn flatten(self) -> JoshutoKeymap {
- let mut keymaps = JoshutoKeymap::new();
+impl Flattenable<JoshutoCommandMapping> for JoshutoRawCommandMapping {
+ fn flatten(self) -> JoshutoCommandMapping {
+ let mut keymaps = JoshutoCommandMapping::new();
self.mapcommand.iter().for_each(|m| {
let args: Vec<&str> = m.args.iter().map(|s| s.as_str()).collect();
match commands::from_args(m.command.as_str(), &args) {
@@ -41,16 +158,16 @@ impl Flattenable<JoshutoKeymap> for JoshutoRawKeymap {
}
}
-pub type JoshutoKeymap = HashMap<i32, CommandKeybind>;
+pub type JoshutoCommandMapping = HashMap<i32, CommandKeybind>;
-impl ConfigStructure for JoshutoKeymap {
+impl ConfigStructure for JoshutoCommandMapping {
fn get_config() -> Self {
- parse_config_file::<JoshutoRawKeymap, JoshutoKeymap>(KEYMAP_FILE)
- .unwrap_or_else(JoshutoKeymap::default)
+ parse_to_config_file::<JoshutoRawCommandMapping, JoshutoCommandMapping>(KEYMAP_FILE)
+ .unwrap_or_else(JoshutoCommandMapping::default)
}
}
-fn insert_keycommand(map: &mut JoshutoKeymap, keycommand: Box<JoshutoCommand>, keys: &[i32]) {
+fn insert_keycommand(map: &mut JoshutoCommandMapping, keycommand: Box<JoshutoCommand>, keys: &[i32]) {
match keys.len() {
0 => {}
1 => match map.entry(keys[0]) {
diff --git a/src/config/mimetype.rs b/src/config/mimetype.rs
index 7e45d8d..eff0540 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 super::{parse_config_file, ConfigStructure, Flattenable};
+use super::{parse_to_config_file, ConfigStructure, Flattenable};
use crate::MIMETYPE_FILE;
const fn default_false() -> bool {
@@ -92,7 +92,7 @@ impl JoshutoMimetype {
impl ConfigStructure for JoshutoMimetype {
fn get_config() -> Self {
- parse_config_file::<JoshutoRawMimetype, JoshutoMimetype>(MIMETYPE_FILE)
+ parse_to_config_file::<JoshutoRawMimetype, JoshutoMimetype>(MIMETYPE_FILE)
.unwrap_or_else(JoshutoMimetype::default)
}
}
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 822af58..db15753 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -5,7 +5,7 @@ pub mod preview;
pub mod theme;
pub use self::config::JoshutoConfig;
-pub use self::keymap::JoshutoKeymap;
+pub use self::keymap::{JoshutoCommandMapping, JoshutoKeyMapping};
pub use self::mimetype::JoshutoMimetype;
pub use self::preview::{JoshutoPreview, JoshutoPreviewEntry};
pub use self::theme::{JoshutoColorTheme, JoshutoTheme};
@@ -40,7 +40,7 @@ where
}
// parses a config file into its appropriate format
-fn parse_config_file<T, S>(filename: &str) -> Option<S>
+fn parse_to_config_file<T, S>(filename: &str) -> Option<S>
where
T: DeserializeOwned + Flattenable<S>,
{
@@ -63,7 +63,7 @@ where
}
// parses a config file into its appropriate format
-fn parse_config<T>(filename: &str) -> Option<T>
+fn parse_config_file<T>(filename: &str) -> Option<T>
where
T: DeserializeOwned,
{
diff --git a/src/config/preview.rs b/src/config/preview.rs
index aad3613..e966e4e 100644
--- a/src/config/preview.rs
+++ b/src/config/preview.rs
@@ -1,7 +1,7 @@
use serde_derive::Deserialize;
use std::collections::HashMap;
-use super::{parse_config_file, ConfigStructure, Flattenable};
+use super::{parse_to_config_file, ConfigStructure, Flattenable};
use crate::PREVIEW_FILE;
#[derive(Debug, Deserialize)]
@@ -45,7 +45,7 @@ pub struct JoshutoPreview {
impl ConfigStructure for JoshutoPreview {
fn get_config() -> Self {
- parse_config_file::<JoshutoRawPreview, JoshutoPreview>(PREVIEW_FILE)
+ parse_to_config_file::<JoshutoRawPreview, JoshutoPreview>(PREVIEW_FILE)
.unwrap_or_else(JoshutoPreview::default)
}
}
diff --git a/src/config/theme.rs b/src/config/theme.rs
index ab88d6f..ef5e9ba 100644
--- a/src/config/theme.rs
+++ b/src/config/theme.rs
@@ -1,7 +1,7 @@
use serde_derive::Deserialize;
use std::collections::HashMap;
-use super::{parse_config, ConfigStructure};
+use super::{parse_config_file, ConfigStructure};
const fn default_zero() -> i16 {
0
@@ -82,7 +82,7 @@ pub struct JoshutoTheme {
impl ConfigStructure for JoshutoTheme {
fn get_config() -> Self {
- parse_config::<JoshutoTheme>(crate::THEME_FILE).unwrap_or_else(JoshutoTheme::default)
+ parse_config_file::<JoshutoTheme>(crate::THEME_FILE).unwrap_or_else(JoshutoTheme::default)
}
}
diff --git a/src/main.rs b/src/main.rs
index 0fce913..5d6435f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -18,7 +18,7 @@ use std::path::PathBuf;
use structopt::StructOpt;
use config::{
- ConfigStructure, JoshutoConfig, JoshutoKeymap, JoshutoMimetype, JoshutoPreview, JoshutoTheme,
+ ConfigStructure, JoshutoConfig, JoshutoCommandMapping, JoshutoKeyMapping, JoshutoMimetype, JoshutoPreview, JoshutoTheme,
};
use run::run;
@@ -46,8 +46,9 @@ lazy_static! {
static ref THEME_T: JoshutoTheme = JoshutoTheme::get_config();
static ref MIMETYPE_T: JoshutoMimetype = JoshutoMimetype::get_config();
static ref PREVIEW_T: JoshutoPreview = JoshutoPreview::get_config();
- static ref HOME_DIR: Option<PathBuf> = dirs::home_dir();
+ static ref KEYMAP_T: JoshutoKeyMapping = JoshutoKeyMapping::get_config();
+ static ref HOME_DIR: Option<PathBuf> = dirs::home_dir();
static ref USERNAME: String = whoami::username();
static ref HOSTNAME: String = whoami::hostname();
}
@@ -62,7 +63,7 @@ fn main() {
let args = Args::from_args();
let config = JoshutoConfig::get_config();
- let keymap = JoshutoKeymap::get_config();
+ let keymap = JoshutoCommandMapping::get_config();
if args.debug {
eprintln!("config: {:#?}", config);
diff --git a/src/run.rs b/src/run.rs
index 4d4053f..33f069a 100644
--- a/src/run.rs
+++ b/src/run.rs
@@ -2,7 +2,7 @@ use std::process;
use std::time;
use crate::commands::{CommandKeybind, FileOperationThread, JoshutoCommand, ReloadDirList};
-use crate::config::{self, JoshutoConfig, JoshutoKeymap};
+use crate::config::{self, JoshutoConfig, JoshutoCommandMapping};
use crate::context::JoshutoContext;
use crate::error::JoshutoError;
use crate::tab::JoshutoTab;
@@ -10,7 +10,7 @@ use crate::ui;
use crate::window::JoshutoPanel;
use crate::window::JoshutoView;
-fn recurse_get_keycommand(keymap: &JoshutoKeymap) -> Option<&Box<JoshutoCommand>> {
+fn recurse_get_keycommand(keymap: &JoshutoCommandMapping) -> Option<&Box<JoshutoCommand>> {
let (term_rows, term_cols) = ui::getmaxyx();
ncurses::timeout(-1);
@@ -136,7 +136,7 @@ fn init_context(context: &mut JoshutoContext, view: &JoshutoView) {
}
}
-pub fn run(config_t: JoshutoConfig, keymap_t: JoshutoKeymap) {
+pub fn run(config_t: JoshutoConfig, keymap_t: JoshutoCommandMapping) {
ui::init_ncurses();
let mut context = JoshutoContext::new(config_t);
diff --git a/src/textfield.rs b/src/textfield.rs
index 5bcd2fe..61e006f 100644
--- a/src/textfield.rs
+++ b/src/textfield.rs
@@ -1,4 +1,4 @@
-use crate::config::keymap;
+use crate::KEYMAP_T;
use crate::window;
use rustyline::completion::{Candidate, Completer, FilenameCompleter, Pair};
@@ -89,41 +89,41 @@ impl<'a> JoshutoTextField<'a> {
ncurses::WchResult::KeyCode(s) => s,
};
- if ch == keymap::ESCAPE {
+ if ch == KEYMAP_T.escape {
return None;
- } else if ch == keymap::ENTER {
+ } else if ch == KEYMAP_T.enter {
break;
- } else if ch == ncurses::KEY_HOME {
+ } else if ch == KEYMAP_T.home {
line_buffer.move_home();
curr_pos = 0;
completion_tracker.take();
- } else if ch == ncurses::KEY_END {
+ } else if ch == KEYMAP_T.end {
line_buffer.move_end();
curr_pos = unicode_width::UnicodeWidthStr::width(line_buffer.as_str());
completion_tracker.take();
- } else if ch == ncurses::KEY_LEFT {
+ } else if ch == KEYMAP_T.left {
if line_buffer.move_backward(1) {
let pos = line_buffer.pos();
curr_pos = unicode_width::UnicodeWidthStr::width(&line_buffer.as_str()[..pos]);
completion_tracker.take();
}
- } else if ch == ncurses::KEY_RIGHT {
+ } else if ch == KEYMAP_T.right {
if line_buffer.move_forward(1) {
let pos = line_buffer.pos();
curr_pos = unicode_width::UnicodeWidthStr::width(&line_buffer.as_str()[..pos]);
completion_tracker.take();
}
- } else if ch == keymap::BACKSPACE {
+ } else if ch == KEYMAP_T.backspace {
if line_buffer.backspace(1) {
let pos = line_buffer.pos();
curr_pos = unicode_width::UnicodeWidthStr::width(&line_buffer.as_str()[..pos]);
completion_tracker.take();
}
- } else if ch == ncurses::KEY_DC {
+ } else if ch == KEYMAP_T.delete {
if line_buffer.delete(1).is_some() {
completion_tracker.take();
}
- } else if ch == keymap::TAB {
+ } else if ch == KEYMAP_T.tab {
if completion_tracker.is_none() {
let res = completer.complete_path(line_buffer.as_str(), line_buffer.pos());
if let Ok((pos, mut candidates)) = res {
@@ -151,9 +151,9 @@ impl<'a> JoshutoTextField<'a> {
curr_pos = unicode_width::UnicodeWidthStr::width(
&line_buffer.as_str()[..line_buffer.pos()],
);
- } else if ch == ncurses::KEY_UP {
+ } else if ch == KEYMAP_T.up {
completion_tracker.take();
- } else if ch == ncurses::KEY_DOWN {
+ } else if ch == KEYMAP_T.down {
completion_tracker.take();
} else if let Some(ch) = std::char::from_u32(ch as u32) {
if line_buffer.insert(ch, 1).is_some() {