Skip to content

Allow sorting files based on access, modification, or creation times #1110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions doc/lsd.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,37 +96,40 @@ lsd is a ls command with a lot of pretty colours and some other stuff to enrich
: Specify the blocks that will be displayed and in what order [possible values: permission, user, group, size, date, name, inode, git]

`--color <color>...`
: When to use terminal colours [default: auto] [possible values: always, auto, never]
: When to use terminal colours [default: auto] [possible values: always, auto, never]

`--date <date>...`
: How to display date [possible values: date, locale, relative, +date-time-format] [default: date]

`--depth <num>...`
: Stop recursing into directories after reaching specified depth

`--ftime <time>...`
: Which file timestamp to display [possible values: mtime (modification time), btime (birth time), atime (access time)] [default: mtime]

`--group-dirs <group-dirs>...`
: Sort the directories then the files [default: none] [possible values: none, first, last]
: Sort the directories then the files [default: none] [possible values: none, first, last]

`--group-directories-first`
: Groups the directories at the top before the files. Same as `--group-dirs=first`

`--hyperlink <hyperlink>...`
: Attach hyperlink to filenames [default: never] [possible values: always, auto, never]
: Attach hyperlink to filenames [default: never] [possible values: always, auto, never]

`--icon <icon>...`
: When to print the icons [default: auto] [possible values: always, auto, never]
: When to print the icons [default: auto] [possible values: always, auto, never]

`--icon-theme <icon-theme>...`
: Whether to use fancy or unicode icons [default: fancy] [possible values: fancy, unicode]
: Whether to use fancy or unicode icons [default: fancy] [possible values: fancy, unicode]

`-I, --ignore-glob <pattern>...`
: Do not display files/directories with names matching the glob pattern(s). More than one can be specified by repeating the argument [default: ]

`--permission <permission>...`
: How to display permissions [default: rwx for linux, attributes for windows] [possible values: rwx, octal, attributes, disable]
: How to display permissions [default: rwx for linux, attributes for windows] [possible values: rwx, octal, attributes, disable]

`--size <size>...`
: How to display size [default: default] [possible values: default, short, bytes]
: How to display size [default: default] [possible values: default, short, bytes]

`--sort <WORD>...`
: Sort by WORD instead of name [possible values: size, time, version, extension, git]
Expand Down
12 changes: 12 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ pub struct Cli {
#[arg(short = 'N', long)]
pub literal: bool,

/// Displayed file time [default: mtime] [possible values: mtime (modification time), btime (birth time), atime (access time)]
#[arg(long, value_parser = validate_ftime_argument)]
pub ftime: Option<String>,

/// Print help information
#[arg(long, action = ArgAction::Help)]
help: (),
Expand All @@ -208,6 +212,14 @@ fn validate_date_argument(arg: &str) -> Result<String, String> {
}
}

fn validate_ftime_argument(arg: &str) -> Result<String, String> {
if arg == "mtime" || arg == "atime" || arg == "btime" {
Result::Ok(arg.to_owned())
} else {
Result::Err("possible values: mtime, btime, atime".to_owned())
}
}

pub fn validate_time_format(formatter: &str) -> Result<String, String> {
let mut chars = formatter.chars();
loop {
Expand Down
3 changes: 3 additions & 0 deletions src/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct Config {
pub header: Option<bool>,
pub literal: Option<bool>,
pub truncate_owner: Option<TruncateOwner>,
pub ftime: Option<String>,
}

#[derive(Eq, PartialEq, Debug, Deserialize)]
Expand Down Expand Up @@ -129,6 +130,7 @@ impl Config {
header: None,
literal: None,
truncate_owner: None,
ftime: None,
}
}

Expand Down Expand Up @@ -428,6 +430,7 @@ mod tests {
after: None,
marker: Some("".to_string()),
}),
ftime: None,
},
c
);
Expand Down
17 changes: 8 additions & 9 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,14 @@ impl Core {
.collect();

for path in paths {
let mut meta =
match Meta::from_path(&path, self.flags.dereference.0, self.flags.permission) {
Ok(meta) => meta,
Err(err) => {
print_error!("{}: {}.", path.display(), err);
exit_code.set_if_greater(ExitCode::MajorIssue);
continue;
}
};
let mut meta = match Meta::from_path(&path, self.flags.dereference.0, &self.flags) {
Ok(meta) => meta,
Err(err) => {
print_error!("{}: {}.", path.display(), err);
exit_code.set_if_greater(ExitCode::MajorIssue);
continue;
}
};

let cache = if self.flags.blocks.0.contains(&Block::GitStatus) {
Some(GitCache::new(&path))
Expand Down
30 changes: 18 additions & 12 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ mod tests {
use crate::app::Cli;
use crate::color;
use crate::color::Colors;
use crate::flags::{HyperlinkOption, IconOption, IconTheme as FlagTheme, PermissionFlag};
use crate::flags::{HyperlinkOption, IconOption, IconTheme as FlagTheme};
use crate::icon::Icons;
use crate::meta::{FileType, Name};
use crate::Config;
Expand Down Expand Up @@ -699,7 +699,7 @@ mod tests {
dir.child("one.d").create_dir_all().unwrap();
dir.child("one.d/two").touch().unwrap();
dir.child("one.d/.hidden").touch().unwrap();
let mut metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
let mut metas = Meta::from_path(Path::new(dir.path()), false, &flags)
.unwrap()
.recurse_into(42, &flags, None)
.unwrap()
Expand Down Expand Up @@ -732,7 +732,7 @@ mod tests {
let dir = assert_fs::TempDir::new().unwrap();
dir.child("dir").create_dir_all().unwrap();
dir.child("dir/file").touch().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
let metas = Meta::from_path(Path::new(dir.path()), false, &flags)
.unwrap()
.recurse_into(42, &flags, None)
.unwrap()
Expand Down Expand Up @@ -773,7 +773,7 @@ mod tests {
let dir = assert_fs::TempDir::new().unwrap();
dir.child("dir").create_dir_all().unwrap();
dir.child("dir/file").touch().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
let metas = Meta::from_path(Path::new(dir.path()), false, &flags)
.unwrap()
.recurse_into(42, &flags, None)
.unwrap()
Expand Down Expand Up @@ -813,7 +813,7 @@ mod tests {
let dir = assert_fs::TempDir::new().unwrap();
dir.child("one.d").create_dir_all().unwrap();
dir.child("one.d/two").touch().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
let metas = Meta::from_path(Path::new(dir.path()), false, &flags)
.unwrap()
.recurse_into(42, &flags, None)
.unwrap()
Expand Down Expand Up @@ -844,7 +844,7 @@ mod tests {
let dir = assert_fs::TempDir::new().unwrap();
dir.child("testdir").create_dir_all().unwrap();
dir.child("test").touch().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
let metas = Meta::from_path(Path::new(dir.path()), false, &flags)
.unwrap()
.recurse_into(1, &flags, None)
.unwrap()
Expand Down Expand Up @@ -878,7 +878,7 @@ mod tests {

let dir = assert_fs::TempDir::new().unwrap();
dir.child("testdir").create_dir_all().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
let metas = Meta::from_path(Path::new(dir.path()), false, &flags)
.unwrap()
.recurse_into(1, &flags, None)
.unwrap()
Expand Down Expand Up @@ -908,11 +908,14 @@ mod tests {

let file_path = tmp_dir.path().join("file");
std::fs::File::create(&file_path).expect("failed to create the file");
let file = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();

let flags = Flags::default();

let file = Meta::from_path(&file_path, false, &flags).unwrap();

let dir_path = tmp_dir.path().join("dir");
std::fs::create_dir(&dir_path).expect("failed to create the dir");
let dir = Meta::from_path(&dir_path, false, PermissionFlag::Rwx).unwrap();
let dir = Meta::from_path(&dir_path, false, &flags).unwrap();

assert_eq!(
display_folder_path(&dir),
Expand Down Expand Up @@ -955,15 +958,18 @@ mod tests {

let file_path = tmp_dir.path().join("file");
std::fs::File::create(&file_path).expect("failed to create the file");
let file = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();

let flags = Flags::default();

let file = Meta::from_path(&file_path, false, &flags).unwrap();

let dir_path = tmp_dir.path().join("dir");
std::fs::create_dir(&dir_path).expect("failed to create the dir");
let dir = Meta::from_path(&dir_path, false, PermissionFlag::Rwx).unwrap();
let dir = Meta::from_path(&dir_path, false, &flags).unwrap();

let link_path = tmp_dir.path().join("link");
std::os::unix::fs::symlink("dir", &link_path).unwrap();
let link = Meta::from_path(&link_path, false, PermissionFlag::Rwx).unwrap();
let link = Meta::from_path(&link_path, false, &flags).unwrap();

const YES: bool = true;
const NO: bool = false;
Expand Down
4 changes: 4 additions & 0 deletions src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod color;
pub mod date;
pub mod dereference;
pub mod display;
pub mod ftime;
pub mod header;
pub mod hyperlink;
pub mod icons;
Expand All @@ -25,6 +26,7 @@ pub use color::{ColorOption, ThemeOption};
pub use date::DateFlag;
pub use dereference::Dereference;
pub use display::Display;
pub use ftime::FileTimeFlag;
pub use header::Header;
pub use hyperlink::HyperlinkOption;
pub use icons::IconOption;
Expand Down Expand Up @@ -77,6 +79,7 @@ pub struct Flags {
pub header: Header,
pub literal: Literal,
pub truncate_owner: TruncateOwner,
pub ftime: FileTimeFlag,
}

impl Flags {
Expand Down Expand Up @@ -108,6 +111,7 @@ impl Flags {
header: Header::configure_from(cli, config),
literal: Literal::configure_from(cli, config),
truncate_owner: TruncateOwner::configure_from(cli, config),
ftime: FileTimeFlag::configure_from(cli, config),
})
}
}
Expand Down
Loading