mirror of
https://github.com/bobwen-dev/hunter
synced 2025-04-12 00:55:41 +02:00
more sorting, directory first toggle
This commit is contained in:
parent
c381203bb2
commit
878d7bdbfb
54
src/files.rs
54
src/files.rs
@ -3,6 +3,7 @@ use std::error::Error;
|
||||
use std::path::PathBuf;
|
||||
use std::ffi::OsStr;
|
||||
use std::cmp::{Ord, Ordering};
|
||||
use std::time::SystemTime;
|
||||
|
||||
use lscolors::{LsColors, Style};
|
||||
|
||||
@ -14,6 +15,7 @@ lazy_static! {
|
||||
pub struct Files {
|
||||
pub files: Vec<File>,
|
||||
pub sort: SortBy,
|
||||
pub dirs_first: bool,
|
||||
}
|
||||
|
||||
impl Index<usize> for Files {
|
||||
@ -45,17 +47,20 @@ impl Files {
|
||||
let path = file.path();
|
||||
let meta = file.metadata()?;
|
||||
let size = meta.len() / 1024;
|
||||
let mtime = meta.modified()?;
|
||||
|
||||
let style
|
||||
= match COLORS.style_for_path_with_metadata(file.path(), Some(&meta)) {
|
||||
Some(style) => Some(style.clone()),
|
||||
None => None
|
||||
};
|
||||
let file = File::new(&name, path, kind, size as usize, style);
|
||||
let file = File::new(&name, path, kind, size as usize, mtime, style);
|
||||
files.push(file)
|
||||
}
|
||||
|
||||
let mut files = Files { files: files,
|
||||
sort: SortBy::Name };
|
||||
sort: SortBy::Name,
|
||||
dirs_first: true };
|
||||
|
||||
files.sort();
|
||||
Ok(files)
|
||||
@ -76,25 +81,31 @@ impl Files {
|
||||
a.size.cmp(&b.size).reverse()
|
||||
});
|
||||
},
|
||||
_ => {}
|
||||
SortBy::MTime => {
|
||||
self.files.sort_by(|a,b| {
|
||||
if a.mtime == b.mtime {
|
||||
return alphanumeric_sort::compare_str(&a.name, &b.name)
|
||||
}
|
||||
a.mtime.cmp(&b.mtime)
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Direcories first
|
||||
self.files.sort_by(|a,b| {
|
||||
if a.is_dir() && !b.is_dir() {
|
||||
Ordering::Less
|
||||
} else { Ordering::Equal }
|
||||
});
|
||||
if self.dirs_first {
|
||||
self.files.sort_by(|a,b| {
|
||||
if a.is_dir() && !b.is_dir() {
|
||||
Ordering::Less
|
||||
} else { Ordering::Equal }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cycle_sort(&mut self) -> SortBy {
|
||||
pub fn cycle_sort(&mut self) {
|
||||
self.sort = match self.sort {
|
||||
SortBy::Name => SortBy::Size,
|
||||
SortBy::Size => SortBy::Name,
|
||||
_ => { SortBy::Name }
|
||||
SortBy::Size => SortBy::MTime,
|
||||
SortBy::MTime => SortBy::Name
|
||||
};
|
||||
self.sort();
|
||||
self.sort
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> std::slice::Iter<File> {
|
||||
@ -120,8 +131,7 @@ impl std::fmt::Display for SortBy {
|
||||
let text = match self {
|
||||
SortBy::Name => "name",
|
||||
SortBy::Size => "size",
|
||||
SortBy::MDate => "mdate",
|
||||
SortBy::CDate => "cdate"
|
||||
SortBy::MTime => "mtime"
|
||||
};
|
||||
write!(formatter, "{}", text)
|
||||
}
|
||||
@ -131,8 +141,7 @@ impl std::fmt::Display for SortBy {
|
||||
pub enum SortBy {
|
||||
Name,
|
||||
Size,
|
||||
MDate,
|
||||
CDate
|
||||
MTime,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
@ -141,12 +150,11 @@ pub struct File {
|
||||
pub path: PathBuf,
|
||||
pub size: Option<usize>,
|
||||
pub kind: Kind,
|
||||
pub style: Option<Style>
|
||||
pub mtime: SystemTime,
|
||||
pub style: Option<Style>,
|
||||
// owner: Option<String>,
|
||||
// group: Option<String>,
|
||||
// flags: Option<String>,
|
||||
// ctime: Option<u32>,
|
||||
// mtime: Option<u32>,
|
||||
}
|
||||
|
||||
|
||||
@ -155,18 +163,18 @@ impl File {
|
||||
path: PathBuf,
|
||||
kind: Kind,
|
||||
size: usize,
|
||||
mtime: SystemTime,
|
||||
style: Option<Style>) -> File {
|
||||
File {
|
||||
name: name.to_string(),
|
||||
path: path,
|
||||
size: Some(size),
|
||||
kind: kind,
|
||||
mtime: mtime,
|
||||
style: style
|
||||
// owner: None,
|
||||
// group: None,
|
||||
// flags: None,
|
||||
// ctime: None,
|
||||
// mtime: None
|
||||
}
|
||||
}
|
||||
pub fn calculate_size(&self) -> (usize, String) {
|
||||
|
@ -156,10 +156,20 @@ impl ListView<Files> where
|
||||
fn cycle_sort(&mut self) {
|
||||
let file = self.clone_selected_file();
|
||||
self.content.cycle_sort();
|
||||
self.content.sort();
|
||||
self.select_file(&file);
|
||||
self.refresh();
|
||||
self.show_status(&format!("Sorting by: {}", self.content.sort));
|
||||
}
|
||||
|
||||
fn toggle_dirs_first(&mut self) {
|
||||
let file = self.clone_selected_file();
|
||||
self.content.dirs_first = !self.content.dirs_first;
|
||||
self.content.sort();
|
||||
self.select_file(&file);
|
||||
self.refresh();
|
||||
self.show_status(&format!("Direcories first: {}", self.content.dirs_first));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -237,6 +247,7 @@ impl Widget for ListView<Files> {
|
||||
self.goto_selected()
|
||||
},
|
||||
Key::Char('s') => { self.cycle_sort() } ,
|
||||
Key::Char('d') => self.toggle_dirs_first() ,
|
||||
_ => { self.bad(Event::Key(key)); }
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user