Skip to content

Commit

Permalink
Sort files in file picker by access, modification and creation date (#…
Browse files Browse the repository at this point in the history
…336)

* Sort files in file picker by access date

* Fallback file time to modified then created then UNIX_EPOCH

* Use `sort_by_key`

* Refactor
  • Loading branch information
vv9k committed Jun 26, 2021
1 parent d534d64 commit eb6fb63
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,40 @@ pub fn regex_prompt(

pub fn file_picker(root: PathBuf) -> Picker<PathBuf> {
use ignore::Walk;
use std::time;
let files = Walk::new(root.clone()).filter_map(|entry| match entry {
Ok(entry) => {
// filter dirs, but we might need special handling for symlinks!
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 {
None
}
}
Err(_err) => None,
});

let files = if root.join(".git").is_dir() {
let mut files: Vec<_> = if root.join(".git").is_dir() {
files.collect()
} else {
const MAX: usize = 8192;
files.take(MAX).collect()
};

files.sort_by_key(|file| file.1);

let files = files.into_iter().map(|(path, _)| path).collect();

Picker::new(
files,
move |path: &PathBuf| {
Expand Down

0 comments on commit eb6fb63

Please sign in to comment.