Add filtering to folder select (#4277)

This commit is contained in:
InfiniteStash
2023-11-20 03:00:28 +01:00
committed by GitHub
parent 61f4d8bd12
commit a0f33e3dab
10 changed files with 120 additions and 85 deletions

View File

@@ -4,6 +4,7 @@ import (
"io/fs"
"os"
"path/filepath"
"strings"
"golang.org/x/text/collate"
)
@@ -25,13 +26,27 @@ func (s dirLister) Bytes(i int) []byte {
// listDir will return the contents of a given directory path as a string slice
func listDir(col *collate.Collator, path string) ([]string, error) {
var dirPaths []string
dirPath := path
files, err := os.ReadDir(path)
if err != nil {
path = filepath.Dir(path)
files, err = os.ReadDir(path)
dirPath = filepath.Dir(path)
dirFiles, err := os.ReadDir(dirPath)
if err != nil {
return dirPaths, err
}
// Filter dir contents by last path fragment if the dir isn't an exact match
base := strings.ToLower(filepath.Base(path))
if base != "." && base != string(filepath.Separator) {
for _, file := range dirFiles {
if strings.HasPrefix(strings.ToLower(file.Name()), base) {
files = append(files, file)
}
}
} else {
files = dirFiles
}
}
if col != nil {
@@ -42,7 +57,7 @@ func listDir(col *collate.Collator, path string) ([]string, error) {
if !file.IsDir() {
continue
}
dirPaths = append(dirPaths, filepath.Join(path, file.Name()))
dirPaths = append(dirPaths, filepath.Join(dirPath, file.Name()))
}
return dirPaths, nil
}