gt/internal/files/files.go

126 lines
2.3 KiB
Go
Raw Normal View History

2024-06-18 18:21:03 -04:00
// Package files finds and displays files on disk
package files
2024-07-15 18:07:39 -04:00
import (
"cmp"
2024-07-16 00:05:21 -04:00
"io/fs"
"path/filepath"
"strconv"
2024-07-15 19:10:04 -04:00
"strings"
2024-07-15 18:07:39 -04:00
"time"
)
type File interface {
Name() string
Path() string
Date() time.Time
Filesize() int64
IsDir() bool
2024-07-16 00:05:21 -04:00
Mode() fs.FileMode
2024-07-15 18:32:33 -04:00
String() string
2024-06-18 18:21:03 -04:00
}
type Files []File
func SortByModified(a, b File) int {
2024-07-15 18:07:39 -04:00
if a.Date().Before(b.Date()) {
return 1
2024-07-15 18:07:39 -04:00
} else if a.Date().After(b.Date()) {
return -1
2024-06-18 18:21:03 -04:00
} else {
return 0
2024-06-18 18:21:03 -04:00
}
}
func SortByModifiedReverse(a, b File) int {
2024-07-15 18:07:39 -04:00
if a.Date().After(b.Date()) {
return 1
2024-07-15 18:07:39 -04:00
} else if a.Date().Before(b.Date()) {
return -1
} else {
return 0
}
}
func SortBySize(a, b File) int {
2024-07-21 23:54:22 -04:00
as := getSortingSize(a)
bs := getSortingSize(b)
return cmp.Compare(as, bs)
2024-06-18 18:21:03 -04:00
}
func SortBySizeReverse(a, b File) int {
2024-07-21 23:54:22 -04:00
as := getSortingSize(a)
bs := getSortingSize(b)
return cmp.Compare(bs, as)
2024-06-18 18:21:03 -04:00
}
func SortByName(a, b File) int {
return doNameSort(a, b)
2024-06-18 18:21:03 -04:00
}
func SortByNameReverse(a, b File) int {
return doNameSort(b, a)
2024-06-18 18:21:03 -04:00
}
func SortByPath(a, b File) int {
2024-07-15 18:07:39 -04:00
return cmp.Compare(a.Path(), b.Path())
}
func SortByPathReverse(a, b File) int {
return cmp.Compare(b.Path(), a.Path())
}
2024-07-15 19:10:04 -04:00
func SortByExtension(a, b File) int {
2024-07-28 00:05:12 -04:00
aext := strings.ToLower(filepath.Ext(a.Name()))
bext := strings.ToLower(filepath.Ext(b.Name()))
2024-07-15 19:10:04 -04:00
return cmp.Compare(aext, bext)
}
func SortByExtensionReverse(a, b File) int {
2024-07-28 00:05:12 -04:00
aext := strings.ToLower(filepath.Ext(a.Name()))
bext := strings.ToLower(filepath.Ext(b.Name()))
2024-07-15 19:10:04 -04:00
return cmp.Compare(bext, aext)
}
2024-07-15 18:07:39 -04:00
func SortDirectoriesFirst(a, b File) int {
if !a.IsDir() && b.IsDir() {
2024-06-18 18:21:03 -04:00
return 1
2024-07-15 18:07:39 -04:00
} else if a.IsDir() && !b.IsDir() {
2024-06-18 18:21:03 -04:00
return -1
} else {
return 0
}
}
2024-07-15 18:07:39 -04:00
func SortDirectoriesLast(a, b File) int {
if a.IsDir() && !b.IsDir() {
2024-06-18 18:21:03 -04:00
return 1
2024-07-15 18:07:39 -04:00
} else if !a.IsDir() && b.IsDir() {
2024-06-18 18:21:03 -04:00
return -1
} else {
return 0
}
}
2024-07-21 23:54:22 -04:00
func doNameSort(a, b File) int {
an := strings.ToLower(a.Name())
bn := strings.ToLower(b.Name())
// check if filename is a number
abase := strings.Replace(an, filepath.Ext(an), "", 1)
bbase := strings.Replace(bn, filepath.Ext(bn), "", 1)
ai, aerr := strconv.Atoi(abase)
bi, berr := strconv.Atoi(bbase)
if aerr == nil && berr == nil {
return cmp.Compare(ai, bi)
}
return cmp.Compare(an, bn)
}
2024-07-21 23:54:22 -04:00
func getSortingSize(f File) int64 {
if f.IsDir() {
return -1
} else {
return f.Filesize()
}
}