diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-11-22 21:51:51 -0500 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-11-22 21:51:51 -0500 |
commit | 0e3ee5aa31368a1501e24beb33ae1020c0ac332e (patch) | |
tree | 3313c5002661ba0f04e74256559b6f13964b5616 /src/commands | |
parent | 29b19b1f1172632a1ef68a232d1058149be87c4a (diff) |
add support for moving parent cursor
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/key_command.rs | 34 | ||||
-rw-r--r-- | src/commands/mod.rs | 1 | ||||
-rw-r--r-- | src/commands/parent_cursor_move.rs | 66 |
3 files changed, 99 insertions, 2 deletions
diff --git a/src/commands/key_command.rs b/src/commands/key_command.rs index 97e655b..e5a2514 100644 --- a/src/commands/key_command.rs +++ b/src/commands/key_command.rs @@ -28,6 +28,9 @@ pub enum KeyCommand { CursorMovePageUp, CursorMovePageDown, + ParentCursorMoveUp(usize), + ParentCursorMoveDown(usize), + DeleteFiles, NewDirectory(path::PathBuf), OpenFile, @@ -79,6 +82,9 @@ impl KeyCommand { 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(_) => "new_directory", Self::OpenFile => "open", @@ -155,6 +161,26 @@ impl KeyCommand { )), }, }, + "parent_cursor_move_down" => match arg { + "" => Ok(Self::ParentCursorMoveDown(1)), + arg => match arg.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.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), @@ -290,6 +316,9 @@ impl JoshutoRunnable for KeyCommand { 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(()) @@ -338,8 +367,9 @@ impl std::fmt::Display for KeyCommand { Self::RenameFile(name) => write!(f, "{} {:?}", self.command(), name), Self::Search(s) => write!(f, "{} {}", self.command(), s), - Self::SelectFiles { toggle, all } => write!(f, "{} toggle={} all={}", - self.command(), toggle, all), + Self::SelectFiles { toggle, all } => { + write!(f, "{} toggle={} all={}", self.command(), toggle, all) + } Self::ShellCommand(c) => write!(f, "{} {:?}", self.command(), c), Self::Sort(t) => write!(f, "{} {}", self.command(), t), Self::TabSwitch(i) => write!(f, "{} {}", self.command(), i), diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 755fd79..3367c73 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -7,6 +7,7 @@ pub mod file_ops; pub mod new_directory; pub mod open_file; pub mod parent_directory; +pub mod parent_cursor_move; pub mod quit; pub mod reload; pub mod rename_file; diff --git a/src/commands/parent_cursor_move.rs b/src/commands/parent_cursor_move.rs new file mode 100644 index 0000000..8234753 --- /dev/null +++ b/src/commands/parent_cursor_move.rs @@ -0,0 +1,66 @@ +use crate::context::JoshutoContext; +use crate::error::JoshutoResult; +use crate::history::DirectoryHistory; +use crate::ui::TuiBackend; +use std::path::PathBuf; + +pub fn parent_cursor_move(new_index: usize, context: &mut JoshutoContext) -> JoshutoResult<()> { + let mut path: Option<PathBuf> = None; + let mut new_index = new_index; + + { + let mut curr_tab = context.tab_context_mut().curr_tab_mut(); + if let Some(curr_list) = curr_tab.parent_list_mut() { + if curr_list.index.is_some() { + let dir_len = curr_list.contents.len(); + if new_index >= dir_len { + new_index = dir_len - 1; + } + let entry = &curr_list.contents[new_index]; + if entry.file_path().is_dir() { + curr_list.index = Some(new_index); + path = Some(entry.file_path().to_path_buf()) + } + } + } + if let Some(path) = path.as_ref() { + curr_tab.set_pwd(path); + } + } + + // get preview + if let Some(path) = path { + if path.is_dir() { + let sort_options = context.config_t.sort_option.clone(); + context + .tab_context_mut() + .curr_tab_mut() + .history_mut() + .create_or_soft_update(path.as_path(), &sort_options)?; + } + } + Ok(()) +} + +pub fn parent_up(context: &mut JoshutoContext, u: usize) -> JoshutoResult<()> { + let movement = match context.tab_context_ref().curr_tab_ref().parent_list_ref() { + Some(list) => list.index.map(|idx| if idx > u { idx - u } else { 0 }), + None => None, + }; + + if let Some(s) = movement { + parent_cursor_move(s, context)?; + } + Ok(()) +} + +pub fn parent_down(context: &mut JoshutoContext, u: usize) -> JoshutoResult<()> { + let movement = match context.tab_context_ref().curr_tab_ref().parent_list_ref() { + Some(list) => list.index.map(|idx| idx + u), + None => None, + }; + if let Some(s) = movement { + parent_cursor_move(s, context)?; + } + Ok(()) +} |