Skip to content
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
5 changes: 4 additions & 1 deletion doc/lsd.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ lsd is a ls command with a lot of pretty colours and some other stuff to enrich
`-r`, `--reverse`
: Reverse the order of the sort

`--namesort`
: Sort by name

`-S`, `--sizesort`
: Sort by size

Expand Down Expand Up @@ -129,7 +132,7 @@ lsd is a ls command with a lot of pretty colours and some other stuff to enrich
: 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]
: Sort by WORD instead of the configured default (name, unless configured otherwise) [possible values: name, size, time, version, extension, git]

`-U`, `--no-sort`
: Do not sort. List entries in directory order
Expand Down
12 changes: 8 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ pub struct Cli {
#[arg(long, value_parser = validate_date_argument)]
pub date: Option<String>,

/// Sort by name
#[arg(long)]
pub namesort: bool,

/// Sort by time modified
#[arg(short = 't', long)]
pub timesort: bool,
Expand All @@ -104,17 +108,17 @@ pub struct Cli {
#[arg(short = 'v', long)]
pub versionsort: bool,

/// Sort by TYPE instead of name
/// Sort by TYPE instead of the configured default (name, unless configured otherwise)
#[arg(
long,
value_name = "TYPE",
value_parser = ["size", "time", "version", "extension", "git", "none"],
overrides_with_all = ["timesort", "sizesort", "extensionsort", "versionsort", "gitsort", "no_sort"]
value_parser = ["name", "size", "time", "version", "extension", "git", "none"],
overrides_with_all = ["namesort", "timesort", "sizesort", "extensionsort", "versionsort", "gitsort", "no_sort"]
)]
pub sort: Option<String>,

/// Do not sort. List entries in directory order
#[arg(short = 'U', long, overrides_with_all = ["timesort", "sizesort", "extensionsort", "versionsort", "gitsort", "sort"])]
#[arg(short = 'U', long, overrides_with_all = ["namesort", "timesort", "sizesort", "extensionsort", "versionsort", "gitsort", "sort"])]
pub no_sort: bool,

/// Reverse the order of the sort
Expand Down
15 changes: 14 additions & 1 deletion src/flags/sorting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ impl Configurable<Self> for SortColumn {
fn from_cli(cli: &Cli) -> Option<Self> {
let sort = cli.sort.as_deref();

if cli.timesort || sort == Some("time") {
if cli.namesort || sort == Some("name") {
Some(Self::Name)
} else if cli.timesort || sort == Some("time") {
Some(Self::Time)
} else if cli.sizesort || sort == Some("size") {
Some(Self::Size)
Expand Down Expand Up @@ -229,6 +231,13 @@ mod test_sort_column {
assert_eq!(Some(SortColumn::Version), SortColumn::from_cli(&cli));
}

#[test]
fn test_from_cli_name() {
let argv = ["lsd", "--namesort"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(Some(SortColumn::Name), SortColumn::from_cli(&cli));
}

#[test]
fn test_from_cli_no_sort() {
let argv = ["lsd", "--no-sort"];
Expand All @@ -238,6 +247,10 @@ mod test_sort_column {

#[test]
fn test_from_cli_sort() {
let argv = ["lsd", "--sort", "name"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(Some(SortColumn::Name), SortColumn::from_cli(&cli));

let argv = ["lsd", "--sort", "time"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(Some(SortColumn::Time), SortColumn::from_cli(&cli));
Expand Down