mirror of
https://github.com/lsd-rs/lsd
synced 2024-12-14 22:22:26 +00:00
* add --sizesort/-S and sort by filesize
* modify display/fetch to also show folder size (they have sizes)
This commit is contained in:
parent
89c5ca2a3a
commit
265a18bb21
5 changed files with 36 additions and 11 deletions
|
@ -140,6 +140,13 @@ pub fn build() -> App<'static, 'static> {
|
||||||
.multiple(true)
|
.multiple(true)
|
||||||
.help("Sort by time modified"),
|
.help("Sort by time modified"),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("sizesort")
|
||||||
|
.short("S")
|
||||||
|
.long("sizesort")
|
||||||
|
.multiple(true)
|
||||||
|
.help("Sort by size"),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("reverse")
|
Arg::with_name("reverse")
|
||||||
.short("r")
|
.short("r")
|
||||||
|
|
|
@ -59,7 +59,6 @@ impl Core {
|
||||||
let mut meta_list = self.fetch(paths);
|
let mut meta_list = self.fetch(paths);
|
||||||
|
|
||||||
self.sort(&mut meta_list);
|
self.sort(&mut meta_list);
|
||||||
|
|
||||||
self.display(meta_list)
|
self.display(meta_list)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,6 +39,8 @@ impl Flags {
|
||||||
|
|
||||||
let sort_by = if matches.is_present("timesort") {
|
let sort_by = if matches.is_present("timesort") {
|
||||||
SortFlag::Time
|
SortFlag::Time
|
||||||
|
} else if matches.is_present("sizesort") {
|
||||||
|
SortFlag::Size
|
||||||
} else {
|
} else {
|
||||||
SortFlag::Name
|
SortFlag::Name
|
||||||
};
|
};
|
||||||
|
@ -193,6 +195,7 @@ impl<'a> From<&'a str> for WhenFlag {
|
||||||
pub enum SortFlag {
|
pub enum SortFlag {
|
||||||
Name,
|
Name,
|
||||||
Time,
|
Time,
|
||||||
|
Size,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
|
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
|
||||||
|
|
|
@ -16,49 +16,50 @@ pub enum Unit {
|
||||||
pub struct Size {
|
pub struct Size {
|
||||||
value: u64,
|
value: u64,
|
||||||
unit: Unit,
|
unit: Unit,
|
||||||
|
bytes: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a Metadata> for Size {
|
impl<'a> From<&'a Metadata> for Size {
|
||||||
fn from(meta: &Metadata) -> Self {
|
fn from(meta: &Metadata) -> Self {
|
||||||
let len = meta.len();
|
let len = meta.len();
|
||||||
|
Self::from_bytes(len)
|
||||||
if meta.is_file() {
|
|
||||||
Self::from_bytes(len)
|
|
||||||
} else {
|
|
||||||
Self {
|
|
||||||
value: 0,
|
|
||||||
unit: Unit::None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Size {
|
impl Size {
|
||||||
|
pub fn get_bytes(&self) -> u64 {
|
||||||
|
self.bytes
|
||||||
|
}
|
||||||
fn from_bytes(len: u64) -> Self {
|
fn from_bytes(len: u64) -> Self {
|
||||||
if len < 1024 {
|
if len < 1024 {
|
||||||
Self {
|
Self {
|
||||||
value: len * 1024,
|
value: len * 1024,
|
||||||
unit: Unit::Byte,
|
unit: Unit::Byte,
|
||||||
|
bytes: len,
|
||||||
}
|
}
|
||||||
} else if len < 1024 * 1024 {
|
} else if len < 1024 * 1024 {
|
||||||
Self {
|
Self {
|
||||||
value: len,
|
value: len,
|
||||||
unit: Unit::Kilo,
|
unit: Unit::Kilo,
|
||||||
|
bytes: len,
|
||||||
}
|
}
|
||||||
} else if len < 1024 * 1024 * 1024 {
|
} else if len < 1024 * 1024 * 1024 {
|
||||||
Self {
|
Self {
|
||||||
value: len / 1024,
|
value: len / 1024,
|
||||||
unit: Unit::Mega,
|
unit: Unit::Mega,
|
||||||
|
bytes: len,
|
||||||
}
|
}
|
||||||
} else if len < 1024 * 1024 * 1024 * 1024 {
|
} else if len < 1024 * 1024 * 1024 * 1024 {
|
||||||
Self {
|
Self {
|
||||||
value: len / (1024 * 1024),
|
value: len / (1024 * 1024),
|
||||||
unit: Unit::Giga,
|
unit: Unit::Giga,
|
||||||
|
bytes: len,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Self {
|
Self {
|
||||||
value: len / (1024 * 1024 * 1024),
|
value: len / (1024 * 1024 * 1024),
|
||||||
unit: Unit::Tera,
|
unit: Unit::Tera,
|
||||||
|
bytes: len,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
17
src/sort.rs
17
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::None => by_name(a, b, flags),
|
||||||
DirOrderFlag::Last => by_name_with_files_first(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 {
|
SortFlag::Time => match flags.directory_order {
|
||||||
DirOrderFlag::First => by_date_with_dirs_first(a, b, flags),
|
DirOrderFlag::First => by_date_with_dirs_first(a, b, flags),
|
||||||
DirOrderFlag::None => by_date(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 {
|
fn by_name(a: &Meta, b: &Meta, flags: Flags) -> Ordering {
|
||||||
if flags.sort_order == SortOrder::Default {
|
if flags.sort_order == SortOrder::Default {
|
||||||
a.name.cmp(&b.name)
|
a.name.cmp(&b.name)
|
||||||
|
|
Loading…
Reference in a new issue