diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-05-13 11:52:45 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-05-13 11:52:45 -0400 |
commit | b60cd0e5ed3d6b40545062c98ff2e04678170b21 (patch) | |
tree | 96103e91ab0f6dc9c84e6893c16dd40535cb66f8 /src | |
parent | 176f0d51dadfa8e4c58a2971e12f139892421f47 (diff) |
fix naming and add open_with id option
Diffstat (limited to 'src')
-rw-r--r-- | src/commands/key_command.rs | 28 | ||||
-rw-r--r-- | src/commands/open_file.rs | 38 | ||||
-rw-r--r-- | src/config/keymap/keymapping.rs | 2 |
3 files changed, 57 insertions, 11 deletions
diff --git a/src/commands/key_command.rs b/src/commands/key_command.rs index ddda0ec..bac766e 100644 --- a/src/commands/key_command.rs +++ b/src/commands/key_command.rs @@ -40,7 +40,7 @@ pub enum KeyCommand { DeleteFiles, NewDirectory(path::PathBuf), OpenFile, - OpenFileWith, + OpenFileWith(Option<usize>), ParentDirectory, Quit, @@ -77,7 +77,7 @@ impl KeyCommand { Self::ChangeDirectory(_) => "cd", Self::NewTab => "new_tab", Self::CloseTab => "close_tab", - Self::CommandLine(_, _) => "console", + Self::CommandLine(_, _) => ":", Self::CutFiles => "cut_files", Self::CopyFiles => "copy_files", @@ -95,15 +95,15 @@ impl KeyCommand { Self::ParentCursorMoveDown(_) => "parent_cursor_move_down", Self::DeleteFiles => "delete_files", - Self::NewDirectory(_) => "new_directory", + Self::NewDirectory(_) => "mkdir", Self::OpenFile => "open", - Self::OpenFileWith => "open_with", + Self::OpenFileWith(_) => "open_with", Self::ParentDirectory => "cd ..", Self::Quit => "quit", Self::ForceQuit => "force_quit", Self::ReloadDirList => "reload_dirlist", - Self::RenameFile(_) => "rename_file", + Self::RenameFile(_) => "rename", Self::RenameFileAppend => "rename_append", Self::RenameFilePrepend => "rename_prepend", @@ -211,11 +211,20 @@ impl KeyCommand { } else { Ok(Self::NewDirectory(path::PathBuf::from(arg))) } - } + }, "new_tab" => Ok(Self::NewTab), - "open_file" => Ok(Self::OpenFile), - "open_file_with" => Ok(Self::OpenFileWith), + "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() { @@ -362,7 +371,8 @@ impl AppExecute for KeyCommand { } Self::NewDirectory(p) => new_directory::new_directory(context, p.as_path()), Self::OpenFile => open_file::open(context, backend), - Self::OpenFileWith => open_file::open_with(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), diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index 71946d3..b51967f 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -133,7 +133,7 @@ where } } -pub fn open_with(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult<()> { +pub fn open_with_interactive(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult<()> { let paths = context .tab_context_ref() .curr_tab_ref() @@ -152,3 +152,39 @@ pub fn open_with(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoR open_with_helper(context, backend, options, files)?; Ok(()) } + +pub fn open_with_index(context: &mut AppContext, backend: &mut TuiBackend, index: usize) -> JoshutoResult<()> { + let paths = context + .tab_context_ref() + .curr_tab_ref() + .curr_list_ref() + .map_or(vec![], |s| s.get_selected_paths()); + + if paths.is_empty() { + return Err(JoshutoError::new( + JoshutoErrorKind::Io(io::ErrorKind::NotFound), + String::from("No files selected"), + )); + } + let files: Vec<&std::ffi::OsStr> = paths.iter().filter_map(|e| e.file_name()).collect(); + let options = get_options(paths[0].as_path()); + + if index >= options.len() { + return Err(JoshutoError::new( + JoshutoErrorKind::Io(std::io::ErrorKind::InvalidData), + "option does not exist".to_string(), + )); + } + + let mimetype_entry = &options[index]; + if mimetype_entry.get_fork() { + mimetype_entry.execute_with(files.as_slice())?; + Ok(()) + } else { + backend.terminal_drop(); + let res = mimetype_entry.execute_with(files.as_slice()); + backend.terminal_restore()?; + res?; + Ok(()) + } +} diff --git a/src/config/keymap/keymapping.rs b/src/config/keymap/keymapping.rs index 3b8313f..6ceb4a4 100644 --- a/src/config/keymap/keymapping.rs +++ b/src/config/keymap/keymapping.rs @@ -167,7 +167,7 @@ impl AppKeyMapping { let keys = [Event::Key(Key::BackTab)]; insert_keycommand(&mut m, cmd, &keys)?; - let cmd = KeyCommand::OpenFileWith; + let cmd = KeyCommand::OpenFileWith(None); let keys = [Event::Key(Key::Char('r'))]; insert_keycommand(&mut m, cmd, &keys)?; |