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-08-14 18:48:06 -04:00
|
|
|
"fmt"
|
2024-07-16 00:05:21 -04:00
|
|
|
"io/fs"
|
2024-08-14 21:52:45 -04:00
|
|
|
"os"
|
2024-07-21 23:33:25 -04:00
|
|
|
"path/filepath"
|
2024-07-29 02:29:59 -04:00
|
|
|
"strconv"
|
2024-07-15 19:10:04 -04:00
|
|
|
"strings"
|
2024-07-15 18:07:39 -04:00
|
|
|
"time"
|
2024-08-14 21:52:45 -04:00
|
|
|
|
|
|
|
"github.com/charmbracelet/log"
|
2024-07-15 18:07:39 -04:00
|
|
|
)
|
2024-07-03 18:05:17 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-08-14 18:48:06 -04:00
|
|
|
func (fls Files) String() string {
|
|
|
|
var out = strings.Builder{}
|
|
|
|
for _, file := range fls {
|
|
|
|
out.WriteString(fmt.Sprintf("%s\t%s\t%s\n",
|
|
|
|
file.Date().Format(time.RFC3339), file.Name(), file.Path(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
|
2024-08-14 21:52:45 -04:00
|
|
|
func (fls Files) TotalSize() int64 {
|
|
|
|
var size int64
|
|
|
|
|
|
|
|
for _, file := range fls {
|
|
|
|
if file.IsDir() {
|
|
|
|
if d, ok := loadedDirSizes[file.Name()]; ok {
|
|
|
|
log.Debugf("%s: got %d from directorysizes", file.Name(), d.size)
|
|
|
|
size += d.size
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
size += calculateDirSize(file.Path())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size += file.Filesize()
|
|
|
|
}
|
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2024-07-03 18:05:17 -04:00
|
|
|
func SortByModified(a, b File) int {
|
2024-07-15 18:07:39 -04:00
|
|
|
if a.Date().Before(b.Date()) {
|
2024-07-03 18:05:17 -04:00
|
|
|
return 1
|
2024-07-15 18:07:39 -04:00
|
|
|
} else if a.Date().After(b.Date()) {
|
2024-07-03 18:05:17 -04:00
|
|
|
return -1
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
2024-07-31 03:13:48 -04:00
|
|
|
return 0
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:05:17 -04:00
|
|
|
func SortByModifiedReverse(a, b File) int {
|
2024-07-15 18:07:39 -04:00
|
|
|
if a.Date().After(b.Date()) {
|
2024-07-03 18:05:17 -04:00
|
|
|
return 1
|
2024-07-15 18:07:39 -04:00
|
|
|
} else if a.Date().Before(b.Date()) {
|
2024-07-03 18:05:17 -04:00
|
|
|
return -1
|
2024-06-19 23:25:04 -04:00
|
|
|
}
|
2024-07-31 03:13:48 -04:00
|
|
|
return 0
|
2024-06-19 23:25:04 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:05:17 -04:00
|
|
|
func SortBySize(a, b File) int {
|
2024-08-14 21:52:45 -04:00
|
|
|
return cmp.Compare(a.Filesize(), b.Filesize())
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:05:17 -04:00
|
|
|
func SortBySizeReverse(a, b File) int {
|
2024-08-14 21:52:45 -04:00
|
|
|
return cmp.Compare(b.Filesize(), a.Filesize())
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:05:17 -04:00
|
|
|
func SortByName(a, b File) int {
|
2024-07-29 02:29:59 -04:00
|
|
|
return doNameSort(a, b)
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:05:17 -04:00
|
|
|
func SortByNameReverse(a, b File) int {
|
2024-07-29 02:29:59 -04:00
|
|
|
return doNameSort(b, a)
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:05:17 -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
|
|
|
|
}
|
2024-07-31 03:13:48 -04:00
|
|
|
return 0
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2024-07-31 03:13:48 -04:00
|
|
|
return 0
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
2024-07-21 23:54:22 -04:00
|
|
|
|
2024-07-29 02:29:59 -04:00
|
|
|
func doNameSort(a, b File) int {
|
2024-07-30 14:43:54 -04:00
|
|
|
aname := strings.ToLower(a.Name())
|
|
|
|
bname := strings.ToLower(b.Name())
|
2024-07-29 02:29:59 -04:00
|
|
|
// check if filename is a number
|
2024-07-30 14:43:54 -04:00
|
|
|
abase := strings.Replace(aname, filepath.Ext(aname), "", 1)
|
|
|
|
bbase := strings.Replace(bname, filepath.Ext(bname), "", 1)
|
2024-07-29 02:29:59 -04:00
|
|
|
ai, aerr := strconv.Atoi(abase)
|
|
|
|
bi, berr := strconv.Atoi(bbase)
|
|
|
|
if aerr == nil && berr == nil {
|
|
|
|
return cmp.Compare(ai, bi)
|
|
|
|
}
|
2024-07-30 14:43:54 -04:00
|
|
|
return cmp.Compare(aname, bname)
|
2024-07-29 02:29:59 -04:00
|
|
|
}
|
|
|
|
|
2024-08-14 21:52:45 -04:00
|
|
|
func calculateDirSize(path string) int64 {
|
|
|
|
var size int64
|
|
|
|
info, err := os.Lstat(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
files, err := os.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return 0
|
2024-07-21 23:54:22 -04:00
|
|
|
}
|
2024-08-14 21:52:45 -04:00
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
filePath := filepath.Join(path, file.Name())
|
|
|
|
info, err := os.Lstat(filePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
size += calculateDirSize(filePath)
|
|
|
|
} else {
|
|
|
|
size += info.Size()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return size
|
2024-07-21 23:54:22 -04:00
|
|
|
}
|