Skip to content

Commit

Permalink
made sorting by file name the defualt
Browse files Browse the repository at this point in the history
  • Loading branch information
willdoescode committed Nov 12, 2020
1 parent a7d631e commit e700506
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "natls"
version = "1.2.10"
version = "1.2.11"
authors = ["Will Lane <[email protected]>"]
description = "nat - the 'ls' replacement you never knew you needed"
license = "MIT"
Expand Down
25 changes: 22 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ pub struct Cli {
/// Specify time format https://docs.rs/chrono/*/chrono/format/strftime/index.html
#[structopt(long = "time-format", default_value = "%b %e %T")]
time_format: String,

/// Turns off sorting by name (on by default)
#[structopt(long="no-name")]
by_name: bool,

/// Sorts by files by date modified
#[structopt(short="m")]
by_modified: bool,
}

fn output() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -81,6 +89,8 @@ fn output() -> Result<(), Box<dyn std::error::Error>> {
let time_format = &args.time_format;
let colors_on = &args.colors_on;
let headline_on = &args.headline_on;
let by_name = &args.by_name;
let by_modified = &args.by_modified;

draw_headlines(*headline_on, *perms_on, *size_on, *time_on, *group_on, *user_on);

Expand All @@ -89,7 +99,12 @@ fn output() -> Result<(), Box<dyn std::error::Error>> {
let mut entries = fs::read_dir(".")?
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, io::Error>>()?;
entries.sort_by(|a, b| FileTime::from_last_modification_time(&fs::symlink_metadata(&a).unwrap()).seconds().cmp(&FileTime::from_last_modification_time(&fs::symlink_metadata(&b).unwrap()).seconds()));
if *by_modified {
entries.sort_by(|a, b| FileTime::from_last_modification_time(&fs::symlink_metadata(&a).unwrap()).seconds().cmp(&FileTime::from_last_modification_time(&fs::symlink_metadata(&b).unwrap()).seconds()));
}
if !*by_name {
entries.sort_by(|a, b| a.file_name().unwrap().to_str().unwrap().to_lowercase().cmp(&b.file_name().unwrap().to_str().unwrap().to_lowercase()));
}

let mut size_count = 4;
for s in &entries {
Expand Down Expand Up @@ -134,8 +149,12 @@ fn output() -> Result<(), Box<dyn std::error::Error>> {
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, io::Error>>()?;

entries.sort_by(|a, b| FileTime::from_last_modification_time(&fs::symlink_metadata(&a).unwrap()).seconds().cmp(&FileTime::from_last_modification_time(&fs::symlink_metadata(&b).unwrap()).seconds()));

if *by_modified {
entries.sort_by(|a, b| FileTime::from_last_modification_time(&fs::symlink_metadata(&a).unwrap()).seconds().cmp(&FileTime::from_last_modification_time(&fs::symlink_metadata(&b).unwrap()).seconds()));
}
if !*by_name {
entries.sort_by(|a, b| a.file_name().unwrap().to_str().unwrap().to_lowercase().cmp(&b.file_name().unwrap().to_str().unwrap().to_lowercase()));
}
let mut size_count = 4;
let mut group_size = 8;
for s in &entries {
Expand Down

0 comments on commit e700506

Please sign in to comment.