From 45cbcaf95fda7779a3d09bc5ba8201babedb9a20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Thu, 23 Mar 2023 16:01:10 +0800 Subject: [PATCH] =?UTF-8?q?enhance(list):=E6=96=87=E4=BB=B6=E5=A4=B9?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E4=B8=8E=E6=96=87=E4=BB=B6=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E6=94=B9=E7=94=A8=E8=87=AA=E7=84=B6=E6=95=B0=E5=AD=97=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/commands/files.rs | 41 +++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src-tauri/src/commands/files.rs b/src-tauri/src/commands/files.rs index f222879..24532b8 100644 --- a/src-tauri/src/commands/files.rs +++ b/src-tauri/src/commands/files.rs @@ -4,12 +4,21 @@ use std::{ }; use mountpoints::mountinfos; +use regex::Regex; use serde::Serialize; use tauri::Runtime; use crate::utils; -#[derive(Debug, Clone, Serialize)] +fn compute_all_digits(text: &str) -> usize { + let re = Regex::new(r#"\d+"#).unwrap(); + re.find_iter(&["a", text, "0"].join(" ")) + .map(|b| b.as_str()) + .map(|b| usize::from_str_radix(b, 10).unwrap_or(0)) + .sum::() +} + +#[derive(Debug, Clone, PartialEq, Serialize)] pub struct FileItem { pub id: String, pub filename: String, @@ -18,7 +27,19 @@ pub struct FileItem { pub width: u32, } -#[derive(Debug, Clone, Serialize)] +impl PartialOrd for FileItem { + fn partial_cmp(&self, other: &Self) -> Option { + let this_digit = compute_all_digits(&self.filename); + let other_digit = compute_all_digits(&other.filename); + if this_digit == other_digit { + self.filename.partial_cmp(&other.filename) + } else { + this_digit.partial_cmp(&other_digit) + } + } +} + +#[derive(Debug, Clone, PartialEq, Serialize)] pub struct DirItem { pub id: String, pub dirname: String, @@ -26,6 +47,18 @@ pub struct DirItem { pub root: bool, } +impl PartialOrd for DirItem { + fn partial_cmp(&self, other: &Self) -> Option { + let this_digit = compute_all_digits(&self.dirname); + let other_digit = compute_all_digits(&other.dirname); + if this_digit == other_digit { + self.dirname.partial_cmp(&other.dirname) + } else { + this_digit.partial_cmp(&other_digit) + } + } +} + fn is_hidden(entry: &DirEntry) -> bool { entry .file_name() @@ -92,7 +125,7 @@ pub async fn scan_directory(target: String) -> Result, String> { width, }); } - file_items.sort_by(|a, b| a.filename.partial_cmp(&b.filename).unwrap()); + file_items.sort_by(|a, b| a.partial_cmp(&b).unwrap()); Ok(file_items) } @@ -193,7 +226,7 @@ pub async fn scan_for_child_dirs( root: false, }); } - child_dirs.sort_by(|a, b| a.dirname.partial_cmp(&b.dirname).unwrap()); + child_dirs.sort_by(|a, b| a.partial_cmp(&b).unwrap()); Ok(child_dirs) }