summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2020-11-22 21:51:51 -0500
committerJiayi Zhao <jeff.no.zhao@gmail.com>2020-11-22 21:51:51 -0500
commit0e3ee5aa31368a1501e24beb33ae1020c0ac332e (patch)
tree3313c5002661ba0f04e74256559b6f13964b5616
parent29b19b1f1172632a1ef68a232d1058149be87c4a (diff)
add support for moving parent cursor
-rw-r--r--config/keymap.toml7
-rw-r--r--src/commands/key_command.rs34
-rw-r--r--src/commands/mod.rs1
-rw-r--r--src/commands/parent_cursor_move.rs66
-rw-r--r--src/io/io_worker.rs13
5 files changed, 117 insertions, 4 deletions
diff --git a/config/keymap.toml b/config/keymap.toml
index 0e300da..5afd8b3 100644
--- a/config/keymap.toml
+++ b/config/keymap.toml
@@ -66,6 +66,13 @@ command = "cursor_move_page_down"
keys = [ "page_down" ]
[[mapcommand]]
+command = "parent_cursor_move_up"
+keys = [ "[" ]
+[[mapcommand]]
+command = "parent_cursor_move_down"
+keys = [ "]" ]
+
+[[mapcommand]]
command = "open_file"
keys = [ "right" ]
[[mapcommand]]
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(())
+}
diff --git a/src/io/io_worker.rs b/src/io/io_worker.rs
index 0ff9f43..dbccc70 100644
--- a/src/io/io_worker.rs
+++ b/src/io/io_worker.rs
@@ -27,7 +27,11 @@ impl std::default::Default for IOWorkerOptions {
impl std::fmt::Display for IOWorkerOptions {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
- write!(f, "overwrite={} skip_exist={}", self.overwrite, self.skip_exist)
+ write!(
+ f,
+ "overwrite={} skip_exist={}",
+ self.overwrite, self.skip_exist
+ )
}
}
@@ -48,7 +52,12 @@ pub struct IOWorkerThread {
}
impl IOWorkerThread {
- pub fn new(kind: FileOp, paths: Vec<path::PathBuf>, dest: path::PathBuf, options: IOWorkerOptions) -> Self {
+ pub fn new(
+ kind: FileOp,
+ paths: Vec<path::PathBuf>,
+ dest: path::PathBuf,
+ options: IOWorkerOptions,
+ ) -> Self {
Self {
kind,
options,