Skip to content

Commit e700506

Browse files
committed
made sorting by file name the defualt
1 parent a7d631e commit e700506

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "natls"
3-
version = "1.2.10"
3+
version = "1.2.11"
44
authors = ["Will Lane <[email protected]>"]
55
description = "nat - the 'ls' replacement you never knew you needed"
66
license = "MIT"

src/main.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ pub struct Cli {
6565
/// Specify time format https://docs.rs/chrono/*/chrono/format/strftime/index.html
6666
#[structopt(long = "time-format", default_value = "%b %e %T")]
6767
time_format: String,
68+
69+
/// Turns off sorting by name (on by default)
70+
#[structopt(long="no-name")]
71+
by_name: bool,
72+
73+
/// Sorts by files by date modified
74+
#[structopt(short="m")]
75+
by_modified: bool,
6876
}
6977

7078
fn output() -> Result<(), Box<dyn std::error::Error>> {
@@ -81,6 +89,8 @@ fn output() -> Result<(), Box<dyn std::error::Error>> {
8189
let time_format = &args.time_format;
8290
let colors_on = &args.colors_on;
8391
let headline_on = &args.headline_on;
92+
let by_name = &args.by_name;
93+
let by_modified = &args.by_modified;
8494

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

@@ -89,7 +99,12 @@ fn output() -> Result<(), Box<dyn std::error::Error>> {
8999
let mut entries = fs::read_dir(".")?
90100
.map(|res| res.map(|e| e.path()))
91101
.collect::<Result<Vec<_>, io::Error>>()?;
92-
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()));
102+
if *by_modified {
103+
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()));
104+
}
105+
if !*by_name {
106+
entries.sort_by(|a, b| a.file_name().unwrap().to_str().unwrap().to_lowercase().cmp(&b.file_name().unwrap().to_str().unwrap().to_lowercase()));
107+
}
93108

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

137-
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()));
138-
152+
if *by_modified {
153+
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()));
154+
}
155+
if !*by_name {
156+
entries.sort_by(|a, b| a.file_name().unwrap().to_str().unwrap().to_lowercase().cmp(&b.file_name().unwrap().to_str().unwrap().to_lowercase()));
157+
}
139158
let mut size_count = 4;
140159
let mut group_size = 8;
141160
for s in &entries {

0 commit comments

Comments
 (0)