diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-10-02 17:24:16 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-10-02 17:24:54 -0400 |
commit | 65006271ed71fc79ef7d4582b5a676029bf3f65b (patch) | |
tree | 9b4b4f552f8eebb4c161ef936762095efaac721c /src | |
parent | 8b5c257adfc8177cc4413309c0d1fc98d854ac1d (diff) |
move key_command code out into separate module
- make separate files for trait impls
- make separate file for constants
Diffstat (limited to 'src')
-rw-r--r-- | src/commands/command_line.rs | 4 | ||||
-rw-r--r-- | src/commands/help.rs | 2 | ||||
-rw-r--r-- | src/commands/key_command.rs | 594 | ||||
-rw-r--r-- | src/commands/mod.rs | 6 | ||||
-rw-r--r-- | src/commands/show_workers.rs | 4 | ||||
-rw-r--r-- | src/config/keymap/keymapping.rs | 2 | ||||
-rw-r--r-- | src/key_command/command_keybind.rs (renamed from src/commands/command_keybind.rs) | 14 | ||||
-rw-r--r-- | src/key_command/constants.rs | 51 | ||||
-rw-r--r-- | src/key_command/impl_appcommand.rs | 70 | ||||
-rw-r--r-- | src/key_command/impl_appexecute.rs | 94 | ||||
-rw-r--r-- | src/key_command/impl_display.rs | 29 | ||||
-rw-r--r-- | src/key_command/impl_from_str.rs | 279 | ||||
-rw-r--r-- | src/key_command/key_command.rs | 157 | ||||
-rw-r--r-- | src/key_command/mod.rs | 14 | ||||
-rw-r--r-- | src/key_command/traits.rs | 17 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/run.rs | 4 | ||||
-rw-r--r-- | src/ui/views/tui_command_menu.rs | 3 | ||||
-rw-r--r-- | src/ui/widgets/tui_help.rs | 2 | ||||
-rw-r--r-- | src/util/input.rs | 3 |
20 files changed, 722 insertions, 628 deletions
diff --git a/src/commands/command_line.rs b/src/commands/command_line.rs index 8680de0..5b8eb44 100644 --- a/src/commands/command_line.rs +++ b/src/commands/command_line.rs @@ -1,14 +1,12 @@ use std::str::FromStr; -use crate::commands::KeyCommand; use crate::config::AppKeyMapping; use crate::context::AppContext; use crate::error::JoshutoResult; +use crate::key_command::{AppExecute, KeyCommand}; use crate::ui::views::TuiTextField; use crate::ui::TuiBackend; -use super::AppExecute; - pub fn read_and_execute( context: &mut AppContext, backend: &mut TuiBackend, diff --git a/src/commands/help.rs b/src/commands/help.rs index 3a01d91..44bbeb8 100644 --- a/src/commands/help.rs +++ b/src/commands/help.rs @@ -1,10 +1,10 @@ use termion::event::{Event, Key}; -use crate::commands::{CommandKeybind, KeyCommand}; use crate::config::AppKeyMapping; use crate::context::AppContext; use crate::error::JoshutoResult; use crate::event::AppEvent; +use crate::key_command::{CommandKeybind, KeyCommand}; use crate::ui::widgets; use crate::ui::widgets::TuiHelp; use crate::ui::TuiBackend; diff --git a/src/commands/key_command.rs b/src/commands/key_command.rs deleted file mode 100644 index 102a586..0000000 --- a/src/commands/key_command.rs +++ /dev/null @@ -1,594 +0,0 @@ -use std::path; - -use termion::event::Key; - -use crate::config::AppKeyMapping; -use crate::context::AppContext; -use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult}; -use crate::io::IoWorkerOptions; -use crate::ui::TuiBackend; -use crate::util::keyparse::str_to_key; -use crate::util::select::SelectOption; -use crate::util::sort::SortType; - -use crate::HOME_DIR; -use dirs_next::home_dir; -use shellexpand::tilde_with_context; - -use super::*; - -#[derive(Clone, Debug)] -pub enum KeyCommand { - BulkRename, - ChangeDirectory(path::PathBuf), - CommandLine(String, String), - - CutFiles, - CopyFiles, - PasteFiles(IoWorkerOptions), - CopyFileName, - CopyFileNameWithoutExtension, - CopyFilePath, - CopyDirPath, - - CursorMoveUp(usize), - CursorMoveDown(usize), - CursorMoveHome, - CursorMoveEnd, - CursorMovePageUp, - CursorMovePageDown, - - ParentCursorMoveUp(usize), - ParentCursorMoveDown(usize), - - // ChildCursorMoveUp(usize), - // ChildCursorMoveDown(usize), - DeleteFiles, - NewDirectory(path::PathBuf), - OpenFile, - OpenFileWith(Option<usize>), - ParentDirectory, - - Quit, - QuitToCurrentDirectory, - ForceQuit, - ReloadDirList, - RenameFile(path::PathBuf), - RenameFileAppend, - RenameFilePrepend, - TouchFile(String), - - SearchGlob(String), - SearchString(String), - SearchSkim, - SearchNext, - SearchPrev, - - SelectFiles(String, SelectOption), - SetMode, - SubProcess(Vec<String>, bool), - ShowWorkers(Key), - - ToggleHiddenFiles, - - Sort(SortType), - SortReverse, - - NewTab, - CloseTab, - TabSwitch(i32), - TabSwitchIndex(u32), - Help, -} - -impl KeyCommand { - pub const fn command(&self) -> &'static str { - match self { - Self::BulkRename => "bulk_rename", - Self::ChangeDirectory(_) => "cd", - Self::NewTab => "new_tab", - Self::CloseTab => "close_tab", - Self::CommandLine(_, _) => ":", - - Self::CutFiles => "cut_files", - Self::CopyFiles => "copy_files", - Self::PasteFiles(_) => "paste_files", - Self::CopyFileName => "copy_filename", - Self::CopyFileNameWithoutExtension => "copy_filename_without_extension", - Self::CopyFilePath => "copy_filepath", - Self::CopyDirPath => "copy_dirpath", - - Self::CursorMoveUp(_) => "cursor_move_up", - Self::CursorMoveDown(_) => "cursor_move_down", - Self::CursorMoveHome => "cursor_move_home", - Self::CursorMoveEnd => "cursor_move_end", - Self::CursorMovePageUp => "cursor_move_page_up", - Self::CursorMovePageDown => "cursor_move_page_down", - - Self::ParentCursorMoveUp(_) => "parent_cursor_move_up", - Self::ParentCursorMoveDown(_) => "parent_cursor_move_down", - - Self::DeleteFiles => "delete_files", - Self::NewDirectory(_) => "mkdir", - Self::OpenFile => "open", - Self::OpenFileWith(_) => "open_with", - Self::ParentDirectory => "cd ..", - - Self::Quit => "quit", - Self::QuitToCurrentDirectory => "quit_to_cwd", - Self::ForceQuit => "force_quit", - Self::ReloadDirList => "reload_dirlist", - Self::RenameFile(_) => "rename", - Self::TouchFile(_) => "touch", - Self::RenameFileAppend => "rename_append", - Self::RenameFilePrepend => "rename_prepend", - - Self::SearchString(_) => "search", - Self::SearchGlob(_) => "search_glob", - Self::SearchSkim => "search_skim", - Self::SearchNext => "search_next", - Self::SearchPrev => "search_prev", - - Self::SelectFiles(_, _) => "select", - Self::SetMode => "set_mode", - Self::SubProcess(_, false) => "shell", - Self::SubProcess(_, true) => "spawn", - Self::ShowWorkers(_) => "show_workers", - - Self::ToggleHiddenFiles => "toggle_hidden", - - Self::Sort(_) => "sort", - Self::SortReverse => "sort reverse", - - Self::TabSwitch(_) => "tab_switch", - Self::TabSwitchIndex(_) => "tab_switch_index", - Self::Help => "help", - } - } - - // These comments are displayed at the help page - pub fn comment(&self) -> &'static str { - match self { - Self::BulkRename => "Bulk rename", - Self::ChangeDirectory(_) => "Change directory", - Self::NewTab => "Open a new tab", - Self::CloseTab => "Close current tab", - Self::CommandLine(command, _) => match command.trim() { - "cd" => "Change directory", - "search" => "Open a search prompt", - "search_glob" => "Glob search", - "rename" => "Rename selected file", - "touch" => "Touch file", - "mkdir" => "Make a new directory", - _ => "Open a command line", - }, - - Self::CutFiles => "Cut selected files", - Self::CopyFiles => "Copy selected files", - Self::PasteFiles(IoWorkerOptions { - overwrite, - skip_exist, - }) => match (overwrite, skip_exist) { - (true, false) => "Paste, overwrite", - (false, true) => "Paste, skip existing files", - _ => "Paste", - }, - Self::CopyFileName => "Copy filename", - Self::CopyFileNameWithoutExtension => "Copy filename without extension", - Self::CopyFilePath => "Copy path to file", - Self::CopyDirPath => "Copy directory name", - - Self::CursorMoveUp(_) => "Move cursor up", - Self::CursorMoveDown(_) => "Move cursor down", - Self::CursorMoveHome => "Move cursor to the very top", - Self::CursorMoveEnd => "Move cursor to the ver bottom", - Self::CursorMovePageUp => "Move cursor one page up", - Self::CursorMovePageDown => "Move cursor one page down", - - Self::ParentCursorMoveUp(_) => "Cursor up in parent list", - Self::ParentCursorMoveDown(_) => "Cursor down in parent list", - - Self::DeleteFiles => "Delete selected files", - Self::NewDirectory(_) => "Make a new directory", - Self::OpenFile => "Open a file", - Self::OpenFileWith(_) => "Open using selected program", - Self::ParentDirectory => "CD to parent directory", - - Self::Quit => "Quit the program", - Self::QuitToCurrentDirectory => "Quit to current directory", - Self::ForceQuit => "Force quit", - Self::ReloadDirList => "Reload current dir listing", - Self::RenameFile(_) => "Rename file", - Self::TouchFile(_) => "Touch file", - Self::RenameFileAppend => "Rename a file", - Self::RenameFilePrepend => "Rename a file", - - Self::SearchString(_) => "Search", - Self::SearchGlob(_) => "Search with globbing", - Self::SearchSkim => "Search via skim", - Self::SearchNext => "Next search entry", - Self::SearchPrev => "Previous search entry", - - Self::SelectFiles(_, _) => "Select file", - Self::SetMode => "Set file permissions", - Self::SubProcess(_, false) => "Run a shell command", - Self::SubProcess(_, true) => "Run commmand in background", - Self::ShowWorkers(_) => "Show IO workers", - - Self::ToggleHiddenFiles => "Toggle hidden files displaying", - - Self::Sort(sort_type) => match sort_type { - SortType::Lexical => "Sort lexically", - SortType::Mtime => "Sort by modifiaction time", - SortType::Natural => "Sort naturally", - SortType::Size => "Sort by size", - SortType::Ext => "Sort by extension", - }, - Self::SortReverse => "Reverse sort order", - - Self::TabSwitch(_) => "Swith to the next tab", - Self::TabSwitchIndex(_) => "Swith to a given tab", - Self::Help => "Open this help page", - } - } -} - -impl std::str::FromStr for KeyCommand { - type Err = JoshutoError; - - fn from_str(s: &str) -> Result<Self, Self::Err> { - if let Some(stripped) = s.strip_prefix(':') { - return Ok(Self::CommandLine(stripped.to_owned(), "".to_owned())); - } - - let (command, arg) = match s.find(' ') { - Some(i) => (&s[..i], s[i..].trim_start()), - None => (s, ""), - }; - - match command { - "bulk_rename" => Ok(Self::BulkRename), - "cd" => match arg { - "" => match HOME_DIR.as_ref() { - Some(s) => Ok(Self::ChangeDirectory(s.clone())), - None => Err(JoshutoError::new( - JoshutoErrorKind::EnvVarNotPresent, - format!("{}: Cannot find home directory", command), - )), - }, - ".." => Ok(Self::ParentDirectory), - arg => Ok({ - let path_accepts_tilde = tilde_with_context(arg, home_dir); - Self::ChangeDirectory(path::PathBuf::from(path_accepts_tilde.as_ref())) - }), - }, - "close_tab" => Ok(Self::CloseTab), - "copy_files" => Ok(Self::CopyFiles), - "copy_filename" => Ok(Self::CopyFileName), - "copy_filename_without_extension" => Ok(Self::CopyFileNameWithoutExtension), - "copy_filepath" => Ok(Self::CopyFilePath), - "copy_dirpath" => Ok(Self::CopyDirPath), - "cursor_move_home" => Ok(Self::CursorMoveHome), - "cursor_move_end" => Ok(Self::CursorMoveEnd), - "cursor_move_page_up" => Ok(Self::CursorMovePageUp), - "cursor_move_page_down" => Ok(Self::CursorMovePageDown), - "cursor_move_down" => match arg { - "" => Ok(Self::CursorMoveDown(1)), - arg => match arg.trim().parse::<usize>() { - Ok(s) => Ok(Self::CursorMoveDown(s)), - Err(e) => Err(JoshutoError::new( - JoshutoErrorKind::ParseError, - e.to_string(), - )), - }, - }, - "cursor_move_up" => match arg { - "" => Ok(Self::CursorMoveUp(1)), - arg => match arg.trim().parse::<usize>() { - Ok(s) => Ok(Self::CursorMoveUp(s)), - Err(e) => Err(JoshutoError::new( - JoshutoErrorKind::ParseError, - e.to_string(), - )), - }, - }, - "parent_cursor_move_down" => match arg { - "" => Ok(Self::ParentCursorMoveDown(1)), - arg => match arg.trim().parse::<usize>() { - Ok(s) => Ok(Self::ParentCursorMoveDown(s)), - Err(e) => Err(JoshutoError::new( - JoshutoErrorKind::ParseError, - e.to_string(), - )), - }, - }, - "parent_cursor_move_up" => match arg { - "" => Ok(Self::ParentCursorMoveUp(1)), - arg => match arg.trim().parse::<usize>() { - Ok(s) => Ok(Self::ParentCursorMoveUp(s)), - Err(e) => Err(JoshutoError::new( - JoshutoErrorKind::ParseError, - e.to_string(), - )), - }, - }, - "cut_files" => Ok(Self::CutFiles), - "delete_files" => Ok(Self::DeleteFiles), - "force_quit" => Ok(Self::ForceQuit), - "mkdir" => { - if arg.is_empty() { - Err(JoshutoError::new( - JoshutoErrorKind::InvalidParameters, - format!("{}: no directory name given", command), - )) - } else { - Ok(Self::NewDirectory(path::PathBuf::from(arg))) - } - } - "new_tab" => Ok(Self::NewTab), - "open" => Ok(Self::OpenFile), - "open_with" => match arg { - "" => Ok(Self::OpenFileWith(None)), - arg => match arg.trim().parse::<usize>() { - Ok(s) => Ok(Self::OpenFileWith(Some(s))), - Err(e) => Err(JoshutoError::new( - JoshutoErrorKind::ParseError, - e.to_string(), - )), - }, - }, - "paste_files" => { - let mut options = IoWorkerOptions::default(); - for arg in arg.split_whitespace() { - match arg { - "--overwrite=true" => options.overwrite = true, - "--skip_exist=true" => options.skip_exist = true, - "--overwrite=false" => options.overwrite = false, - "--skip_exist=false" => options.skip_exist = false, - _ => { - return Err(JoshutoError::new( - JoshutoErrorKind::UnrecognizedArgument, - format!("{}: unknown option '{}'", command, arg), - )); - } - } - } - Ok(Self::PasteFiles(options)) - } - "quit" => Ok(Self::Quit), - "quit_to_cwd" => Ok(Self::QuitToCurrentDirectory), - "reload_dirlist" => Ok(Self::ReloadDirList), - "rename" => match arg { - "" => Err(JoshutoError::new( - JoshutoErrorKind::InvalidParameters, - format!("{}: Expected 1, got 0", command), - )), - arg => { - let path: path::PathBuf = path::PathBuf::from(arg); - Ok(Self::RenameFile(path)) - } - }, - "touch" => Ok(Self::TouchFile(arg.to_string())), - "rename_append" => Ok(Self::RenameFileAppend), - "rename_prepend" => Ok(Self::RenameFilePrepend), - "search" => match arg { - "" => Err(JoshutoError::new( - JoshutoErrorKind::InvalidParameters, - format!("{}: Expected 1, got 0", command), - )), - arg => Ok(Self::SearchString(arg.to_string())), - }, - "search_glob" => match arg { - "" => Err(JoshutoError::new( - JoshutoErrorKind::InvalidParameters, - format!("{}: Expected 1, got 0", command), - )), - arg => Ok(Self::SearchGlob(arg.to_string())), - }, - "search_skim" => Ok(Self::SearchSkim), - "search_next" => Ok(Self::SearchNext), - "search_prev" => Ok(Self::SearchPrev), - "select" => { - let mut options = SelectOption::default(); - let mut pattern = ""; - match shell_words::split(arg) { - Ok(args) => { - for arg in args.iter() { - match arg.as_str() { - "--toggle=true" => options.toggle = true, - "--all=true" => options.all = true, - "--toggle=false" => options.toggle = false, - "--all=false" => options.all = false, - "--deselect=true" => options.reverse = true, - "--deselect=false" => options.reverse = false, - s => pattern = s, - } - } - Ok(Self::SelectFiles(pattern.to_string(), options)) - } - Err(e) => Err(JoshutoError::new( - JoshutoErrorKind::InvalidParameters, - format!("{}: {}", arg, e), - )), - } - } - "set_mode" => Ok(Self::SetMode), - "shell" | "spawn" => match shell_words::split(arg) { - Ok(s) if !s.is_empty() => Ok(Self::SubProcess(s, command == "spawn")), - Ok(_) => Err(JoshutoError::new( - JoshutoErrorKind::InvalidParameters, - format!("{}: No commands given", command), - )), - Err(e) => Err(JoshutoError::new( - JoshutoErrorKind::InvalidParameters, - format!("{}: {}", arg, e), - )), - }, - "show_workers" => match shell_words::split(arg) { - Ok(args) => { - let mut exit_key = Key::Esc; - for arg in args.iter() { - if let Some(key_str) = arg.strip_prefix("--exit-key=") { - match str_to_key(key_str) { - Some(key) => exit_key = key, - _ => eprintln!("Error: failed to parse key '{}'", arg), - } - } - } - Ok(Self::ShowWorkers(exit_key)) - } - Err(e) => Err(JoshutoError::new( - JoshutoErrorKind::InvalidParameters, - format!("{}: {}", arg, e), - )), - }, - "sort" => match arg { - "reverse" => Ok(Self::SortReverse), - arg => match SortType::parse(arg) { - Some(s) => Ok(Self::Sort(s)), - None => Err(JoshutoError::new( - JoshutoErrorKind::InvalidParameters, - format!("{}: Unknown option '{}'", command, arg), - )), - }, - }, - "tab_switch" => match arg.parse::<i32>() { - Ok(s) => Ok(Self::TabSwitch(s)), - Err(e) => Err(JoshutoError::new( - JoshutoErrorKind::InvalidParameters, - format!("{}: {}", command, e.to_string()), - )), - }, - "tab_switch_index" => match arg.parse::<u32>() { - Ok(s) => Ok(Self::TabSwitchIndex(s)), - Err(e) => Err(JoshutoError::new( - JoshutoErrorKind::InvalidParameters, - format!("{}: {}", command, e.to_string()), - )), - }, - "toggle_hidden" => Ok(Self::ToggleHiddenFiles), - "help" => Ok(Self::Help), - inp => Err(JoshutoError::new( - JoshutoErrorKind::UnrecognizedCommand, - format!("Unrecognized command '{}'", inp), - )), - } - } -} - -impl AppExecute for KeyCommand { - fn execute( - &self, - context: &mut AppContext, - backend: &mut TuiBackend, - keymap_t: &AppKeyMapping, - ) -> JoshutoResult<()> { - match &*self { - Self::BulkRename => bulk_rename::bulk_rename(context, backend), - Self::ChangeDirectory(p) => { - change_directory::change_directory(context, p.as_path())?; - Ok(()) - } - Self::NewTab => tab_ops::new_tab(context), - Self::CloseTab => tab_ops::close_tab(context), - Self::CommandLine(p, s) => { - command_line::read_and_execute(context, backend, keymap_t, p.as_str(), s.as_str()) - } - Self::CutFiles => file_ops::cut(context), - Self::CopyFiles => file_ops::copy(context), - Self::PasteFiles(options) => file_ops::paste(context, *options), - Self::CopyFileName => file_ops::copy_filename(context), - Self::CopyFileNameWithoutExtension => { - file_ops::copy_filename_without_extension(context) - } - Self::CopyFilePath => file_ops::copy_filepath(context), - Self::CopyDirPath => file_ops::copy_dirpath(context), - - Self::CursorMoveUp(u) => cursor_move::up(context, *u), - Self::CursorMoveDown(u) => cursor_move::down(context, *u), - Self::CursorMoveHome => cursor_move::home(context), - Self::CursorMoveEnd => cursor_move::end(context), - Self::CursorMovePageUp => cursor_move::page_up(context, backend), - Self::CursorMovePageDown => cursor_move::page_down(context, backend), - - Self::ParentCursorMoveUp(u) => parent_cursor_move::parent_up(context, *u), - Self::ParentCursorMoveDown(u) => parent_cursor_move::parent_down(context, *u), - - Self::DeleteFiles => { - delete_files::delete_selected_files(context, backend)?; - Ok(()) - } - Self::NewDirectory(p) => new_directory::new_directory(context, p.as_path()), - Self::OpenFile => open_file::open(context, backend), - Self::OpenFileWith(None) => open_file::open_with_interactive(context, backend), - Self::OpenFileWith(Some(i)) => open_file::open_with_index(context, backend, *i), - Self::ParentDirectory => parent_directory::parent_directory(context), - - Self::Quit => quit::quit(context), - Self::QuitToCurrentDirectory => quit::quit_to_current_directory(context), - Self::ForceQuit => quit::force_quit(context), - - Self::ReloadDirList => reload::reload_dirlist(context), - Self::RenameFile(p) => rename_file::rename_file(context, p.as_path()), - Self::RenameFileAppend => rename_file::rename_file_append(context, backend, keymap_t), - Self::RenameFilePrepend => rename_file::rename_file_prepend(context, backend, keymap_t), - Self::TouchFile(arg) => touch_file::touch_file(context, arg.as_str()), - Self::SearchGlob(pattern) => search_glob::search_glob(context, pattern.as_str()), - Self::SearchString(pattern) => search_string::search_string(context, pattern.as_str()), - Self::SearchSkim => search_skim::search_skim(context, backend), - Self::SearchNext => search::search_next(context), - Self::SearchPrev => search::search_prev(context), - - Self::SelectFiles(pattern, options) => { - selection::select_files(context, pattern.as_str(), options) - } - Self::SetMode => set_mode::set_mode(context, backend), - Self::SubProcess(v, spawn) => { - sub_process::sub_process(context, backend, v.as_slice(), *spawn) - } - Self::ShowWorkers(k) => show_workers::show_workers(context, backend, k), - - Self::ToggleHiddenFiles => show_hidden::toggle_hidden(context), - - Self::Sort(t) => sort::set_sort(context, *t), - Self::SortReverse => sort::toggle_reverse(context), - - Self::TabSwitch(i) => { - tab_ops::tab_switch(*i, context)?; - Ok(()) - } - Self::TabSwitchIndex(i) => tab_ops::tab_switch_index(*i as usize, context), - Self::Help => help::help_loop(context, backend, keymap_t), - } - } -} - -impl std::fmt::Display for KeyCommand { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - match &*self { - Self::ChangeDirectory(p) => write!(f, "{} {:?}", self.command(), p), - Self::CommandLine(s, p) => write!(f, "{} {} {}", self.command(), s, p), - Self::CursorMoveUp(i) => write!(f, "{} {}", self.command(), i), - Self::CursorMoveDown(i) => write!(f, "{} {}", self.command(), i), - - Self::NewDirectory(d) => write!(f, "{} {:?}", self.command(), d), - - Self::PasteFiles(options) => write!(f, "{} {}", self.command(), options), - - Self::RenameFile(name) => write!(f, "{} {:?}", self.command(), name), - - Self::SearchGlob(s) => write!(f, "{} {}", self.command(), s), - Self::SearchString(s) => write!(f, "{} {}", self.command(), s), - Self::SelectFiles(pattern, options) => { - write!(f, "{} {} {}", self.command(), pattern, options) - } - Self::SubProcess(c, _) => write!(f, "{} {:?}", self.command(), c), - Self::Sort(t) => write!(f, "{} {}", self.command(), t), - Self::TabSwitch(i) => write!(f, "{} {}", self.command(), i), - Self::TabSwitchIndex(i) => write!(f, "{} {}", self.command(), i), - _ => write!(f, "{}", self.command()), - } - } -} - -impl AppCommand for KeyCommand {} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index d294b60..68ae2e8 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -24,9 +24,3 @@ pub mod sort; pub mod sub_process; pub mod tab_ops; pub mod touch_file; - -pub mod command_keybind; -pub mod key_command; - -pub use self::command_keybind::{AppCommand, AppExecute, CommandKeybind}; -pub use self::key_command::KeyCommand; diff --git a/src/commands/show_workers.rs b/src/commands/show_workers.rs index d73245f..3250f5b 100644 --- a/src/commands/show_workers.rs +++ b/src/commands/show_workers.rs @@ -1,5 +1,6 @@ use termion::event::{Event, Key}; +use crate::config::AppKeyMapping; use crate::context::AppContext; use crate::error::JoshutoResult; use crate::event::AppEvent; @@ -10,7 +11,7 @@ use crate::util::input; pub fn show_workers( context: &mut AppContext, backend: &mut TuiBackend, - exit_key: &Key, + keymap_t: &AppKeyMapping, ) -> JoshutoResult<()> { context.flush_event(); @@ -22,7 +23,6 @@ pub fn show_workers( AppEvent::Termion(event) => { match event { Event::Key(Key::Esc) => break, - Event::Key(k) if k == *exit_key => break, _ => {} } context.flush_event(); diff --git a/src/config/keymap/keymapping.rs b/src/config/keymap/keymapping.rs index 071bdf7..6cb682f 100644 --- a/src/config/keymap/keymapping.rs +++ b/src/config/keymap/keymapping.rs @@ -7,9 +7,9 @@ use std::str::FromStr; use termion::event::MouseEvent; use termion::event::{Event, Key}; -use crate::commands::{CommandKeybind, KeyCommand}; use crate::config::{parse_to_config_file, ConfigStructure, Flattenable}; use crate::io::IoWorkerOptions; +use crate::key_command::{CommandKeybind, KeyCommand}; use crate::util::keyparse::str_to_event; #[derive(Debug, Deserialize)] diff --git a/src/commands/command_keybind.rs b/src/key_command/command_keybind.rs index afe87ef..3a45fe6 100644 --- a/src/commands/command_keybind.rs +++ b/src/key_command/command_keybind.rs @@ -1,7 +1,4 @@ use crate::config::AppKeyMapping; -use crate::context::AppContext; -use crate::error::JoshutoResult; -use crate::ui::TuiBackend; use super::KeyCommand; @@ -19,14 +16,3 @@ impl std::fmt::Display for CommandKeybind { } } } - -pub trait AppExecute { - fn execute( - &self, - context: &mut AppContext, - backend: &mut TuiBackend, - keymap_t: &AppKeyMapping, - ) -> JoshutoResult<()>; -} - -pub trait AppCommand: AppExecute + std::fmt::Display + std::fmt::Debug {} diff --git a/src/key_command/constants.rs b/src/key_command/constants.rs new file mode 100644 index 0000000..c9778e6 --- /dev/null +++ b/src/key_command/constants.rs @@ -0,0 +1,51 @@ +pub const CMD_HELP: &str = "help"; + +pub const CMD_QUIT: &str = "quit"; +pub const CMD_QUIT_TO_CURRENT_DIRECTORY: &str = "quit_to_cwd"; +pub const CMD_FORCE_QUIT: &str = "force_quit"; + +pub const CMD_BULK_RENAME: &str = "bulk_rename"; +pub const CMD_CHANGE_DIRECTORY: &str = "cd"; +pub const CMD_NEW_TAB: &str = "new_tab"; +pub const CMD_CLOSE_TAB: &str = "close_tab"; +pub const CMD_COMMAND_LINE: &str = ":"; +pub const CMD_CUT_FILES: &str = "cut_files"; +pub const CMD_COPY_FILES: &str = "copy_files"; +pub const CMD_PASTE_FILES: &str = "paste_files"; +pub const CMD_COPY_FILENAME: &str = "copy_filename"; +pub const CMD_COPY_FILENAME_WITHOUT_EXTENSION: &str = "copy_filename_without_extension"; +pub const CMD_COPY_FILEPATH: &str = "copy_filepath"; +pub const CMD_COPY_DIRECTORY_PATH: &str = "copy_dirpath"; +pub const CMD_CURSOR_MOVE_UP: &str = "cursor_move_up"; +pub const CMD_CURSOR_MOVE_DOWN: &str = "cursor_move_down"; +pub const CMD_CURSOR_MOVE_HOME: &str = "cursor_move_home"; +pub const CMD_CURSOR_MOVE_END: &str = "cursor_move_end"; +pub const CMD_CURSOR_MOVE_PAGEUP: &str = "cursor_move_page_up"; +pub const CMD_CURSOR_MOVE_PAGEDOWN: &str = "cursor_move_page_down"; +pub const CMD_PARENT_CURSOR_MOVE_UP: &str = "parent_cursor_move_up"; +pub const CMD_PARENT_CURSOR_MOVE_DOWN: &str = "parent_cursor_move_down"; +pub const CMD_DELETE_FILES: &str = "delete_files"; +pub const CMD_NEW_DIRECTORY: &str = "mkdir"; +pub const CMD_OPEN_FILE: &str = "open"; +pub const CMD_OPEN_FILE_WITH: &str = "open_with"; +pub const CMD_PARENT_DIRECTORY: &str = "cd .."; +pub const CMD_RELOAD_DIRECTORY_LIST: &str = "reload_dirlist"; +pub const CMD_RENAME_FILE: &str = "rename"; +pub const CMD_RENAME_FILE_APPEND: &str = "rename_append"; +pub const CMD_RENAME_FILE_PREPEND: &str = "rename_prepend"; +pub const CMD_SEARCH_STRING: &str = "search"; +pub const CMD_SEARCH_GLOB: &str = "search_glob"; +pub const CMD_SEARCH_SKIM: &str = "search_skim"; +pub const CMD_SEARCH_NEXT: &str = "search_next"; +pub const CMD_SEARCH_PREV: &str = "search_prev"; +pub const CMD_SELECT_FILES: &str = "select"; +pub const CMD_SET_MODE: &str = "set_mode"; +pub const CMD_SORT: &str = "sort"; |