Skip to content

Commit

Permalink
add optons.sort_ignore_case
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-ward committed Jan 1, 2025
1 parent 2e18efd commit 6fb7b1e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
4 changes: 2 additions & 2 deletions lsv/natural_compare.v
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import v.mathutil

// compares strings with embedded numbers (e.g. log17.txt)
fn natural_compare(a &string, b &string) int {
fn natural_compare(a &string, b &string, ignore_case bool) int {
pa := split(a)
pb := split(b)
max := mathutil.min(pa.len, pb.len)
Expand All @@ -13,7 +13,7 @@ fn natural_compare(a &string, b &string) int {
return result
}
} else {
result := compare_strings(pa[i], pb[i])
result := string_compare(pa[i], pb[i], ignore_case)
if result != 0 {
return result
}
Expand Down
35 changes: 19 additions & 16 deletions lsv/options.v
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@ struct Options {
no_wrap bool
//
// filter, group and sorting options
all bool
dirs_first bool
only_dirs bool
only_files bool
recursion_depth int
recursive bool
sort_ext bool
sort_natural bool
sort_none bool
sort_reverse bool
sort_size bool
sort_time bool
sort_width bool
all bool
dirs_first bool
only_dirs bool
only_files bool
recursion_depth int
recursive bool
sort_ext bool
sort_ignore_case bool
sort_natural bool
sort_none bool
sort_reverse bool
sort_size bool
sort_time bool
sort_width bool
//
// long view options
accessed_date bool
Expand Down Expand Up @@ -86,7 +87,7 @@ fn parse_args(args []string) Options {
colorize := fp.bool('', `c`, false, 'color the listing')
dir_indicator := fp.bool('', `D`, false, 'append / to directories')
icons := fp.bool('', `i`, false, 'show file icon (requires nerd fonts)')
long_format := fp.bool('', `l`, false, 'long listing format')
long_format := fp.bool('', `l`, false, 'long listing format (see Long Listing Options)')
with_commas := fp.bool('', `m`, false, 'list of files separated by commas')
quote := fp.bool('', `q`, false, 'enclose files in quotes')
recursive := fp.bool('', `R`, false, 'list subdirectories recursively')
Expand All @@ -104,11 +105,12 @@ fn parse_args(args []string) Options {
sort_natural := fp.bool('', `v`, false, 'sort digits within text as numbers')
sort_width := fp.bool('', `w`, false, 'sort by width, shortest first')
sort_ext := fp.bool('', `x`, false, 'sort by file extension')
sort_none := fp.bool('', `u`, false, 'no sorting\n\nLong Listing Options:')
sort_none := fp.bool('', `u`, false, 'no sorting\n')
sort_ignore_case := fp.bool('ignore-case', 0, false, 'ignore case when sorting\n\nLong Listing Options:')

size_comma := fp.bool('', `,`, false, 'show file sizes grouped and separated by thousands')
blocked_output := fp.bool('', `b`, false, 'blank line every 5 rows')
table_format := fp.bool('', `B`, false, 'add borders to long listing format')
size_comma := fp.bool('', `,`, false, 'sizes comma separated by thousands')
size_ki := fp.bool('', `k`, false, 'sizes in kibibytes (1024) (e.g. 1k 234m 2g)')
size_kb := fp.bool('', `K`, false, 'sizes in Kilobytes (1000) (e.g. 1kb 234mb 2gb)')
octal_permissions := fp.bool('', `o`, false, 'show octal permissions')
Expand Down Expand Up @@ -177,6 +179,7 @@ fn parse_args(args []string) Options {
size_kb: size_kb
size_ki: size_ki
sort_ext: sort_ext
sort_ignore_case: sort_ignore_case
sort_natural: sort_natural
sort_none: sort_none
sort_reverse: sort_reverse
Expand Down
41 changes: 28 additions & 13 deletions lsv/sort.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,57 @@ fn sort(entries []Entry, options Options) []Entry {
}
}
options.sort_size {
fn (a &Entry, b &Entry) int {
fn [options] (a &Entry, b &Entry) int {
return match true {
// vfmt off
a.size < b.size { 1 }
a.size > b.size { -1 }
else { compare_strings(a.name, b.name) }
else { string_compare(a.name, b.name, options.sort_ignore_case) }
// vfmt on
}
}
}
options.sort_time {
fn (a &Entry, b &Entry) int {
fn [options] (a &Entry, b &Entry) int {
return match true {
// vfmt off
a.stat.mtime < b.stat.mtime { 1 }
a.stat.mtime > b.stat.mtime { -1 }
else { compare_strings(a.name, b.name) }
else { string_compare(a.name, b.name, options.sort_ignore_case) }
// vfmt on
}
}
}
options.sort_width {
fn (a &Entry, b &Entry) int {
fn [options] (a &Entry, b &Entry) int {
a_len := a.name.len + a.link_origin.len + if a.link_origin.len > 0 { 4 } else { 0 }
b_len := b.name.len + b.link_origin.len + if b.link_origin.len > 0 { 4 } else { 0 }
result := a_len - b_len
return if result != 0 { result } else { compare_strings(a.name, b.name) }
return if result != 0 {
result
} else {
string_compare(a.name, b.name, options.sort_ignore_case)
}
}
}
options.sort_natural {
fn (a &Entry, b &Entry) int {
return natural_compare(a.name, b.name)
fn [options] (a &Entry, b &Entry) int {
return natural_compare(a.name, b.name, options.sort_ignore_case)
}
}
options.sort_ext {
fn (a &Entry, b &Entry) int {
result := compare_strings(os.file_ext(a.name), os.file_ext(b.name))
return if result != 0 { result } else { compare_strings(a.name, b.name) }
fn [options] (a &Entry, b &Entry) int {
result := string_compare(os.file_ext(a.name), os.file_ext(b.name), options.sort_ignore_case)
return if result != 0 {
result
} else {
string_compare(a.name, b.name, options.sort_ignore_case)
}
}
}
else {
fn (a &Entry, b &Entry) int {
return compare_strings(a.name, b.name)
fn [options] (a &Entry, b &Entry) int {
return string_compare(a.name, b.name, options.sort_ignore_case)
}
}
}
Expand All @@ -70,3 +78,10 @@ fn sort(entries []Entry, options Options) []Entry {

return if options.sort_reverse { sorted.reverse() } else { sorted }
}

fn string_compare(a &string, b &string, ignore_case bool) int {
return match ignore_case {
true { compare_strings(a.to_lower(), b.to_lower()) }
else { compare_strings(a, b) }
}
}

0 comments on commit 6fb7b1e

Please sign in to comment.