This commit is contained in:
sxyazi 2023-08-28 19:13:39 +08:00
parent 0d11c52e4d
commit 46e7c4d102
No known key found for this signature in database
2 changed files with 18 additions and 5 deletions

View file

@ -15,12 +15,12 @@ pub struct File {
impl File { impl File {
#[inline] #[inline]
pub async fn from(path: &Path) -> Result<File> { pub async fn from(path: &Path) -> Result<Self> {
let meta = fs::metadata(path).await?; let meta = fs::metadata(path).await?;
Ok(Self::from_meta(path, meta).await) Ok(Self::from_meta(path, meta).await)
} }
pub async fn from_meta(path: &Path, mut meta: Metadata) -> File { pub async fn from_meta(path: &Path, mut meta: Metadata) -> Self {
let is_link = meta.is_symlink(); let is_link = meta.is_symlink();
let mut link_to = None; let mut link_to = None;
@ -31,7 +31,7 @@ impl File {
let length = if meta.is_dir() { None } else { Some(meta.len()) }; let length = if meta.is_dir() { None } else { Some(meta.len()) };
let is_hidden = path.file_name().map(|s| s.to_string_lossy().starts_with('.')).unwrap_or(false); let is_hidden = path.file_name().map(|s| s.to_string_lossy().starts_with('.')).unwrap_or(false);
File { path: path.to_path_buf(), meta, length, link_to, is_link, is_hidden } Self { path: path.to_path_buf(), meta, length, link_to, is_link, is_hidden }
} }
} }

View file

@ -1,4 +1,4 @@
use std::cmp::Ordering; use std::{cmp::Ordering, mem, path::PathBuf};
use config::{manager::SortBy, MANAGER}; use config::{manager::SortBy, MANAGER};
@ -70,7 +70,20 @@ impl FilesSorter {
} }
}); });
items.sort_unstable_by_key(|_| indices.pop().unwrap()); let dummy = File {
path: PathBuf::new(),
meta: items[0].meta.clone(),
length: None,
link_to: None,
is_link: false,
is_hidden: false,
};
let mut new = Vec::with_capacity(indices.len());
for i in indices {
new.push(mem::replace(&mut items[i], dummy.clone()));
}
*items = new;
} }
#[inline] #[inline]