gt/internal/files/files.go

85 lines
1.4 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"
"time"
)
2024-07-15 18:32:33 -04:00
const string_format = "%s %s %s %d %t"
type File interface {
Name() string
Path() string
Date() time.Time
Filesize() int64
IsDir() bool
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-15 18:07:39 -04:00
return cmp.Compare(b.Filesize(), a.Filesize())
2024-06-18 18:21:03 -04:00
}
func SortBySizeReverse(a, b File) int {
2024-07-15 18:07:39 -04:00
return cmp.Compare(a.Filesize(), b.Filesize())
2024-06-18 18:21:03 -04:00
}
func SortByName(a, b File) int {
2024-07-15 18:07:39 -04:00
return cmp.Compare(a.Name(), b.Name())
2024-06-18 18:21:03 -04:00
}
func SortByNameReverse(a, b File) int {
2024-07-15 18:07:39 -04:00
return cmp.Compare(b.Name(), a.Name())
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())
}
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
}
}