-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable the ability to sort the which key (#662)
- Loading branch information
Showing
11 changed files
with
139 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod sorting; | ||
mod which; | ||
|
||
pub use sorting::*; | ||
pub use which::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use std::str::FromStr; | ||
|
||
use anyhow::bail; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)] | ||
#[serde(try_from = "String")] | ||
pub enum SortBy { | ||
#[default] | ||
None, | ||
Key, | ||
Desc, | ||
} | ||
|
||
impl FromStr for SortBy { | ||
type Err = anyhow::Error; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
Ok(match s { | ||
"none" => Self::None, | ||
"key" => Self::Key, | ||
"desc" => Self::Desc, | ||
_ => bail!("Invalid sort option: {s}"), | ||
}) | ||
} | ||
} | ||
|
||
impl TryFrom<String> for SortBy { | ||
type Error = anyhow::Error; | ||
|
||
fn try_from(value: String) -> Result<Self, Self::Error> { Self::from_str(&value) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use validator::Validate; | ||
|
||
use super::SortBy; | ||
use crate::MERGED_YAZI; | ||
|
||
#[derive(Debug, Deserialize, Serialize, Validate)] | ||
pub struct Which { | ||
// Sorting | ||
pub sort_by: SortBy, | ||
pub sort_sensitive: bool, | ||
pub sort_reverse: bool, | ||
} | ||
|
||
impl Default for Which { | ||
fn default() -> Self { | ||
#[derive(Deserialize)] | ||
struct Outer { | ||
which: Which, | ||
} | ||
|
||
toml::from_str::<Outer>(&MERGED_YAZI).unwrap().which | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
mod commands; | ||
mod sorter; | ||
mod which; | ||
|
||
pub use sorter::*; | ||
pub use which::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::{borrow::Cow, mem}; | ||
|
||
use yazi_config::{keymap::ControlCow, which::SortBy, WHICH}; | ||
use yazi_shared::natsort; | ||
|
||
#[derive(Clone, Copy, PartialEq)] | ||
pub struct WhichSorter { | ||
pub by: SortBy, | ||
pub sensitive: bool, | ||
pub reverse: bool, | ||
} | ||
|
||
impl Default for WhichSorter { | ||
fn default() -> Self { | ||
Self { | ||
by: WHICH.sort_by, | ||
sensitive: WHICH.sort_sensitive, | ||
reverse: WHICH.sort_reverse, | ||
} | ||
} | ||
} | ||
|
||
impl WhichSorter { | ||
pub(super) fn sort(&self, items: &mut Vec<ControlCow>) { | ||
if self.by == SortBy::None || items.is_empty() { | ||
return; | ||
} | ||
|
||
let mut indices = Vec::with_capacity(items.len()); | ||
let mut entities = Vec::with_capacity(items.len()); | ||
for (i, ctrl) in items.iter().enumerate() { | ||
indices.push(i); | ||
entities.push(match self.by { | ||
SortBy::None => unreachable!(), | ||
SortBy::Key => Cow::Owned(ctrl.on()), | ||
SortBy::Desc => ctrl.desc_or_exec(), | ||
}); | ||
} | ||
|
||
indices.sort_unstable_by(|&a, &b| { | ||
let ordering = natsort(entities[a].as_bytes(), entities[b].as_bytes(), !self.sensitive); | ||
if self.reverse { ordering.reverse() } else { ordering } | ||
}); | ||
|
||
*items = indices.into_iter().map(|i| mem::take(&mut items[i])).collect(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters