summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2023-03-05 08:31:31 +0100
committerYorhel <git@yorhel.nl>2023-03-05 08:31:31 +0100
commit46b88bcb5ca584ff2c976af40a182e8e048892be (patch)
treec705b8dd7046cc14da8a7d8e9dd92c1e11ab5254
parentca1f293310d5eaa00cf143f4c7e85965a4e76999 (diff)
Add --(enable|disable)-natsort options
-rw-r--r--ncdu.pod4
-rw-r--r--src/browser.zig8
-rw-r--r--src/main.zig3
3 files changed, 11 insertions, 4 deletions
diff --git a/ncdu.pod b/ncdu.pod
index b842e52..f1a00df 100644
--- a/ncdu.pod
+++ b/ncdu.pod
@@ -252,6 +252,10 @@ The column can be suffixed with I<-asc> or I<-desc> to set the order to
ascending or descending, respectively. e.g. C<--sort=name-desc> will sort by
name in descending order.
+=item B<--enable-natsort>, B<--disable-natsort>
+
+Enable (default) or disable natural sort when sorting by file name.
+
=item B<--group-directories-first>, B<--no-group-directories-first>
Sort (or not) directories before files.
diff --git a/src/browser.zig b/src/browser.zig
index 4e1f4da..65e48e8 100644
--- a/src/browser.zig
+++ b/src/browser.zig
@@ -103,10 +103,10 @@ fn sortLt(_: void, ap: ?*model.Entry, bp: ?*model.Entry) bool {
},
}
- const an = a.name();
- const bn = b.name();
- return if (main.config.sort_order == .asc) util.strnatcmp(an, bn) == .lt
- else util.strnatcmp(bn, an) == .lt;
+ const an = (if (main.config.sort_order == .asc) a else b).name();
+ const bn = (if (main.config.sort_order == .asc) b else a).name();
+ return if (main.config.sort_natural) util.strnatcmp(an, bn) == .lt
+ else std.mem.lessThan(u8, an, bn);
}
// Should be called when:
diff --git a/src/main.zig b/src/main.zig
index a00ffe9..2e253b4 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -66,6 +66,7 @@ pub const config = struct {
pub var sort_col: SortCol = .blocks;
pub var sort_order: SortOrder = .desc;
pub var sort_dirsfirst: bool = false;
+ pub var sort_natural: bool = true;
pub var imported: bool = false;
pub var can_delete: ?bool = null;
@@ -183,6 +184,8 @@ fn argConfig(args: *Args, opt: Args.Option) bool {
else if (opt.is("--hide-percent")) config.show_percent = false
else if (opt.is("--group-directories-first")) config.sort_dirsfirst = true
else if (opt.is("--no-group-directories-first")) config.sort_dirsfirst = false
+ else if (opt.is("--enable-natsort")) config.sort_natural = true
+ else if (opt.is("--disable-natsort")) config.sort_natural = false
else if (opt.is("--graph-style")) {
const val = args.arg();
if (std.mem.eql(u8, val, "hash")) config.graph_style = .hash