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)
|
||||
.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")
|
||||
|
|
|
@ -59,7 +59,6 @@ impl Core {
|
|||
let mut meta_list = self.fetch(paths);
|
||||
|
||||
self.sort(&mut meta_list);
|
||||
|
||||
self.display(meta_list)
|
||||
}
|
||||
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
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::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)
|
||||
|
|
Loading…
Reference in a new issue