From c6eb52b457ee91547316625e2bf7d3466bda5e1d Mon Sep 17 00:00:00 2001 From: Anton Simonov Date: Wed, 23 Aug 2023 08:48:10 +0300 Subject: [PATCH] feat: prepare for natural sort --- config/docs/yazi.md | 1 + config/src/manager/sorting.rs | 2 ++ core/src/files/files.rs | 58 ++++++++++++++++++++++------------- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/config/docs/yazi.md b/config/docs/yazi.md index b375b8b3..49a2d3d8 100644 --- a/config/docs/yazi.md +++ b/config/docs/yazi.md @@ -11,6 +11,7 @@ - `"alphabetical"`: Sort alphabetically - `"created"`: Sort by creation time - `"modified"`: Sort by last modified time + - `"natural"`: Sort naturally - `"size"`: Sort by file size - sort_reverse: Display files in reverse order diff --git a/config/src/manager/sorting.rs b/config/src/manager/sorting.rs index 8c0010e5..6d0b565f 100644 --- a/config/src/manager/sorting.rs +++ b/config/src/manager/sorting.rs @@ -8,6 +8,7 @@ pub enum SortBy { Alphabetical, Created, Modified, + Natural, Size, } @@ -19,6 +20,7 @@ impl TryFrom for SortBy { "alphabetical" => Self::Alphabetical, "created" => Self::Created, "modified" => Self::Modified, + "natural" => Self::Natural, "size" => Self::Size, _ => bail!("invalid sort_by value: {s}"), }) diff --git a/core/src/files/files.rs b/core/src/files/files.rs index 8af98a90..b500de72 100644 --- a/core/src/files/files.rs +++ b/core/src/files/files.rs @@ -1,4 +1,9 @@ -use std::{cmp::Ordering, collections::BTreeMap, ops::{Deref, DerefMut}, path::{Path, PathBuf}}; +use std::{ + cmp::Ordering, + collections::BTreeMap, + ops::{Deref, DerefMut}, + path::{Path, PathBuf}, +}; use anyhow::Result; use config::{manager::SortBy, MANAGER}; @@ -8,18 +13,14 @@ use tokio::fs; use super::File; pub struct Files { - items: IndexMap, - pub sort: FilesSort, + items: IndexMap, + pub sort: FilesSort, pub show_hidden: bool, } impl Default for Files { fn default() -> Self { - Self { - items: Default::default(), - sort: Default::default(), - show_hidden: MANAGER.show_hidden, - } + Self { items: Default::default(), sort: Default::default(), show_hidden: MANAGER.show_hidden } } } @@ -122,13 +123,21 @@ impl Files { if promote != Ordering::Equal { promote } else { - if reverse { b.cmp(&a) } else { a.cmp(&b) } + if reverse { + b.cmp(&a) + } else { + a.cmp(&b) + } } } #[inline] fn promote(a: &File, b: &File, dir_first: bool) -> Ordering { - if dir_first { b.meta.is_dir().cmp(&a.meta.is_dir()) } else { Ordering::Equal } + if dir_first { + b.meta.is_dir().cmp(&a.meta.is_dir()) + } else { + Ordering::Equal + } } let reverse = self.sort.reverse; @@ -149,6 +158,9 @@ impl Files { } Ordering::Equal }), + SortBy::Natural => { + self.items.sort_by(|_, a, _, b| cmp(&a.path, &b.path, reverse, promote(a, b, dir_first))) + } SortBy::Size => self.items.sort_by(|_, a, _, b| { cmp(a.length.unwrap_or(0), b.length.unwrap_or(0), reverse, promote(a, b, dir_first)) }), @@ -160,27 +172,27 @@ impl Files { impl Deref for Files { type Target = IndexMap; - fn deref(&self) -> &Self::Target { &self.items } + fn deref(&self) -> &Self::Target { + &self.items + } } impl DerefMut for Files { - fn deref_mut(&mut self) -> &mut Self::Target { &mut self.items } + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.items + } } #[derive(PartialEq)] pub struct FilesSort { - pub by: SortBy, - pub reverse: bool, + pub by: SortBy, + pub reverse: bool, pub dir_first: bool, } impl Default for FilesSort { fn default() -> Self { - Self { - by: MANAGER.sort_by, - reverse: MANAGER.sort_reverse, - dir_first: MANAGER.sort_dir_first, - } + Self { by: MANAGER.sort_by, reverse: MANAGER.sort_reverse, dir_first: MANAGER.sort_dir_first } } } @@ -205,8 +217,12 @@ impl FilesOp { } #[inline] - pub fn read_empty(path: &Path) -> Self { Self::Read(path.to_path_buf(), BTreeMap::new()) } + pub fn read_empty(path: &Path) -> Self { + Self::Read(path.to_path_buf(), BTreeMap::new()) + } #[inline] - pub fn search_empty(path: &Path) -> Self { Self::Search(path.to_path_buf(), BTreeMap::new()) } + pub fn search_empty(path: &Path) -> Self { + Self::Search(path.to_path_buf(), BTreeMap::new()) + } }