diff options
author | qkzk <qu3nt1n@gmail.com> | 2023-01-05 21:41:47 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2023-01-05 21:41:47 +0100 |
commit | 5facb36e6bad997161490f007701683d4f4ee1f2 (patch) | |
tree | b1976a0e7b35c8d3ce1ab815788b37bd9159ed74 | |
parent | a5cea197cd17ab12ca4b6471c31ab36c38f30244 (diff) |
tree: skim & rename
-rw-r--r-- | src/event_exec.rs | 27 | ||||
-rw-r--r-- | src/mode.rs | 4 | ||||
-rw-r--r-- | src/status.rs | 5 |
3 files changed, 19 insertions, 17 deletions
diff --git a/src/event_exec.rs b/src/event_exec.rs index 77cf1fb..43c6fd2 100644 --- a/src/event_exec.rs +++ b/src/event_exec.rs @@ -683,7 +683,9 @@ impl EventExec { /// Enter the rename mode. pub fn event_rename(tab: &mut Tab) -> FmResult<()> { - tab.mode = Mode::InputSimple(InputSimple::Rename); + if let Some(fileinfo) = tab.selected() { + tab.mode = Mode::InputSimple(InputSimple::Rename(fileinfo.path.clone())); + } Ok(()) } @@ -852,18 +854,15 @@ impl EventExec { /// We only tries to rename in the same directory, so it shouldn't be a problem. /// Filename is sanitized before processing. pub fn exec_rename(tab: &mut Tab) -> FmResult<()> { - if tab.path_content.content.is_empty() { - return Err(FmError::custom("event rename", "Empty directory")); + if let Some(fileinfo) = tab.selected() { + info!("fileinfo {:?}", fileinfo); + let path = &fileinfo.path; + if let Some(parent) = path.parent() { + let new_name = parent.join(sanitize_filename::sanitize(tab.input.string())); + info!("new_name {:?}", new_name); + fs::rename(path, new_name)?; + } } - fs::rename( - tab.path_content - .selected_path_string() - .ok_or_else(|| FmError::custom("exec rename", "File not found"))?, - tab.path_content - .path - .to_path_buf() - .join(sanitize_filename::sanitize(tab.input.string())), - )?; tab.refresh_view() } @@ -1160,8 +1159,8 @@ impl EventExec { /// In normal mode, it will open the file. /// Reset to normal mode afterwards. pub fn enter(status: &mut Status) -> FmResult<()> { - match status.selected().mode { - Mode::InputSimple(InputSimple::Rename) => EventExec::exec_rename(status.selected())?, + match status.selected_non_mut().mode { + Mode::InputSimple(InputSimple::Rename(_)) => EventExec::exec_rename(status.selected())?, Mode::InputSimple(InputSimple::Newfile) => EventExec::exec_newfile(status.selected())?, Mode::InputSimple(InputSimple::Newdir) => EventExec::exec_newdir(status.selected())?, Mode::InputSimple(InputSimple::Chmod) => EventExec::exec_chmod(status)?, diff --git a/src/mode.rs b/src/mode.rs index d1e64f0..4e8297b 100644 --- a/src/mode.rs +++ b/src/mode.rs @@ -60,7 +60,7 @@ impl std::fmt::Display for NeedConfirmation { #[derive(Clone)] pub enum InputSimple { /// Rename the selected file - Rename, + Rename(std::path::PathBuf), /// Change permissions of the selected file Chmod, /// Touch a new file @@ -118,7 +118,7 @@ impl fmt::Display for Mode { match *self { Mode::Normal => write!(f, "Normal: "), Mode::Tree => write!(f, "Tree: "), - Mode::InputSimple(InputSimple::Rename) => write!(f, "Rename: "), + Mode::InputSimple(InputSimple::Rename(_)) => write!(f, "Rename: "), Mode::InputSimple(InputSimple::Chmod) => write!(f, "Chmod: "), Mode::InputSimple(InputSimple::Newfile) => write!(f, "Newfile: "), Mode::InputSimple(InputSimple::Newdir) => write!(f, "Newdir: "), diff --git a/src/status.rs b/src/status.rs index 0599dc8..85ba2ff 100644 --- a/src/status.rs +++ b/src/status.rs @@ -146,7 +146,10 @@ impl Status { .skimer .no_source( self.selected_non_mut() - .path_content_str() + .selected() + .ok_or_else(|| FmError::custom("skim", "no selected file"))? + .path + .to_str() .ok_or_else(|| FmError::custom("skim", "skim error"))?, ) .first() |