diff --git a/go.mod b/go.mod index 7743623a..0d759c7f 100644 --- a/go.mod +++ b/go.mod @@ -8,11 +8,11 @@ require ( github.com/mattn/go-runewidth v0.0.15 golang.org/x/sys v0.20.0 golang.org/x/term v0.20.0 - golang.org/x/text v0.14.0 ) require ( github.com/gdamore/encoding v1.0.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/rivo/uniseg v0.4.3 // indirect + golang.org/x/text v0.14.0 // indirect ) diff --git a/nav.go b/nav.go index 69e11fc7..296fb8f2 100644 --- a/nav.go +++ b/nav.go @@ -16,8 +16,6 @@ import ( "time" "github.com/djherbis/times" - "golang.org/x/text/collate" - "golang.org/x/text/language" ) type linkState byte @@ -249,24 +247,35 @@ func (dir *dir) sort() { dir.files = filtered } - collopts := []collate.Option{} - if dir.ignorecase { - collopts = append(collopts, collate.IgnoreCase) + normalizefun := func(s1, s2 string) (string, string) { + return s1, s2 } - if dir.ignoredia { - collopts = append(collopts, collate.IgnoreDiacritics) - } - if dir.sortby == naturalSort { - collopts = append(collopts, collate.Numeric) + if dir.ignorecase && dir.ignoredia { + normalizefun = func(s1, s2 string) (string, string) { + return removeDiacritics(strings.ToLower(s1)), removeDiacritics(strings.ToLower(s2)) + } + } else if dir.ignorecase { + normalizefun = func(s1, s2 string) (string, string) { + return strings.ToLower(s1), strings.ToLower(s2) + } + } else if dir.ignoredia { + normalizefun = func(s1, s2 string) (string, string) { + return removeDiacritics(s1), removeDiacritics(s2) + } } - coll := collate.New(language.Und, collopts...) var lessfun func(i, j int) bool switch dir.sortby { - case nameSort, naturalSort: + case naturalSort: lessfun = func(i, j int) bool { - return coll.CompareString(dir.files[i].Name(), dir.files[j].Name()) == -1 + s1, s2 := normalizefun(dir.files[i].Name(), dir.files[j].Name()) + return naturalLess(s1, s2) + } + case nameSort: + lessfun = func(i, j int) bool { + s1, s2 := normalizefun(dir.files[i].Name(), dir.files[j].Name()) + return s1 < s2 } case sizeSort: lessfun = func(i, j int) bool { @@ -286,8 +295,16 @@ func (dir *dir) sort() { } case extSort: lessfun = func(i, j int) bool { - cmp := coll.CompareString(dir.files[i].ext, dir.files[j].ext) - return cmp == -1 || cmp == 0 && coll.CompareString(dir.files[i].Name(), dir.files[j].Name()) == -1 + ext1, ext2 := normalizefun(dir.files[i].ext, dir.files[j].ext) + extcmp := strings.Compare(ext1, ext2) + if extcmp == -1 { + return true + } + if extcmp == 0 { + name1, name2 := normalizefun(dir.files[i].Name(), dir.files[j].Name()) + return name1 < name2 + } + return false } }