From c57bb9c4c28a50a9bbeb1510dc0806d9759ab31e Mon Sep 17 00:00:00 2001 From: Seednode Date: Thu, 28 Sep 2023 03:36:26 -0500 Subject: [PATCH] Sorting now supports arbitrary numbering length, so long as it is sequential --- README.md | 6 ++--- cmd/files.go | 72 ++++++++++++++++++++++++++++++---------------------- cmd/root.go | 2 +- cmd/web.go | 2 +- 4 files changed, 47 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index b43e4e2..69dbef3 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ You can also provide a comma-delimited string of alphanumeric patterns to exclud Filenames matching any of these patterns will not be served. -You can also combine these two parameters, with exclusions taking priority over inclusions. +You can combine these two parameters, with exclusions taking priority over inclusions. Both filtering parameters ignore the file extension and full path; they only compare against the bare filename. @@ -77,7 +77,7 @@ Enjoy! ## Sorting -You can specify a sorting pattern via the `sort=` query parameter, assuming the `-s|--sort` flag is enabled. +You can specify a sorting direction via the `sort=` query parameter, assuming the `-s|--sort` flag is enabled. A value of `sort=asc` means files will be served in ascending order (lowest-numbered to highest). @@ -95,7 +95,7 @@ For `sort=desc`, the highest-numbered file will be served instead. If any other (or no) value is provided, the selected file will be random. -Note: These patterns require sequentially-numbered files matching the following pattern: `filename###.extension`. +Note: These options require sequentially-numbered files matching the following pattern: `filename[0-9]*.extension`. ## Themes The `--code` handler provides syntax highlighting via [alecthomas/chroma](https://github.com/alecthomas/chroma). diff --git a/cmd/files.go b/cmd/files.go index a67eb72..f302b42 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -42,16 +42,30 @@ type scanStatsChannels struct { type splitPath struct { base string - number int + number string extension string } func (splitPath *splitPath) increment() { - splitPath.number = splitPath.number + 1 + length := len(splitPath.number) + + asInt, err := strconv.Atoi(splitPath.number) + if err != nil { + return + } + + splitPath.number = fmt.Sprintf("%0*d", length, asInt+1) } func (splitPath *splitPath) decrement() { - splitPath.number = splitPath.number - 1 + length := len(splitPath.number) + + asInt, err := strconv.Atoi(splitPath.number) + if err != nil { + return + } + + splitPath.number = fmt.Sprintf("%0*d", length, asInt-1) } func humanReadableSize(bytes int) string { @@ -85,26 +99,22 @@ func kill(path string, cache *fileCache) error { return nil } -func split(path string, regexes *regexes) (*splitPath, error) { +func split(path string, regexes *regexes) (*splitPath, int, error) { p := splitPath{} - var err error split := regexes.filename.FindAllStringSubmatch(path, -1) if len(split) < 1 || len(split[0]) < 3 { - return &splitPath{}, nil + return &splitPath{}, 0, nil } p.base = split[0][1] - p.number, err = strconv.Atoi(split[0][2]) - if err != nil { - return &splitPath{}, err - } + p.number = split[0][2] p.extension = split[0][3] - return &p, nil + return &p, len(p.number), nil } func newFile(list []string, sortOrder string, regexes *regexes, formats *types.Types) (string, error) { @@ -113,37 +123,39 @@ func newFile(list []string, sortOrder string, regexes *regexes, formats *types.T return "", err } - splitPath, err := split(path, regexes) - if err != nil { - return "", err - } - - splitPath.number = 1 - - switch { - case sortOrder == "asc": - path, err = tryExtensions(splitPath, formats) + if sortOrder == "asc" || sortOrder == "desc" { + splitPath, length, err := split(path, regexes) if err != nil { return "", err } - case sortOrder == "desc": - for { - splitPath.increment() + + switch { + case sortOrder == "asc": + splitPath.number = fmt.Sprintf("%0*d", length, 1) path, err = tryExtensions(splitPath, formats) if err != nil { return "", err } - - if path == "" { - splitPath.decrement() + case sortOrder == "desc": + for { + splitPath.increment() path, err = tryExtensions(splitPath, formats) if err != nil { return "", err } - break + if path == "" { + splitPath.decrement() + + path, err = tryExtensions(splitPath, formats) + if err != nil { + return "", err + } + + break + } } } } @@ -152,7 +164,7 @@ func newFile(list []string, sortOrder string, regexes *regexes, formats *types.T } func nextFile(filePath, sortOrder string, regexes *regexes, formats *types.Types) (string, error) { - splitPath, err := split(filePath, regexes) + splitPath, _, err := split(filePath, regexes) if err != nil { return "", err } @@ -178,7 +190,7 @@ func tryExtensions(splitPath *splitPath, formats *types.Types) (string, error) { var path string for extension := range formats.Extensions { - path = fmt.Sprintf("%s%.3d%s", splitPath.base, splitPath.number, extension) + path = fmt.Sprintf("%s%s%s", splitPath.base, splitPath.number, extension) exists, err := fileExists(path) if err != nil { diff --git a/cmd/root.go b/cmd/root.go index f5239e3..ef7f261 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,7 +12,7 @@ import ( ) const ( - ReleaseVersion string = "0.96.5" + ReleaseVersion string = "0.97.0" ) var ( diff --git a/cmd/web.go b/cmd/web.go index 7035c56..56b8a3d 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -460,7 +460,7 @@ func ServePage(args []string) error { } regexes := ®exes{ - filename: regexp.MustCompile(`(.+)([0-9]{3})(\..+)`), + filename: regexp.MustCompile(`(.+?)([0-9]*)(\..+)`), alphanumeric: regexp.MustCompile(`^[A-z0-9]*$`), }