Skip to content

Commit

Permalink
feat: enable the ability to sort the which key (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhaysp95 authored Feb 12, 2024
1 parent 3213674 commit 84616bd
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 19 deletions.
5 changes: 5 additions & 0 deletions yazi-config/preset/yazi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,10 @@ open_title = "Open with:"
open_origin = "hovered"
open_offset = [ 0, 1, 50, 7 ]

[which]
sort_by = "none"
sort_sensitive = false
sort_reverse = false

[log]
enabled = false
7 changes: 6 additions & 1 deletion yazi-config/src/keymap/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use yazi_shared::event::Cmd;

use super::Key;

#[derive(Debug, Deserialize)]
#[derive(Debug, Default, Deserialize)]
pub struct Control {
pub on: Vec<Key>,
#[serde(deserialize_with = "super::exec_deserialize")]
Expand Down Expand Up @@ -43,6 +43,7 @@ impl Control {
}
}

#[derive(Debug)]
pub enum ControlCow {
Owned(Control),
Borrowed(&'static Control),
Expand All @@ -67,6 +68,10 @@ impl Deref for ControlCow {
}
}

impl Default for ControlCow {
fn default() -> Self { Self::Owned(Control::default()) }
}

impl ControlCow {
pub fn into_seq(self) -> VecDeque<Cmd> {
match self {
Expand Down
3 changes: 3 additions & 0 deletions yazi-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod priority;
mod tasks;
pub mod theme;
mod validation;
pub mod which;
mod xdg;

pub use layout::*;
Expand All @@ -43,6 +44,7 @@ pub static TASKS: RoCell<tasks::Tasks> = RoCell::new();
pub static THEME: RoCell<theme::Theme> = RoCell::new();
pub static INPUT: RoCell<popup::Input> = RoCell::new();
pub static SELECT: RoCell<popup::Select> = RoCell::new();
pub static WHICH: RoCell<which::Which> = RoCell::new();

pub fn init() {
ARGS.with(Default::default);
Expand All @@ -63,4 +65,5 @@ pub fn init() {
THEME.with(Default::default);
INPUT.with(Default::default);
SELECT.with(Default::default);
WHICH.with(Default::default);
}
5 changes: 5 additions & 0 deletions yazi-config/src/which/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod sorting;
mod which;

pub use sorting::*;
pub use which::*;
32 changes: 32 additions & 0 deletions yazi-config/src/which/sorting.rs
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) }
}
24 changes: 24 additions & 0 deletions yazi-config/src/which/which.rs
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
}
}
13 changes: 4 additions & 9 deletions yazi-core/src/folder/sorter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub struct FilesSorter {
}

impl FilesSorter {
pub(super) fn sort(&self, items: &mut Vec<File>, sizes: &BTreeMap<Url, u64>) -> bool {
pub(super) fn sort(&self, items: &mut Vec<File>, sizes: &BTreeMap<Url, u64>) {
if items.is_empty() {
return false;
return;
}

let by_alphabetical = |a: &File, b: &File| {
Expand All @@ -30,7 +30,7 @@ impl FilesSorter {
};

match self.by {
SortBy::None => return false,
SortBy::None => {}
SortBy::Modified => items.sort_unstable_by(|a, b| {
let ord = self.cmp(a.modified, b.modified, self.promote(a, b));
if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord }
Expand Down Expand Up @@ -60,7 +60,6 @@ impl FilesSorter {
if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord }
}),
}
true
}

fn sort_naturally(&self, items: &mut Vec<File>) {
Expand All @@ -81,11 +80,7 @@ impl FilesSorter {
if self.reverse { ordering.reverse() } else { ordering }
});

let mut new = Vec::with_capacity(indices.len());
for i in indices {
new.push(mem::take(&mut items[i]));
}
*items = new;
*items = indices.into_iter().map(|i| mem::take(&mut items[i])).collect();
}

#[inline(always)]
Expand Down
3 changes: 2 additions & 1 deletion yazi-core/src/which/commands/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str::FromStr;
use yazi_config::{keymap::{Control, Key}, KEYMAP};
use yazi_shared::{event::Cmd, render, Layer};

use crate::which::Which;
use crate::which::{Which, WhichSorter};

pub struct Opt {
cands: Vec<Control>,
Expand Down Expand Up @@ -52,6 +52,7 @@ impl Which {
.map(|c| c.into())
.collect();

WhichSorter::default().sort(&mut self.cands);
self.visible = true;
self.silent = false;
render!();
Expand Down
2 changes: 2 additions & 0 deletions yazi-core/src/which/mod.rs
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::*;
47 changes: 47 additions & 0 deletions yazi-core/src/which/sorter.rs
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();
}
}
17 changes: 9 additions & 8 deletions yazi-core/src/which/which.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@ pub struct Which {
pub times: usize,
pub cands: Vec<ControlCow>,

// Visibility
pub visible: bool,
pub silent: bool,
}

impl Which {
fn reset(&mut self) {
self.times = 0;
self.cands.clear();

self.visible = false;
self.silent = false;
}

pub fn type_(&mut self, key: Key) -> bool {
self.cands.retain(|c| c.on.len() > self.times && c.on[self.times] == key);
self.times += 1;
Expand All @@ -37,4 +30,12 @@ impl Which {
render!();
true
}

fn reset(&mut self) {
self.times = 0;
self.cands.clear();

self.visible = false;
self.silent = false;
}
}

0 comments on commit 84616bd

Please sign in to comment.