Fix sorting not working and apply the suggested changes

This commit is contained in:
sxyazi 2024-02-12 21:58:23 +08:00
parent 8a9b7279f8
commit f2976400f6
No known key found for this signature in database
4 changed files with 18 additions and 31 deletions

View file

@ -80,11 +80,7 @@ impl FilesSorter {
if self.reverse { ordering.reverse() } else { ordering } if self.reverse { ordering.reverse() } else { ordering }
}); });
let mut new = Vec::with_capacity(indices.len()); *items = indices.into_iter().map(|i| mem::take(&mut items[i])).collect();
for i in indices {
new.push(mem::take(&mut items[i]));
}
*items = new;
} }
#[inline(always)] #[inline(always)]

View file

@ -3,7 +3,7 @@ use std::str::FromStr;
use yazi_config::{keymap::{Control, Key}, KEYMAP}; use yazi_config::{keymap::{Control, Key}, KEYMAP};
use yazi_shared::{event::Cmd, render, Layer}; use yazi_shared::{event::Cmd, render, Layer};
use crate::which::Which; use crate::which::{Which, WhichSorter};
pub struct Opt { pub struct Opt {
cands: Vec<Control>, cands: Vec<Control>,
@ -52,7 +52,7 @@ impl Which {
.map(|c| c.into()) .map(|c| c.into())
.collect(); .collect();
self.sorter().sort(&mut self.cands); WhichSorter::default().sort(&mut self.cands);
self.visible = true; self.visible = true;
self.silent = false; self.silent = false;
render!(); render!();

View file

@ -1,15 +1,25 @@
use std::{borrow::Cow, mem}; use std::{borrow::Cow, mem};
use yazi_config::{keymap::ControlCow, which::SortBy}; use yazi_config::{keymap::ControlCow, which::SortBy, WHICH};
use yazi_shared::natsort; use yazi_shared::natsort;
#[derive(Clone, Copy, Default, PartialEq)] #[derive(Clone, Copy, PartialEq)]
pub struct WhichSorter { pub struct WhichSorter {
pub by: SortBy, pub by: SortBy,
pub sensitive: bool, pub sensitive: bool,
pub reverse: 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 { impl WhichSorter {
pub(super) fn sort(&self, items: &mut Vec<ControlCow>) { pub(super) fn sort(&self, items: &mut Vec<ControlCow>) {
if self.by == SortBy::None || items.is_empty() { if self.by == SortBy::None || items.is_empty() {
@ -32,10 +42,6 @@ impl WhichSorter {
if self.reverse { ordering.reverse() } else { ordering } if self.reverse { ordering.reverse() } else { ordering }
}); });
let mut new = Vec::with_capacity(indices.len()); *items = indices.into_iter().map(|i| mem::take(&mut items[i])).collect();
for i in indices {
new.push(mem::take(&mut items[i]));
}
*items = new;
} }
} }

View file

@ -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 yazi_shared::{emit, render, Layer};
use super::WhichSorter; #[derive(Default)]
#[derive(Default, Debug)]
pub struct Which { pub struct Which {
pub(super) layer: Layer, pub(super) layer: Layer,
pub times: usize, pub times: usize,
pub cands: Vec<ControlCow>, pub cands: Vec<ControlCow>,
// Sorting
sort_by: SortBy,
sort_sensitive: bool,
sort_reverse: bool,
// Visibility // Visibility
pub visible: bool, pub visible: bool,
pub silent: bool, pub silent: bool,
@ -38,14 +31,6 @@ impl Which {
true true
} }
pub(super) fn sorter(&self) -> WhichSorter {
WhichSorter {
by: self.sort_by,
sensitive: self.sort_sensitive,
reverse: self.sort_reverse,
}
}
fn reset(&mut self) { fn reset(&mut self) {
self.times = 0; self.times = 0;
self.cands.clear(); self.cands.clear();