From 2536838ce31955ec55561aabb4b86fdd4bc984df Mon Sep 17 00:00:00 2001 From: Azad <49314270+Akmadan23@users.noreply.github.com> Date: Thu, 14 Mar 2024 17:05:42 +0100 Subject: feat: add `--reverse` flag to sort methods (#507) --- docs/configuration/keymap.toml.md | 6 ++++++ src/commands/sort.rs | 7 ++++++- src/key_command/command.rs | 5 ++++- src/key_command/impl_appcommand.rs | 2 +- src/key_command/impl_appexecute.rs | 2 +- src/key_command/impl_comment.rs | 2 +- src/key_command/impl_display.rs | 12 +++++++++++- src/key_command/impl_from_str.rs | 28 +++++++++++++++++++++------- 8 files changed, 51 insertions(+), 13 deletions(-) diff --git a/docs/configuration/keymap.toml.md b/docs/configuration/keymap.toml.md index d44660f..81069a1 100644 --- a/docs/configuration/keymap.toml.md +++ b/docs/configuration/keymap.toml.md @@ -152,8 +152,14 @@ function joshuto() { - `sort lexical`: sort lexically (`10.txt` comes before `2.txt`) - `sort natural`: sort naturally (`2.txt` comes before `10.txt`) - `sort mtime`: sort via last modified time +- `sort size`: sort by file size +- `sort ext`: sort by extension - `sort reverse`: reverse the sorting +All methods (except `reverse`) support the `--reverse` flag: +- `--reverse=true` applies sort method and sets reverse to `true` +- `--reverse=false` applies sort method and sets reverse to `false` + ### `linemode`: change the line-mode (textual representation of files and directories in the “current view”) - `linemode size`: show the entry’s size (bytes for files, number of entries for directories) (default) diff --git a/src/commands/sort.rs b/src/commands/sort.rs index 7bea48f..fa597c1 100644 --- a/src/commands/sort.rs +++ b/src/commands/sort.rs @@ -5,13 +5,18 @@ use crate::history::DirectoryHistory; use super::reload; -pub fn set_sort(context: &mut AppContext, method: SortType) -> AppResult { +pub fn set_sort(context: &mut AppContext, method: SortType, reverse: Option) -> AppResult { let curr_tab = context.tab_context_mut().curr_tab_mut(); curr_tab .option_mut() .sort_options_mut() .set_sort_method(method); curr_tab.history_mut().depreciate_all_entries(); + + if let Some(r) = reverse { + curr_tab.option_mut().sort_options_mut().reverse = r; + } + refresh(context) } diff --git a/src/key_command/command.rs b/src/key_command/command.rs index 0a4736f..c0c575d 100644 --- a/src/key_command/command.rs +++ b/src/key_command/command.rs @@ -153,7 +153,10 @@ pub enum Command { initial: char, }, - Sort(SortType), + Sort { + sort_type: SortType, + reverse: Option, + }, SortReverse, FilterGlob { diff --git a/src/key_command/impl_appcommand.rs b/src/key_command/impl_appcommand.rs index e261391..df9ffcf 100644 --- a/src/key_command/impl_appcommand.rs +++ b/src/key_command/impl_appcommand.rs @@ -79,7 +79,7 @@ impl AppCommand for Command { Self::Flat { .. } => CMD_FLAT, Self::NumberedCommand { .. } => CMD_NUMBERED_COMMAND, - Self::Sort(_) => CMD_SORT, + Self::Sort { .. } => CMD_SORT, Self::SortReverse => CMD_SORT_REVERSE, Self::FilterGlob { .. } => CMD_FILTER_GLOB, diff --git a/src/key_command/impl_appexecute.rs b/src/key_command/impl_appexecute.rs index 10ab5e9..3540cd1 100644 --- a/src/key_command/impl_appexecute.rs +++ b/src/key_command/impl_appexecute.rs @@ -135,7 +135,7 @@ impl AppExecute for Command { } => case_sensitivity::set_case_sensitivity(context, *case_sensitivity, *set_type), Self::SetMode => set_mode::set_mode(context, backend), Self::ShowTasks => show_tasks::show_tasks(context, backend, keymap_t), - Self::Sort(t) => sort::set_sort(context, *t), + Self::Sort { sort_type, reverse } => sort::set_sort(context, *sort_type, *reverse), Self::SetLineMode(mode) => linemode::set_linemode(context, *mode), Self::SortReverse => sort::toggle_reverse(context), Self::SubProcess { words, mode } => { diff --git a/src/key_command/impl_comment.rs b/src/key_command/impl_comment.rs index bcfd3c9..6fa744d 100644 --- a/src/key_command/impl_comment.rs +++ b/src/key_command/impl_comment.rs @@ -119,7 +119,7 @@ impl CommandComment for Command { Self::Flat { .. } => "Flattern directory list", Self::NumberedCommand { .. } => "Jump via input number", - Self::Sort(sort_type) => match sort_type { + Self::Sort { sort_type, .. } => match sort_type { SortType::Lexical => "Sort lexically", SortType::Mtime => "Sort by modification time", SortType::Natural => "Sort naturally", diff --git a/src/key_command/impl_display.rs b/src/key_command/impl_display.rs index 50252ed..b219a9a 100644 --- a/src/key_command/impl_display.rs +++ b/src/key_command/impl_display.rs @@ -49,7 +49,17 @@ impl std::fmt::Display for Command { Self::SearchRegex { pattern } => write!(f, "{} {}", self.command(), pattern), Self::SearchString { pattern } => write!(f, "{} {}", self.command(), pattern), Self::SubProcess { words, .. } => write!(f, "{} {:?}", self.command(), words), - Self::Sort(t) => write!(f, "{} {}", self.command(), t), + Self::Sort { sort_type, reverse } => write!( + f, + "{} {}{}", + self.command(), + sort_type, + match reverse { + Some(true) => " --reverse=true", + Some(false) => " --reverse=false", + None => "", + }, + ), Self::TabSwitch { offset } => write!(f, "{} {}", self.command(), offset), Self::TabSwitchIndex { index } => write!(f, "{} {}", self.command(), index), _ => write!(f, "{}", self.command()), diff --git a/src/key_command/impl_from_str.rs b/src/key_command/impl_from_str.rs index 79a7164..d63407d 100644 --- a/src/key_command/impl_from_str.rs +++ b/src/key_command/impl_from_str.rs @@ -510,13 +510,27 @@ impl std::str::FromStr for Command { } else if command == CMD_SORT { match arg { "reverse" => Ok(Self::SortReverse), - arg => match SortType::from_str(arg) { - Some(s) => Ok(Self::Sort(s)), - None => Err(AppError::new( - AppErrorKind::InvalidParameters, - format!("{}: Unknown option '{}'", command, arg), - )), - }, + arg => { + let (sort, reverse) = match arg.split_once(' ') { + Some((s, "--reverse=true")) => (s, Some(true)), + Some((s, "--reverse=false")) => (s, Some(false)), + Some((_, opt)) => { + return Err(AppError::new( + AppErrorKind::InvalidParameters, + format!("{}: Unknown option '{}'", command, opt), + )) + } + None => (arg, None), + }; + + match SortType::from_str(sort) { + Some(sort_type) => Ok(Self::Sort { sort_type, reverse }), + None => Err(AppError::new( + AppErrorKind::InvalidParameters, + format!("{}: Unknown option '{}'", command, sort), + )), + } + } } } else if command == CMD_SET_LINEMODE { Ok(Self::SetLineMode(LineMode::from_string(arg)?)) -- cgit v1.2.3