Sort files in file picker by access, modification and creation date (#336)

* Sort files in file picker by access date

* Fallback file time to modified then created then UNIX_EPOCH

* Use `sort_by_key`

* Refactor
imgbot
Wojciech Kępka 4 years ago committed by GitHub
parent d534d6470f
commit eb6fb63e74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -76,11 +76,22 @@ pub fn regex_prompt(
pub fn file_picker(root: PathBuf) -> Picker<PathBuf> { pub fn file_picker(root: PathBuf) -> Picker<PathBuf> {
use ignore::Walk; use ignore::Walk;
use std::time;
let files = Walk::new(root.clone()).filter_map(|entry| match entry { let files = Walk::new(root.clone()).filter_map(|entry| match entry {
Ok(entry) => { Ok(entry) => {
// filter dirs, but we might need special handling for symlinks! // filter dirs, but we might need special handling for symlinks!
if !entry.file_type().map_or(false, |entry| entry.is_dir()) { if !entry.file_type().map_or(false, |entry| entry.is_dir()) {
Some(entry.into_path()) let time = if let Ok(metadata) = entry.metadata() {
metadata
.accessed()
.or_else(|_| metadata.modified())
.or_else(|_| metadata.created())
.unwrap_or(time::UNIX_EPOCH)
} else {
time::UNIX_EPOCH
};
Some((entry.into_path(), time))
} else { } else {
None None
} }
@ -88,13 +99,17 @@ pub fn file_picker(root: PathBuf) -> Picker<PathBuf> {
Err(_err) => None, Err(_err) => None,
}); });
let files = if root.join(".git").is_dir() { let mut files: Vec<_> = if root.join(".git").is_dir() {
files.collect() files.collect()
} else { } else {
const MAX: usize = 8192; const MAX: usize = 8192;
files.take(MAX).collect() files.take(MAX).collect()
}; };
files.sort_by_key(|file| file.1);
let files = files.into_iter().map(|(path, _)| path).collect();
Picker::new( Picker::new(
files, files,
move |path: &PathBuf| { move |path: &PathBuf| {

Loading…
Cancel
Save