From 76dd2d93e739dd165ae1a36a195fd5715cb40a63 Mon Sep 17 00:00:00 2001 From: Azad <49314270+Akmadan23@users.noreply.github.com> Date: Wed, 3 Apr 2024 19:12:57 +0200 Subject: feat: add `--last` option to `new_tab` command (#517) * feat: add `--last` option to `new_tab` command * Better flag detection * Even better flag detection --- docs/configuration/keymap.toml.md | 4 +++- src/commands/tab_ops.rs | 13 +++++++++---- src/context/tab_context.rs | 8 ++++++-- src/key_command/command.rs | 1 + src/key_command/impl_appexecute.rs | 2 +- src/key_command/impl_from_str.rs | 14 +++++++++++++- src/run.rs | 2 +- 7 files changed, 34 insertions(+), 10 deletions(-) diff --git a/docs/configuration/keymap.toml.md b/docs/configuration/keymap.toml.md index 81069a1..55aba2e 100644 --- a/docs/configuration/keymap.toml.md +++ b/docs/configuration/keymap.toml.md @@ -237,7 +237,7 @@ All methods (except `reverse`) support the `--reverse` flag: ## Tabs -### `new_tab [--current][--cursor][dir]`: opens a new tab +### `new_tab [--last] [--current|--cursor|dir]`: opens a new tab - `new_tab`, without any argument, opens a new tab with the default directory. @@ -246,6 +246,8 @@ All methods (except `reverse`) support the `--reverse` flag: - `new_tab some-dir` opens new tab with directory `some-dir` - `new_tab --current` opens new tab with the same directory as the current tab - `new_tab --cursor` opens new tab with the directory which is currently marked by the cursor +- `new_tab --last` new tab will be placed at the end of the stack. + This is the only flag that can be combined with the others described above. ### `close_tab`: close current tab diff --git a/src/commands/tab_ops.rs b/src/commands/tab_ops.rs index e519267..752ea15 100644 --- a/src/commands/tab_ops.rs +++ b/src/commands/tab_ops.rs @@ -97,7 +97,7 @@ pub fn tab_switch_index(context: &mut AppContext, new_index: usize) -> AppResult _tab_switch(new_index - 1, context)?; } else if new_index > num_tabs { for _ in 0..(new_index - num_tabs) { - new_tab(context, &NewTabMode::Default)?; + new_tab(context, &NewTabMode::Default, true)?; } _tab_switch(new_index - 1, context)?; } @@ -115,7 +115,7 @@ pub fn new_tab_home_path(context: &AppContext) -> path::PathBuf { } } -pub fn new_tab(context: &mut AppContext, mode: &NewTabMode) -> AppResult { +pub fn new_tab(context: &mut AppContext, mode: &NewTabMode, last: bool) -> AppResult { let new_tab_path = match mode { NewTabMode::Default => Ok(new_tab_home_path(context)), NewTabMode::CurrentTabDir => { @@ -172,8 +172,13 @@ pub fn new_tab(context: &mut AppContext, mode: &NewTabMode) -> AppResult { .default_tab_display_option .clone(); let tab = JoshutoTab::new(new_tab_path, new_tab_history, tab_display_options)?; - context.tab_context_mut().insert_tab(id, tab); - let new_index = context.tab_context_ref().len() - 1; + context.tab_context_mut().insert_tab(id, tab, last); + let new_index = if last { + context.tab_context_ref().len() - 1 + } else { + context.tab_context_ref().index + 1 + }; + context.tab_context_mut().index = new_index; _tab_switch(new_index, context)?; Ok(()) diff --git a/src/context/tab_context.rs b/src/context/tab_context.rs index 35ee9ed..fc3a422 100644 --- a/src/context/tab_context.rs +++ b/src/context/tab_context.rs @@ -51,9 +51,13 @@ impl TabContext { let id = &self.tab_order[self.index]; self.tabs.get_mut(id).unwrap() } - pub fn insert_tab(&mut self, id: Uuid, tab: JoshutoTab) { + pub fn insert_tab(&mut self, id: Uuid, tab: JoshutoTab, last: bool) { self.tabs.insert(id, tab); - self.tab_order.push(id); + if last { + self.tab_order.push(id); + } else { + self.tab_order.insert(self.index + 1, id); + } } pub fn remove_tab(&mut self, id: &Uuid) -> Option { let tab = self.tabs.remove(id); diff --git a/src/key_command/command.rs b/src/key_command/command.rs index c0c575d..87ab74b 100644 --- a/src/key_command/command.rs +++ b/src/key_command/command.rs @@ -171,6 +171,7 @@ pub enum Command { NewTab { mode: NewTabMode, + last: bool, }, CloseTab, TabSwitch { diff --git a/src/key_command/impl_appexecute.rs b/src/key_command/impl_appexecute.rs index 3540cd1..5ab59b7 100644 --- a/src/key_command/impl_appexecute.rs +++ b/src/key_command/impl_appexecute.rs @@ -26,7 +26,7 @@ impl AppExecute for Command { Self::ParentDirectory => change_directory::parent_directory(context), Self::PreviousDirectory => change_directory::previous_directory(context), - Self::NewTab { mode } => tab_ops::new_tab(context, mode), + Self::NewTab { mode, last } => tab_ops::new_tab(context, mode, *last), Self::CloseTab => tab_ops::close_tab(context), Self::CommandLine { prefix, suffix } => command_line::read_and_execute( context, diff --git a/src/key_command/impl_from_str.rs b/src/key_command/impl_from_str.rs index d63407d..55a7fd7 100644 --- a/src/key_command/impl_from_str.rs +++ b/src/key_command/impl_from_str.rs @@ -124,8 +124,20 @@ impl std::str::FromStr for Command { _ => Ok(Self::Quit(QuitAction::Noop)), } } else if command == CMD_NEW_TAB { + let mut new_arg = arg.to_string(); + let mut last = false; + + for arg in arg.split_whitespace() { + if arg == "--last" { + new_arg = new_arg.split("--last").collect(); + last = true; + break; + } + } + Ok(Self::NewTab { - mode: NewTabMode::from_str(arg), + mode: NewTabMode::from_str(&new_arg), + last, }) } else if command == CMD_CHANGE_DIRECTORY { match arg { diff --git a/src/run.rs b/src/run.rs index c12a76d..843180b 100644 --- a/src/run.rs +++ b/src/run.rs @@ -52,7 +52,7 @@ pub fn run_loop( new_tab_history.insert_entries(dirlists); let tab = JoshutoTab::new(curr_path, new_tab_history, tab_display_options)?; - context.tab_context_mut().insert_tab(id, tab); + context.tab_context_mut().insert_tab(id, tab, true); // trigger a preview of child preview_default::load_preview(context, backend); -- cgit v1.2.3