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::path::PathBuf;
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::cmp::{Ord, Ordering};
|
use std::cmp::{Ord, Ordering};
|
||||||
|
use std::time::SystemTime;
|
||||||
|
|
||||||
use lscolors::{LsColors, Style};
|
use lscolors::{LsColors, Style};
|
||||||
|
|
||||||
@ -14,6 +15,7 @@ lazy_static! {
|
|||||||
pub struct Files {
|
pub struct Files {
|
||||||
pub files: Vec<File>,
|
pub files: Vec<File>,
|
||||||
pub sort: SortBy,
|
pub sort: SortBy,
|
||||||
|
pub dirs_first: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Index<usize> for Files {
|
impl Index<usize> for Files {
|
||||||
@ -45,17 +47,20 @@ impl Files {
|
|||||||
let path = file.path();
|
let path = file.path();
|
||||||
let meta = file.metadata()?;
|
let meta = file.metadata()?;
|
||||||
let size = meta.len() / 1024;
|
let size = meta.len() / 1024;
|
||||||
|
let mtime = meta.modified()?;
|
||||||
|
|
||||||
let style
|
let style
|
||||||
= match COLORS.style_for_path_with_metadata(file.path(), Some(&meta)) {
|
= match COLORS.style_for_path_with_metadata(file.path(), Some(&meta)) {
|
||||||
Some(style) => Some(style.clone()),
|
Some(style) => Some(style.clone()),
|
||||||
None => None
|
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)
|
files.push(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut files = Files { files: files,
|
let mut files = Files { files: files,
|
||||||
sort: SortBy::Name };
|
sort: SortBy::Name,
|
||||||
|
dirs_first: true };
|
||||||
|
|
||||||
files.sort();
|
files.sort();
|
||||||
Ok(files)
|
Ok(files)
|
||||||
@ -76,25 +81,31 @@ impl Files {
|
|||||||
a.size.cmp(&b.size).reverse()
|
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
|
if self.dirs_first {
|
||||||
self.files.sort_by(|a,b| {
|
self.files.sort_by(|a,b| {
|
||||||
if a.is_dir() && !b.is_dir() {
|
if a.is_dir() && !b.is_dir() {
|
||||||
Ordering::Less
|
Ordering::Less
|
||||||
} else { Ordering::Equal }
|
} else { Ordering::Equal }
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cycle_sort(&mut self) -> SortBy {
|
pub fn cycle_sort(&mut self) {
|
||||||
self.sort = match self.sort {
|
self.sort = match self.sort {
|
||||||
SortBy::Name => SortBy::Size,
|
SortBy::Name => SortBy::Size,
|
||||||
SortBy::Size => SortBy::Name,
|
SortBy::Size => SortBy::MTime,
|
||||||
_ => { SortBy::Name }
|
SortBy::MTime => SortBy::Name
|
||||||
};
|
};
|
||||||
self.sort();
|
|
||||||
self.sort
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn iter(&self) -> std::slice::Iter<File> {
|
pub fn iter(&self) -> std::slice::Iter<File> {
|
||||||
@ -120,8 +131,7 @@ impl std::fmt::Display for SortBy {
|
|||||||
let text = match self {
|
let text = match self {
|
||||||
SortBy::Name => "name",
|
SortBy::Name => "name",
|
||||||
SortBy::Size => "size",
|
SortBy::Size => "size",
|
||||||
SortBy::MDate => "mdate",
|
SortBy::MTime => "mtime"
|
||||||
SortBy::CDate => "cdate"
|
|
||||||
};
|
};
|
||||||
write!(formatter, "{}", text)
|
write!(formatter, "{}", text)
|
||||||
}
|
}
|
||||||
@ -131,8 +141,7 @@ impl std::fmt::Display for SortBy {
|
|||||||
pub enum SortBy {
|
pub enum SortBy {
|
||||||
Name,
|
Name,
|
||||||
Size,
|
Size,
|
||||||
MDate,
|
MTime,
|
||||||
CDate
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
@ -141,12 +150,11 @@ pub struct File {
|
|||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
pub size: Option<usize>,
|
pub size: Option<usize>,
|
||||||
pub kind: Kind,
|
pub kind: Kind,
|
||||||
pub style: Option<Style>
|
pub mtime: SystemTime,
|
||||||
|
pub style: Option<Style>,
|
||||||
// owner: Option<String>,
|
// owner: Option<String>,
|
||||||
// group: Option<String>,
|
// group: Option<String>,
|
||||||
// flags: Option<String>,
|
// flags: Option<String>,
|
||||||
// ctime: Option<u32>,
|
|
||||||
// mtime: Option<u32>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -155,18 +163,18 @@ impl File {
|
|||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
kind: Kind,
|
kind: Kind,
|
||||||
size: usize,
|
size: usize,
|
||||||
|
mtime: SystemTime,
|
||||||
style: Option<Style>) -> File {
|
style: Option<Style>) -> File {
|
||||||
File {
|
File {
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
path: path,
|
path: path,
|
||||||
size: Some(size),
|
size: Some(size),
|
||||||
kind: kind,
|
kind: kind,
|
||||||
|
mtime: mtime,
|
||||||
style: style
|
style: style
|
||||||
// owner: None,
|
// owner: None,
|
||||||
// group: None,
|
// group: None,
|
||||||
// flags: None,
|
// flags: None,
|
||||||
// ctime: None,
|
|
||||||
// mtime: None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn calculate_size(&self) -> (usize, String) {
|
pub fn calculate_size(&self) -> (usize, String) {
|
||||||
|
@ -156,10 +156,20 @@ impl ListView<Files> where
|
|||||||
fn cycle_sort(&mut self) {
|
fn cycle_sort(&mut self) {
|
||||||
let file = self.clone_selected_file();
|
let file = self.clone_selected_file();
|
||||||
self.content.cycle_sort();
|
self.content.cycle_sort();
|
||||||
|
self.content.sort();
|
||||||
self.select_file(&file);
|
self.select_file(&file);
|
||||||
self.refresh();
|
self.refresh();
|
||||||
self.show_status(&format!("Sorting by: {}", self.content.sort));
|
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()
|
self.goto_selected()
|
||||||
},
|
},
|
||||||
Key::Char('s') => { self.cycle_sort() } ,
|
Key::Char('s') => { self.cycle_sort() } ,
|
||||||
|
Key::Char('d') => self.toggle_dirs_first() ,
|
||||||
_ => { self.bad(Event::Key(key)); }
|
_ => { self.bad(Event::Key(key)); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user