diff --git a/src/app.rs b/src/app.rs index b2e3b84..a255037 100644 --- a/src/app.rs +++ b/src/app.rs @@ -140,6 +140,13 @@ pub fn build() -> App<'static, 'static> { .multiple(true) .help("Sort by time modified"), ) + .arg( + Arg::with_name("sizesort") + .short("S") + .long("sizesort") + .multiple(true) + .help("Sort by size"), + ) .arg( Arg::with_name("reverse") .short("r") diff --git a/src/core.rs b/src/core.rs index 8f50fdd..70b8efd 100644 --- a/src/core.rs +++ b/src/core.rs @@ -59,7 +59,6 @@ impl Core { let mut meta_list = self.fetch(paths); self.sort(&mut meta_list); - self.display(meta_list) } diff --git a/src/flags.rs b/src/flags.rs index 14fc87e..9670ae8 100644 --- a/src/flags.rs +++ b/src/flags.rs @@ -39,6 +39,8 @@ impl Flags { let sort_by = if matches.is_present("timesort") { SortFlag::Time + } else if matches.is_present("sizesort") { + SortFlag::Size } else { SortFlag::Name }; @@ -193,6 +195,7 @@ impl<'a> From<&'a str> for WhenFlag { pub enum SortFlag { Name, Time, + Size, } #[derive(Clone, Debug, Copy, PartialEq, Eq)] diff --git a/src/meta/size.rs b/src/meta/size.rs index afa30f7..ce31306 100644 --- a/src/meta/size.rs +++ b/src/meta/size.rs @@ -16,49 +16,50 @@ pub enum Unit { pub struct Size { value: u64, unit: Unit, + bytes: u64, } impl<'a> From<&'a Metadata> for Size { fn from(meta: &Metadata) -> Self { let len = meta.len(); - - if meta.is_file() { - Self::from_bytes(len) - } else { - Self { - value: 0, - unit: Unit::None, - } - } + Self::from_bytes(len) } } impl Size { + pub fn get_bytes(&self) -> u64 { + self.bytes + } fn from_bytes(len: u64) -> Self { if len < 1024 { Self { value: len * 1024, unit: Unit::Byte, + bytes: len, } } else if len < 1024 * 1024 { Self { value: len, unit: Unit::Kilo, + bytes: len, } } else if len < 1024 * 1024 * 1024 { Self { value: len / 1024, unit: Unit::Mega, + bytes: len, } } else if len < 1024 * 1024 * 1024 * 1024 { Self { value: len / (1024 * 1024), unit: Unit::Giga, + bytes: len, } } else { Self { value: len / (1024 * 1024 * 1024), unit: Unit::Tera, + bytes: len, } } } diff --git a/src/sort.rs b/src/sort.rs index 53292ca..b9531c3 100644 --- a/src/sort.rs +++ b/src/sort.rs @@ -9,7 +9,11 @@ pub fn by_meta(a: &Meta, b: &Meta, flags: Flags) -> Ordering { DirOrderFlag::None => by_name(a, b, flags), DirOrderFlag::Last => by_name_with_files_first(a, b, flags), }, - + SortFlag::Size => match flags.directory_order { + DirOrderFlag::First => by_size(a, b, flags), + DirOrderFlag::None => by_size(a, b, flags), + DirOrderFlag::Last => by_size(a, b, flags), + }, SortFlag::Time => match flags.directory_order { DirOrderFlag::First => by_date_with_dirs_first(a, b, flags), DirOrderFlag::None => by_date(a, b, flags), @@ -18,6 +22,17 @@ pub fn by_meta(a: &Meta, b: &Meta, flags: Flags) -> Ordering { } } +fn by_size(a: &Meta, b: &Meta, flags: Flags) -> Ordering { + + if flags.sort_order == SortOrder::Default { + b.size.get_bytes().cmp(&a.size.get_bytes()) + } else { + a.size.get_bytes().cmp(&b.size.get_bytes()) + } +} + + + fn by_name(a: &Meta, b: &Meta, flags: Flags) -> Ordering { if flags.sort_order == SortOrder::Default { a.name.cmp(&b.name)