summaryrefslogtreecommitdiffstats
path: root/src/files.rs
diff options
context:
space:
mode:
authorrabite <rabite@posteo.de>2019-01-22 22:06:19 +0100
committerrabite <rabite@posteo.de>2019-01-22 22:06:19 +0100
commita1900941ce9a6277f9f0cecd685f8f1c1c6bb705 (patch)
treeb4406da2b2e33c2a40716f632b1e48435614ea82 /src/files.rs
parentab9baf17f110622391d143122195e2b82acc7d10 (diff)
added natural sorting of file names
Diffstat (limited to 'src/files.rs')
-rw-r--r--src/files.rs50
1 files changed, 46 insertions, 4 deletions
diff --git a/src/files.rs b/src/files.rs
index dd86304..d1422f5 100644
--- a/src/files.rs
+++ b/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,