From 0f7dee03f70a6d298c6a67d47fcf74d079d6d64d Mon Sep 17 00:00:00 2001 From: rqdmap <55649208+rqdmap@users.noreply.github.com> Date: Sun, 3 Sep 2023 01:50:53 +0800 Subject: feat: Add rename_file_append_ext command (#423) * feat: Add rename_file_append_ext command * format code style * swap functionality of two rename command * rename_file_append_base finds the last dot pattern --- docs/configuration/keymap.toml.md | 8 ++++++-- src/commands/rename_file.rs | 12 ++++++++++++ src/key_command/command.rs | 1 + src/key_command/constants.rs | 1 + src/key_command/impl_appcommand.rs | 1 + src/key_command/impl_appexecute.rs | 3 +++ src/key_command/impl_comment.rs | 1 + src/key_command/impl_from_str.rs | 5 +++++ 8 files changed, 30 insertions(+), 2 deletions(-) diff --git a/docs/configuration/keymap.toml.md b/docs/configuration/keymap.toml.md index 68a020b..759784f 100644 --- a/docs/configuration/keymap.toml.md +++ b/docs/configuration/keymap.toml.md @@ -286,8 +286,12 @@ function joshuto() { ### `rename_append`: opens the command prompt with the rename command and the current file name filled in. -- cursor will be set right before the extension of the file - (end of file name if no extension) +- cursor will be set to the end of the file name + +### `rename_append_base`: opens the command prompt with the rename command and the current file name filled in. + +- cursor will be set right after the base name of the file. + (beginning of the file name if no base name) ### `rename_prepend`: opens the command prompt with the rename command and the current file name filled in. diff --git a/src/commands/rename_file.rs b/src/commands/rename_file.rs index 6ef6cd5..e98c598 100644 --- a/src/commands/rename_file.rs +++ b/src/commands/rename_file.rs @@ -67,6 +67,18 @@ pub fn rename_file_append( context: &mut AppContext, backend: &mut AppBackend, keymap_t: &AppKeyMapping, +) -> JoshutoResult { + if let Some(file_name) = _get_current_file_name(context) { + let (prefix, suffix) = (format!("rename {}", file_name), "".to_string()); + command_line::read_and_execute(context, backend, keymap_t, &prefix, &suffix)?; + } + Ok(()) +} + +pub fn rename_file_append_base( + context: &mut AppContext, + backend: &mut AppBackend, + keymap_t: &AppKeyMapping, ) -> JoshutoResult { if let Some(file_name) = _get_current_file_name(context) { let (prefix, suffix): (String, String) = match file_name.rfind('.') { diff --git a/src/key_command/command.rs b/src/key_command/command.rs index 4469ebe..626574e 100644 --- a/src/key_command/command.rs +++ b/src/key_command/command.rs @@ -93,6 +93,7 @@ pub enum Command { new_name: path::PathBuf, }, RenameFileAppend, + RenameFileAppendBase, RenameFilePrepend, RenameFileKeepExt, TouchFile { diff --git a/src/key_command/constants.rs b/src/key_command/constants.rs index 356997a..4510f55 100644 --- a/src/key_command/constants.rs +++ b/src/key_command/constants.rs @@ -53,6 +53,7 @@ cmd_constants![ (CMD_RELOAD_DIRECTORY_LIST, "reload_dirlist"), (CMD_RENAME_FILE, "rename"), (CMD_RENAME_FILE_APPEND, "rename_append"), + (CMD_RENAME_FILE_APPEND_BASE, "rename_append_base"), (CMD_RENAME_FILE_PREPEND, "rename_prepend"), (CMD_RENAME_FILE_KEEP_EXT, "rename_keep_ext"), (CMD_SEARCH_STRING, "search"), diff --git a/src/key_command/impl_appcommand.rs b/src/key_command/impl_appcommand.rs index 655ff64..d555db2 100644 --- a/src/key_command/impl_appcommand.rs +++ b/src/key_command/impl_appcommand.rs @@ -55,6 +55,7 @@ impl AppCommand for Command { Self::ReloadDirList => CMD_RELOAD_DIRECTORY_LIST, Self::RenameFile { .. } => CMD_RENAME_FILE, Self::RenameFileAppend => CMD_RENAME_FILE_APPEND, + Self::RenameFileAppendBase => CMD_RENAME_FILE_APPEND_BASE, Self::RenameFilePrepend => CMD_RENAME_FILE_PREPEND, Self::RenameFileKeepExt => CMD_RENAME_FILE_KEEP_EXT, diff --git a/src/key_command/impl_appexecute.rs b/src/key_command/impl_appexecute.rs index c4571ea..ba9c802 100644 --- a/src/key_command/impl_appexecute.rs +++ b/src/key_command/impl_appexecute.rs @@ -99,6 +99,9 @@ impl AppExecute for Command { Self::ReloadDirList => reload::reload_dirlist(context), Self::RenameFile { new_name } => rename_file::rename_file(context, new_name.as_path()), Self::RenameFileAppend => rename_file::rename_file_append(context, backend, keymap_t), + Self::RenameFileAppendBase => { + rename_file::rename_file_append_base(context, backend, keymap_t) + } Self::RenameFilePrepend => rename_file::rename_file_prepend(context, backend, keymap_t), Self::RenameFileKeepExt => { rename_file::rename_file_keep_ext(context, backend, keymap_t) diff --git a/src/key_command/impl_comment.rs b/src/key_command/impl_comment.rs index 9549621..9282146 100644 --- a/src/key_command/impl_comment.rs +++ b/src/key_command/impl_comment.rs @@ -85,6 +85,7 @@ impl CommandComment for Command { Self::RenameFile { .. } => "Rename file", Self::TouchFile { .. } => "Touch file", Self::RenameFileAppend => "Rename a file", + Self::RenameFileAppendBase => "Rename a file", Self::RenameFilePrepend => "Rename a file", Self::RenameFileKeepExt => "Rename a file", diff --git a/src/key_command/impl_from_str.rs b/src/key_command/impl_from_str.rs index 1080198..55a278e 100644 --- a/src/key_command/impl_from_str.rs +++ b/src/key_command/impl_from_str.rs @@ -83,6 +83,11 @@ impl std::str::FromStr for Command { simple_command_conversion_case!(command, CMD_RELOAD_DIRECTORY_LIST, Self::ReloadDirList); simple_command_conversion_case!(command, CMD_RENAME_FILE_APPEND, Self::RenameFileAppend); + simple_command_conversion_case!( + command, + CMD_RENAME_FILE_APPEND_BASE, + Self::RenameFileAppendBase + ); simple_command_conversion_case!(command, CMD_RENAME_FILE_PREPEND, Self::RenameFilePrepend); simple_command_conversion_case!(command, CMD_RENAME_FILE_KEEP_EXT, Self::RenameFileKeepExt); simple_command_conversion_case!(command, CMD_SEARCH_NEXT, Self::SearchNext); -- cgit v1.2.3