mirror of https://github.com/bobwen-dev/hunter
added natural sorting of file names
This commit is contained in:
parent
ab9baf17f1
commit
a1900941ce
|
@ -8,4 +8,5 @@ edition = "2018"
|
|||
termion = "*"
|
||||
unicode-width = "0.1.5"
|
||||
lazy_static = "*"
|
||||
x11-clipboard = "*"
|
||||
x11-clipboard = "*"
|
||||
alphanumeric-sort = "1.0.6"
|
50
src/files.rs
50
src/files.rs
|
@ -2,6 +2,7 @@ use std::ops::Index;
|
|||
use std::error::Error;
|
||||
use std::path::PathBuf;
|
||||
use std::ffi::OsStr;
|
||||
use std::cmp::{Ord, Ordering};
|
||||
|
||||
pub struct Files(Vec<File>);
|
||||
|
||||
|
@ -12,22 +13,53 @@ impl Index<usize> for Files {
|
|||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for File {
|
||||
fn partial_cmp(&self, other: &File) -> Option<Ordering> {
|
||||
Some(alphanumeric_sort::compare_str(&self.name, &other.name))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for File {
|
||||
fn cmp(&self, other: &File) -> Ordering {
|
||||
alphanumeric_sort::compare_str(&self.name, &other.name)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_kind(file: &std::fs::DirEntry) -> Kind {
|
||||
let file = file.file_type().unwrap();
|
||||
if file.is_file() { return Kind::File; }
|
||||
if file.is_dir() { return Kind::Directory; }
|
||||
if file.is_symlink() { return Kind::Link; }
|
||||
Kind::Pipe
|
||||
}
|
||||
|
||||
impl Files {
|
||||
pub fn new_from_path<S: AsRef<OsStr> + Sized>(path: S)
|
||||
-> Result<Files, Box<dyn Error>>
|
||||
where S: std::convert::AsRef<std::path::Path> {
|
||||
let mut files = Vec::new();
|
||||
let mut dirs = Vec::new();
|
||||
for file in std::fs::read_dir(path)? {
|
||||
let file = file?;
|
||||
let name = file.file_name();
|
||||
let name = name.to_string_lossy();
|
||||
let kind = get_kind(&file);
|
||||
let path = file.path();
|
||||
let size = file.metadata()?.len() / 1024;
|
||||
files.push(File::new(&name, path, size as usize));
|
||||
let file = File::new(&name, path, kind, size as usize);
|
||||
match kind {
|
||||
Kind::Directory => dirs.push(file),
|
||||
_ => files.push(file),
|
||||
}
|
||||
}
|
||||
files.sort();
|
||||
dirs.sort();
|
||||
dirs.append(&mut files);
|
||||
|
||||
let files = dirs;
|
||||
|
||||
Ok(Files(files))
|
||||
}
|
||||
|
||||
|
||||
pub fn iter(&self) -> std::slice::Iter<File> {
|
||||
self.0.iter()
|
||||
|
@ -38,11 +70,20 @@ impl Files {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub enum Kind {
|
||||
Directory,
|
||||
File,
|
||||
Link,
|
||||
Pipe
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct File {
|
||||
pub name: String,
|
||||
pub path: PathBuf,
|
||||
pub size: Option<usize>,
|
||||
pub kind: Kind,
|
||||
// owner: Option<String>,
|
||||
// group: Option<String>,
|
||||
// flags: Option<String>,
|
||||
|
@ -52,11 +93,12 @@ pub struct File {
|
|||
|
||||
|
||||
impl File {
|
||||
pub fn new(name: &str, path: PathBuf, size: usize) -> File {
|
||||
pub fn new(name: &str, path: PathBuf, kind: Kind, size: usize) -> File {
|
||||
File {
|
||||
name: name.to_string(),
|
||||
path: path,
|
||||
size: Some(size),
|
||||
kind: kind
|
||||
// owner: None,
|
||||
// group: None,
|
||||
// flags: None,
|
||||
|
|
|
@ -2,6 +2,7 @@ extern crate termion;
|
|||
extern crate unicode_width;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate alphanumeric_sort;
|
||||
|
||||
use std::io::{stdout, Write};
|
||||
|
||||
|
|
Loading…
Reference in New Issue