summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2020-03-18 22:29:44 -0400
committerJiayi Zhao <jeff.no.zhao@gmail.com>2020-03-18 22:29:44 -0400
commitf3081669e488c54ab4fcae2829ea2f58f082c6e7 (patch)
treed3c83c861f4733e7930e4d7733b059b38404c588
parent12f28d72fa9814bcd9fb2fd5bc4c54fabe93c031 (diff)
add default keymappings when config dne
- message colors are now yellow instead of cyan - there is now a wrapper around keymap hashmap
-rw-r--r--src/commands/bulk_rename.rs1
-rw-r--r--src/commands/mod.rs8
-rw-r--r--src/config/keymap.rs235
-rw-r--r--src/run.rs2
-rw-r--r--src/ui/widgets/tui_menu.rs3
-rw-r--r--src/ui/widgets/tui_view.rs3
6 files changed, 228 insertions, 24 deletions
diff --git a/src/commands/bulk_rename.rs b/src/commands/bulk_rename.rs
index f1f9549..2fff733 100644
--- a/src/commands/bulk_rename.rs
+++ b/src/commands/bulk_rename.rs
@@ -88,6 +88,7 @@ impl BulkRename {
let path = path::PathBuf::from(line);
paths_renamed.push(path);
}
+ std::fs::remove_file(&file_path)?;
}
if paths_renamed.len() < paths.len() {
return Err(JoshutoError::new(
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
index cca9b20..d511a52 100644
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -95,12 +95,12 @@ pub fn from_args(command: String, args: Vec<String>) -> JoshutoResult<Box<dyn Jo
"copy_files" => Ok(Box::new(self::CopyFiles::new())),
"console" => match args.len() {
0 => Ok(Box::new(self::CommandLine::new(
- String::new(),
- String::new(),
+ "".to_string(),
+ "".to_string(),
))),
1 => Ok(Box::new(self::CommandLine::new(
- String::from(args[0].as_str()),
- String::new(),
+ args[0].clone(),
+ "".to_string(),
))),
i => Err(JoshutoError::new(
JoshutoErrorKind::IOInvalidData,
diff --git a/src/config/keymap.rs b/src/config/keymap.rs
index 6bf915c..2ff5e43 100644
--- a/src/config/keymap.rs
+++ b/src/config/keymap.rs
@@ -6,10 +6,215 @@ use termion::event::Key;
use super::{parse_to_config_file, ConfigStructure, Flattenable};
use crate::commands::{self, CommandKeybind, JoshutoCommand};
+use crate::io::Options;
use crate::util::key_mapping::str_to_key;
use crate::KEYMAP_FILE;
-pub type JoshutoCommandMapping = HashMap<Key, CommandKeybind>;
+#[derive(Debug)]
+pub struct JoshutoCommandMapping {
+ map: HashMap<Key, CommandKeybind>,
+}
+
+impl std::convert::AsRef<HashMap<Key, CommandKeybind>> for JoshutoCommandMapping {
+ fn as_ref(&self) -> &HashMap<Key, CommandKeybind> {
+ &self.map
+ }
+}
+
+impl std::convert::AsMut<HashMap<Key, CommandKeybind>> for JoshutoCommandMapping {
+ fn as_mut(&mut self) -> &mut HashMap<Key, CommandKeybind> {
+ &mut self.map
+ }
+}
+
+impl JoshutoCommandMapping {
+ pub fn new() -> Self {
+ Self {
+ map: HashMap::new(),
+ }
+ }
+}
+
+impl std::default::Default for JoshutoCommandMapping {
+ fn default() -> Self {
+ let mut m = Self {
+ map: HashMap::new(),
+ };
+
+ let cmd = Box::new(commands::CursorMoveUp::new(1));
+ let keys = [ Key::Up ];
+ insert_keycommand(&mut m, cmd, &keys);
+ let cmd = Box::new(commands::CursorMoveDown::new(1));
+ let keys = [ Key::Down ];
+ insert_keycommand(&mut m, cmd, &keys);
+ let cmd = Box::new(commands::ParentDirectory::new());
+ let keys = [ Key::Left ];
+ insert_keycommand(&mut m, cmd, &keys);
+ let cmd = Box::new(commands::OpenFile::new());
+ let keys = [ Key::Right ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::OpenFile::new());
+ let keys = [ Key::Char('\n') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::CursorMoveHome::new());
+ let keys = [ Key::Home ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::CursorMoveEnd::new());
+ let keys = [ Key::End ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::CursorMovePageUp::new());
+ let keys = [ Key::PageUp ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::CursorMovePageDown::new());
+ let keys = [ Key::PageDown ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ // vim keys
+ let cmd = Box::new(commands::CursorMoveUp::new(1));
+ let keys = [ Key::Char('k') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::CursorMoveDown::new(1));
+ let keys = [ Key::Char('j') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::ParentDirectory::new());
+ let keys = [ Key::Char('h') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::OpenFile::new());
+ let keys = [ Key::Char('l') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::NewTab::new());
+ let keys = [ Key::Char('T') ];
+ insert_keycommand(&mut m, cmd, &keys);
+ let cmd = Box::new(commands::NewTab::new());
+ let keys = [ Key::Ctrl('t') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::NewTab::new());
+ let keys = [ Key::Char('W') ];
+ insert_keycommand(&mut m, cmd, &keys);
+ let cmd = Box::new(commands::NewTab::new());
+ let keys = [ Key::Ctrl('w') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::CloseTab::new());
+ let keys = [ Key::Char('q') ];
+ insert_keycommand(&mut m, cmd, &keys);
+ let cmd = Box::new(commands::ForceQuit::new());
+ let keys = [ Key::Char('Q') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::ReloadDirList::new());
+ let keys = [ Key::Char('R') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+
+ let cmd = Box::new(commands::ToggleHiddenFiles::new());
+ let keys = [ Key::Char('z'), Key::Char('h') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::TabSwitch::new(1));
+ let keys = [ Key::Char('\t') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::TabSwitch::new(-1));
+ let keys = [ Key::BackTab ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::OpenFileWith::new());
+ let keys = [ Key::Char('r') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::CutFiles::new());
+ let keys = [ Key::Char('d'), Key::Char('d') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::CopyFiles::new());
+ let keys = [ Key::Char('y'), Key::Char('y') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::PasteFiles::new(Options::default()));
+ let keys = [ Key::Char('p'), Key::Char('p') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::DeleteFiles::new());
+ let keys = [ Key::Delete ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::DeleteFiles::new());
+ let keys = [ Key::Char('D'), Key::Char('d') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::RenameFileAppend::new());
+ let keys = [ Key::Char('a') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::RenameFilePrepend::new());
+ let keys = [ Key::Char('A') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::CommandLine::new(
+ "search ".to_string(),
+ "".to_string(),
+ ));
+ let keys = [ Key::Char('/') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::SearchNext::new());
+ let keys = [ Key::Char('n') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::SearchPrev::new());
+ let keys = [ Key::Char('N') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::BulkRename::new());
+ let keys = [ Key::Char('b'), Key::Char('b') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::SetMode::new());
+ let keys = [ Key::Char('=') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::CommandLine::new(
+ "".to_string(),
+ "".to_string(),
+ ));
+ let keys = [ Key::Char(';') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::CommandLine::new(
+ "mkdir ".to_string(),
+ "".to_string(),
+ ));
+ let keys = [ Key::Char('m'), Key::Char('k') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ let cmd = Box::new(commands::CommandLine::new(
+ "rename ".to_string(),
+ "".to_string(),
+ ));
+ let keys = [ Key::Char('c'), Key::Char('w') ];
+ insert_keycommand(&mut m, cmd, &keys);
+
+ m
+ }
+}
+
+impl ConfigStructure for JoshutoCommandMapping {
+ fn get_config() -> Self {
+ parse_to_config_file::<JoshutoRawCommandMapping, JoshutoCommandMapping>(KEYMAP_FILE)
+ .unwrap_or_else(Self::default)
+ }
+}
#[derive(Debug, Deserialize)]
struct JoshutoMapCommand {
@@ -31,7 +236,15 @@ impl Flattenable<JoshutoCommandMapping> for JoshutoRawCommandMapping {
for m in self.mapcommand {
match commands::from_args(m.command, m.args) {
Ok(command) => {
- let keycodes: Vec<&str> = m.keys.iter().map(|s| s.as_str()).collect();
+ let keycodes: Vec<Key> = m.keys
+ .iter()
+ .filter_map(|s| str_to_key(s.as_str()))
+ .collect();
+
+ if keycodes.len() != m.keys.len() {
+ eprintln!("Failed to parse keycodes: {:?}", m.keys);
+ continue;
+ }
let result = insert_keycommand(&mut keymaps, command, &keycodes);
match result {
@@ -46,17 +259,10 @@ impl Flattenable<JoshutoCommandMapping> for JoshutoRawCommandMapping {
}
}
-impl ConfigStructure for JoshutoCommandMapping {
- fn get_config() -> Self {
- parse_to_config_file::<JoshutoRawCommandMapping, JoshutoCommandMapping>(KEYMAP_FILE)
- .unwrap_or_else(Self::default)
- }
-}
-
fn insert_keycommand(
keymap: &mut JoshutoCommandMapping,
keycommand: Box<dyn JoshutoCommand>,
- keycodes: &[&str],
+ keycodes: &[Key],
) -> Result<(), String> {
let keycode_len = keycodes.len();
@@ -64,13 +270,10 @@ fn insert_keycommand(
return Ok(());
}
- let key = match str_to_key(keycodes[0]) {
- Some(k) => k,
- None => return Err(format!("Unknown keycode: {}", keycodes[0])),
- };
+ let key = keycodes[0];
if keycode_len == 1 {
- match keymap.entry(key) {
+ match keymap.as_mut().entry(key) {
Entry::Occupied(_) => {
return Err(format!("Error: Keybindings ambiguous for {}", keycommand))
}
@@ -79,7 +282,7 @@ fn insert_keycommand(
return Ok(());
}
- match keymap.entry(key) {
+ match keymap.as_mut().entry(key) {
Entry::Occupied(mut entry) => match entry.get_mut() {
CommandKeybind::CompositeKeybind(ref mut m) => {
insert_keycommand(m, keycommand, &keycodes[1..])
diff --git a/src/run.rs b/src/run.rs
index 35a6656..98c9d8d 100644
--- a/src/run.rs
+++ b/src/run.rs
@@ -81,7 +81,7 @@ pub fn run(config_t: JoshutoConfig, keymap_t: JoshutoCommandMapping) -> std::io:
if !context.message_queue.is_empty() {
let _ = context.message_queue.pop_front();
}
- match keymap_t.get(&key) {
+ match keymap_t.as_ref().get(&key) {
None => {
context
.message_queue
diff --git a/src/ui/widgets/tui_menu.rs b/src/ui/widgets/tui_menu.rs
index 5ac008b..e862065 100644
--- a/src/ui/widgets/tui_menu.rs
+++ b/src/ui/widgets/tui_menu.rs
@@ -46,6 +46,7 @@ impl TuiCommandMenu {
{
// draw menu
let mut display_vec: Vec<String> = map
+ .as_ref()
.iter()
.map(|(k, v)| format!(" {:?} {}", k, v))
.collect();
@@ -80,7 +81,7 @@ impl TuiCommandMenu {
Event::Input(key) => {
match key {
Key::Esc => return None,
- key => match map.get(&key) {
+ key => match map.as_ref().get(&key) {
Some(CommandKeybind::SimpleKeybind(s)) => {
return Some(s);
}
diff --git a/src/ui/widgets/tui_view.rs b/src/ui/widgets/tui_view.rs
index 7cfe223..3cc630d 100644
--- a/src/ui/widgets/tui_view.rs
+++ b/src/ui/widgets/tui_view.rs
@@ -102,8 +102,7 @@ impl<'a> Widget for TuiView<'a> {
};
let message_style = Style::default()
- .fg(Color::LightCyan)
- .modifier(Modifier::BOLD);
+ .fg(Color::Yellow);
if self.show_bottom_status {
/* draw the bottom status bar */