Skip to content

Commit

Permalink
feat: add --reverse flag to sort methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Akmadan23 committed Mar 13, 2024
1 parent 59b6ae5 commit cdcbc86
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 13 deletions.
6 changes: 6 additions & 0 deletions docs/configuration/keymap.toml.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) <sup>✻</sup>
Expand Down
7 changes: 6 additions & 1 deletion src/commands/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>) -> 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)
}

Expand Down
5 changes: 4 additions & 1 deletion src/key_command/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ pub enum Command {
initial: char,
},

Sort(SortType),
Sort {
sort_type: SortType,
reverse: Option<bool>,
},
SortReverse,

FilterGlob {
Expand Down
2 changes: 1 addition & 1 deletion src/key_command/impl_appcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/key_command/impl_appexecute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 } => {
Expand Down
2 changes: 1 addition & 1 deletion src/key_command/impl_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 11 additions & 1 deletion src/key_command/impl_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
28 changes: 21 additions & 7 deletions src/key_command/impl_from_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?))
Expand Down

0 comments on commit cdcbc86

Please sign in to comment.