Skip to content

Commit

Permalink
Fix sorting not working and apply the suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Feb 12, 2024
1 parent 8a9b727 commit f297640
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 31 deletions.
6 changes: 1 addition & 5 deletions yazi-core/src/folder/sorter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,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
4 changes: 2 additions & 2 deletions 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,7 +52,7 @@ impl Which {
.map(|c| c.into())
.collect();

self.sorter().sort(&mut self.cands);
WhichSorter::default().sort(&mut self.cands);
self.visible = true;
self.silent = false;
render!();
Expand Down
20 changes: 13 additions & 7 deletions yazi-core/src/which/sorter.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
use std::{borrow::Cow, mem};

use yazi_config::{keymap::ControlCow, which::SortBy};
use yazi_config::{keymap::ControlCow, which::SortBy, WHICH};
use yazi_shared::natsort;

#[derive(Clone, Copy, Default, PartialEq)]
#[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() {
Expand All @@ -32,10 +42,6 @@ impl WhichSorter {
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();
}
}
19 changes: 2 additions & 17 deletions yazi-core/src/which/which.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
use yazi_config::{keymap::{ControlCow, Key}, which::SortBy};
use yazi_config::keymap::{ControlCow, Key};
use yazi_shared::{emit, render, Layer};

use super::WhichSorter;

#[derive(Default, Debug)]
#[derive(Default)]
pub struct Which {
pub(super) layer: Layer,
pub times: usize,
pub cands: Vec<ControlCow>,

// Sorting
sort_by: SortBy,
sort_sensitive: bool,
sort_reverse: bool,

// Visibility
pub visible: bool,
pub silent: bool,
Expand All @@ -38,14 +31,6 @@ impl Which {
true
}

pub(super) fn sorter(&self) -> WhichSorter {
WhichSorter {
by: self.sort_by,
sensitive: self.sort_sensitive,
reverse: self.sort_reverse,
}
}

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

0 comments on commit f297640

Please sign in to comment.