Compare commits
2 commits
1339d642dd
...
054c1c23a5
Author | SHA1 | Date | |
---|---|---|---|
Lilian Jónsdóttir | 054c1c23a5 | ||
Lilian Jónsdóttir | cb47a01884 |
2
gt.1
2
gt.1
|
@ -5,7 +5,7 @@
|
||||||
.nh
|
.nh
|
||||||
.ad l
|
.ad l
|
||||||
.\" Begin generated content:
|
.\" Begin generated content:
|
||||||
.TH "gt" "1" "2024-07-31" "gt version v0.0.2" "User Commands"
|
.TH "gt" "1" "2024-07-31" "gt version v0.0.3" "User Commands"
|
||||||
.PP
|
.PP
|
||||||
.SH NAME
|
.SH NAME
|
||||||
.PP
|
.PP
|
||||||
|
|
6
gt.1.scd
6
gt.1.scd
|
@ -1,8 +1,8 @@
|
||||||
gt(1) ["gt version v0.0.2" ["User Commands"]]
|
gt(1) ["gt version v0.0.3" ["User Commands"]]
|
||||||
|
|
||||||
# NAME
|
# NAME
|
||||||
|
|
||||||
gt \- manual page for gt version 0.0.2
|
gt \- manual page for gt version 0.0.3
|
||||||
|
|
||||||
# DESCRIPTION
|
# DESCRIPTION
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ g(o)t(rash) is a simple, command line program to interface with the XDG Trash. F
|
||||||
|
|
||||||
## VERSION:
|
## VERSION:
|
||||||
|
|
||||||
0.0.2
|
0.0.3
|
||||||
|
|
||||||
## AUTHOR:
|
## AUTHOR:
|
||||||
|
|
||||||
|
|
154
internal/files/directorysizes.go
Normal file
154
internal/files/directorysizes.go
Normal file
|
@ -0,0 +1,154 @@
|
||||||
|
package files
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.burning.moe/celediel/gt/internal/dirs"
|
||||||
|
"github.com/charmbracelet/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
directorysizes = "directorysizes"
|
||||||
|
length int = 3
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
loadedDirSizes directorySizes
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
loadedDirSizes = readDirectorySizesFromFile()
|
||||||
|
}
|
||||||
|
|
||||||
|
type directorySize struct {
|
||||||
|
size int64
|
||||||
|
mtime int64
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
type directorySizes map[string]directorySize
|
||||||
|
|
||||||
|
func WriteDirectorySizes() {
|
||||||
|
loadedDirSizes = updateDirectorySizes(loadedDirSizes)
|
||||||
|
writeDirectorySizes(loadedDirSizes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func readDirectorySizesFromFile() directorySizes {
|
||||||
|
dirSizes := directorySizes{}
|
||||||
|
for _, trash := range getAllTrashes() {
|
||||||
|
dsf := filepath.Join(trash, directorysizes)
|
||||||
|
if _, err := os.Lstat(dsf); os.IsNotExist(err) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(dsf)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
var (
|
||||||
|
size int64
|
||||||
|
mtime int64
|
||||||
|
name string
|
||||||
|
)
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
split := strings.Split(line, " ")
|
||||||
|
if len(split) != length {
|
||||||
|
log.Errorf("malformed line '%s' in %s", line, dsf)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
size, err = strconv.ParseInt(split[0], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("size %s can't be int?", split[0])
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
mtime, err = strconv.ParseInt(split[1], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("mtime %s can't be int?", split[1])
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
name = dirs.PercentDecode(split[2])
|
||||||
|
|
||||||
|
dirSize := directorySize{
|
||||||
|
size: size,
|
||||||
|
mtime: mtime,
|
||||||
|
name: name,
|
||||||
|
}
|
||||||
|
dirSizes[name] = dirSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dirSizes
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateDirectorySizes(ds directorySizes) directorySizes {
|
||||||
|
newDs := directorySizes{}
|
||||||
|
for k, v := range ds {
|
||||||
|
newDs[k] = v
|
||||||
|
}
|
||||||
|
for _, trash := range getAllTrashes() {
|
||||||
|
files, err := os.ReadDir(filepath.Join(trash, "files"))
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, file := range files {
|
||||||
|
if _, ok := loadedDirSizes[file.Name()]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err := file.Info()
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !info.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
newDs[info.Name()] = directorySize{
|
||||||
|
size: calculateDirSize(filepath.Join(trash, "files", info.Name())),
|
||||||
|
mtime: info.ModTime().Unix(),
|
||||||
|
name: info.Name(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newDs
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeDirectorySizes(dirSizes directorySizes) {
|
||||||
|
// TODO: make this less bad
|
||||||
|
for _, trash := range getAllTrashes() {
|
||||||
|
var lines []string
|
||||||
|
out := filepath.Join(trash, directorysizes)
|
||||||
|
files, err := os.ReadDir(filepath.Join(trash, "files"))
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, file := range files {
|
||||||
|
if dirSize, ok := dirSizes[file.Name()]; ok {
|
||||||
|
lines = append(lines, fmt.Sprintf("%d %d ", dirSize.size, dirSize.mtime)+dirs.PercentEncode(file.Name()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile(out, []byte(strings.Join(lines, "\n")), noExecutePerm)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -26,12 +26,7 @@ func (f DiskFile) Path() string { return f.path }
|
||||||
func (f DiskFile) Date() time.Time { return f.modified }
|
func (f DiskFile) Date() time.Time { return f.modified }
|
||||||
func (f DiskFile) IsDir() bool { return f.isdir }
|
func (f DiskFile) IsDir() bool { return f.isdir }
|
||||||
func (f DiskFile) Mode() fs.FileMode { return f.mode }
|
func (f DiskFile) Mode() fs.FileMode { return f.mode }
|
||||||
func (f DiskFile) Filesize() int64 {
|
func (f DiskFile) Filesize() int64 { return f.filesize }
|
||||||
if f.isdir {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return f.filesize
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f DiskFile) String() string {
|
func (f DiskFile) String() string {
|
||||||
// this is unique enough because two files can't be named the same in the same directory
|
// this is unique enough because two files can't be named the same in the same directory
|
||||||
|
@ -53,14 +48,22 @@ func NewDisk(path string) (DiskFile, error) {
|
||||||
|
|
||||||
name := filepath.Base(abs)
|
name := filepath.Base(abs)
|
||||||
basePath := filepath.Dir(abs)
|
basePath := filepath.Dir(abs)
|
||||||
|
actualPath := filepath.Join(basePath, name)
|
||||||
|
|
||||||
|
var size int64
|
||||||
|
if info.IsDir() {
|
||||||
|
size = calculateDirSize(actualPath)
|
||||||
|
} else {
|
||||||
|
size = info.Size()
|
||||||
|
}
|
||||||
|
|
||||||
log.Debugf("%s (base:%s) (size:%s) (modified:%s) exists",
|
log.Debugf("%s (base:%s) (size:%s) (modified:%s) exists",
|
||||||
name, basePath, humanize.Bytes(uint64(info.Size())), info.ModTime())
|
name, basePath, humanize.Bytes(uint64(size)), info.ModTime())
|
||||||
|
|
||||||
return DiskFile{
|
return DiskFile{
|
||||||
name: name,
|
name: name,
|
||||||
path: filepath.Join(basePath, name),
|
path: actualPath,
|
||||||
filesize: info.Size(),
|
filesize: size,
|
||||||
modified: info.ModTime(),
|
modified: info.ModTime(),
|
||||||
isdir: info.IsDir(),
|
isdir: info.IsDir(),
|
||||||
mode: info.Mode(),
|
mode: info.Mode(),
|
||||||
|
@ -130,10 +133,17 @@ func walkDir(dir string, fltr *filter.Filter) Files {
|
||||||
name := dirEntry.Name()
|
name := dirEntry.Name()
|
||||||
info, _ := dirEntry.Info()
|
info, _ := dirEntry.Info()
|
||||||
if fltr.Match(info) {
|
if fltr.Match(info) {
|
||||||
|
var size int64
|
||||||
|
if info.IsDir() {
|
||||||
|
size = calculateDirSize(filepath.Join(actualPath, name))
|
||||||
|
} else {
|
||||||
|
size = info.Size()
|
||||||
|
}
|
||||||
|
|
||||||
files = append(files, DiskFile{
|
files = append(files, DiskFile{
|
||||||
path: actualPath,
|
path: actualPath,
|
||||||
name: name,
|
name: name,
|
||||||
filesize: info.Size(),
|
filesize: size,
|
||||||
modified: info.ModTime(),
|
modified: info.ModTime(),
|
||||||
isdir: info.IsDir(),
|
isdir: info.IsDir(),
|
||||||
mode: info.Mode(),
|
mode: info.Mode(),
|
||||||
|
@ -167,13 +177,24 @@ func readDir(dir string, fltr *filter.Filter) Files {
|
||||||
}
|
}
|
||||||
|
|
||||||
path := filepath.Dir(filepath.Join(dir, name))
|
path := filepath.Dir(filepath.Join(dir, name))
|
||||||
|
actualPath, e := filepath.Abs(path)
|
||||||
|
if e != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if fltr.Match(info) {
|
if fltr.Match(info) {
|
||||||
|
var size int64
|
||||||
|
if info.IsDir() {
|
||||||
|
size = calculateDirSize(filepath.Join(actualPath, name))
|
||||||
|
} else {
|
||||||
|
size = info.Size()
|
||||||
|
}
|
||||||
|
|
||||||
files = append(files, DiskFile{
|
files = append(files, DiskFile{
|
||||||
name: name,
|
name: name,
|
||||||
path: filepath.Join(path, name),
|
path: filepath.Join(actualPath, name),
|
||||||
modified: info.ModTime(),
|
modified: info.ModTime(),
|
||||||
filesize: info.Size(),
|
filesize: size,
|
||||||
isdir: info.IsDir(),
|
isdir: info.IsDir(),
|
||||||
mode: info.Mode(),
|
mode: info.Mode(),
|
||||||
})
|
})
|
||||||
|
|
|
@ -5,10 +5,13 @@ import (
|
||||||
"cmp"
|
"cmp"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/charmbracelet/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type File interface {
|
type File interface {
|
||||||
|
@ -33,6 +36,23 @@ func (fls Files) String() string {
|
||||||
return out.String()
|
return out.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
size += file.Filesize()
|
||||||
|
}
|
||||||
|
|
||||||
|
return size
|
||||||
|
}
|
||||||
|
|
||||||
func SortByModified(a, b File) int {
|
func SortByModified(a, b File) int {
|
||||||
if a.Date().Before(b.Date()) {
|
if a.Date().Before(b.Date()) {
|
||||||
return 1
|
return 1
|
||||||
|
@ -52,15 +72,11 @@ func SortByModifiedReverse(a, b File) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
func SortBySize(a, b File) int {
|
func SortBySize(a, b File) int {
|
||||||
as := getSortingSize(a)
|
return cmp.Compare(a.Filesize(), b.Filesize())
|
||||||
bs := getSortingSize(b)
|
|
||||||
return cmp.Compare(as, bs)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func SortBySizeReverse(a, b File) int {
|
func SortBySizeReverse(a, b File) int {
|
||||||
as := getSortingSize(a)
|
return cmp.Compare(b.Filesize(), a.Filesize())
|
||||||
bs := getSortingSize(b)
|
|
||||||
return cmp.Compare(bs, as)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func SortByName(a, b File) int {
|
func SortByName(a, b File) int {
|
||||||
|
@ -123,9 +139,36 @@ func doNameSort(a, b File) int {
|
||||||
return cmp.Compare(aname, bname)
|
return cmp.Compare(aname, bname)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSortingSize(f File) int64 {
|
func calculateDirSize(path string) int64 {
|
||||||
if f.IsDir() {
|
var size int64
|
||||||
return -1
|
info, err := os.Lstat(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
return f.Filesize()
|
if !info.IsDir() {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
files, err := os.ReadDir(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,8 +24,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
sep = string(os.PathSeparator)
|
|
||||||
executePerm = fs.FileMode(0755)
|
executePerm = fs.FileMode(0755)
|
||||||
|
noExecutePerm = fs.FileMode(0644)
|
||||||
noExecuteUserPerm = fs.FileMode(0600)
|
noExecuteUserPerm = fs.FileMode(0600)
|
||||||
randomStrLength int = 8
|
randomStrLength int = 8
|
||||||
trashName string = ".Trash"
|
trashName string = ".Trash"
|
||||||
|
@ -40,6 +40,8 @@ DeletionDate={date}
|
||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var homeTrash = filepath.Join(xdg.DataHome, "Trash")
|
||||||
|
|
||||||
type TrashInfo struct {
|
type TrashInfo struct {
|
||||||
name, ogpath string
|
name, ogpath string
|
||||||
path, trashinfo string
|
path, trashinfo string
|
||||||
|
@ -56,12 +58,7 @@ func (t TrashInfo) TrashInfo() string { return t.trashinfo }
|
||||||
func (t TrashInfo) Date() time.Time { return t.trashed }
|
func (t TrashInfo) Date() time.Time { return t.trashed }
|
||||||
func (t TrashInfo) IsDir() bool { return t.isdir }
|
func (t TrashInfo) IsDir() bool { return t.isdir }
|
||||||
func (t TrashInfo) Mode() fs.FileMode { return t.mode }
|
func (t TrashInfo) Mode() fs.FileMode { return t.mode }
|
||||||
func (t TrashInfo) Filesize() int64 {
|
func (t TrashInfo) Filesize() int64 { return t.filesize }
|
||||||
if t.isdir {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return t.filesize
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t TrashInfo) String() string {
|
func (t TrashInfo) String() string {
|
||||||
return t.name + t.path + t.ogpath + t.trashinfo
|
return t.name + t.path + t.ogpath + t.trashinfo
|
||||||
|
@ -70,12 +67,6 @@ func (t TrashInfo) String() string {
|
||||||
func FindInAllTrashes(ogdir string, fltr *filter.Filter) Files {
|
func FindInAllTrashes(ogdir string, fltr *filter.Filter) Files {
|
||||||
var files Files
|
var files Files
|
||||||
|
|
||||||
personalTrash := filepath.Join(xdg.DataHome, "Trash")
|
|
||||||
|
|
||||||
if fls, err := findTrash(personalTrash, ogdir, fltr); err == nil {
|
|
||||||
files = append(files, fls...)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, trash := range getAllTrashes() {
|
for _, trash := range getAllTrashes() {
|
||||||
fls, err := findTrash(trash, ogdir, fltr)
|
fls, err := findTrash(trash, ogdir, fltr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -142,17 +133,17 @@ func findTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
|
||||||
var files Files
|
var files Files
|
||||||
|
|
||||||
infodir := filepath.Join(trashdir, "info")
|
infodir := filepath.Join(trashdir, "info")
|
||||||
dirs, err := os.ReadDir(infodir)
|
entries, err := os.ReadDir(infodir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, dir := range dirs {
|
for _, entry := range entries {
|
||||||
if dir.IsDir() || filepath.Ext(dir.Name()) != trashInfoExt {
|
if entry.IsDir() || filepath.Ext(entry.Name()) != trashInfoExt {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
path := filepath.Join(infodir, dir.Name())
|
path := filepath.Join(infodir, entry.Name())
|
||||||
|
|
||||||
// trashinfo is just an ini file, so
|
// trashinfo is just an ini file, so
|
||||||
trashInfo, err := ini.Load(path)
|
trashInfo, err := ini.Load(path)
|
||||||
|
@ -161,39 +152,57 @@ func findTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if section := trashInfo.Section(trashInfoSec); section != nil {
|
section := trashInfo.Section(trashInfoSec)
|
||||||
basepath := section.Key(trashInfoPath).String()
|
if section == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
filename := filepath.Base(basepath)
|
basepath := dirs.PercentDecode(section.Key(trashInfoPath).String())
|
||||||
trashedpath := strings.Replace(strings.Replace(path, "info", "files", 1), trashInfoExt, "", 1)
|
if !strings.HasPrefix(basepath, string(os.PathSeparator)) {
|
||||||
info, err := os.Lstat(trashedpath)
|
root, err := getRoot(trashdir)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
log.Errorf("error reading '%s': %s", trashedpath, err)
|
basepath = filepath.Join(root, basepath)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
s := section.Key(trashInfoDate).Value()
|
filename := filepath.Base(basepath)
|
||||||
date, err := time.ParseInLocation(trashInfoDateFmt, s, time.Local)
|
trashedpath := strings.Replace(strings.Replace(path, "info", "files", 1), trashInfoExt, "", 1)
|
||||||
if err != nil {
|
info, err := os.Lstat(trashedpath)
|
||||||
log.Errorf("error parsing date '%s' in trashinfo file '%s': %s", s, path, err)
|
if err != nil {
|
||||||
continue
|
log.Errorf("error reading '%s': %s", trashedpath, err)
|
||||||
}
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if ogdir != "" && filepath.Dir(basepath) != ogdir {
|
s := section.Key(trashInfoDate).Value()
|
||||||
continue
|
date, err := time.ParseInLocation(trashInfoDateFmt, s, time.Local)
|
||||||
}
|
if err != nil {
|
||||||
|
log.Errorf("error parsing date '%s' in trashinfo file '%s': %s", s, path, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if fltr.Match(info) {
|
if ogdir != "" && filepath.Dir(basepath) != ogdir {
|
||||||
files = append(files, TrashInfo{
|
continue
|
||||||
name: filename,
|
}
|
||||||
path: trashedpath,
|
|
||||||
ogpath: basepath,
|
var size int64
|
||||||
trashinfo: path,
|
if d, ok := loadedDirSizes[info.Name()]; ok {
|
||||||
trashed: date,
|
size = d.size
|
||||||
isdir: info.IsDir(),
|
} else if info.IsDir() {
|
||||||
filesize: info.Size(),
|
size = calculateDirSize(trashedpath)
|
||||||
})
|
} else {
|
||||||
}
|
size = info.Size()
|
||||||
|
}
|
||||||
|
|
||||||
|
if fltr.Match(info) {
|
||||||
|
files = append(files, TrashInfo{
|
||||||
|
name: filename,
|
||||||
|
path: trashedpath,
|
||||||
|
ogpath: basepath,
|
||||||
|
trashinfo: path,
|
||||||
|
trashed: date,
|
||||||
|
isdir: info.IsDir(),
|
||||||
|
filesize: size,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -212,8 +221,21 @@ func trashFile(filename string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var path string
|
||||||
|
if trashDir == homeTrash {
|
||||||
|
path = filename
|
||||||
|
} else {
|
||||||
|
root, err := getRoot(trashDir)
|
||||||
|
if err != nil {
|
||||||
|
path = filename
|
||||||
|
} else {
|
||||||
|
path = strings.Replace(filename, root+string(os.PathSeparator), "", 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Debugf("fucking %s %s %s", filename, trashDir, path)
|
||||||
|
|
||||||
trashInfo, err := formatter.Format(trashInfoTemplate, formatter.Named{
|
trashInfo, err := formatter.Format(trashInfoTemplate, formatter.Named{
|
||||||
"path": filename,
|
"path": path,
|
||||||
"date": time.Now().Format(trashInfoDateFmt),
|
"date": time.Now().Format(trashInfoDateFmt),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -355,7 +377,7 @@ func getTrashDir(filename string) (string, error) {
|
||||||
if strings.Contains(filename, xdg.Home) {
|
if strings.Contains(filename, xdg.Home) {
|
||||||
trashDir = filepath.Join(xdg.DataHome, trashName[1:])
|
trashDir = filepath.Join(xdg.DataHome, trashName[1:])
|
||||||
} else {
|
} else {
|
||||||
trashDir = filepath.Clean(root + sep + trashName)
|
trashDir = filepath.Clean(filepath.Join(root, trashName))
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Lstat(trashDir); err != nil {
|
if _, err := os.Lstat(trashDir); err != nil {
|
||||||
|
@ -406,12 +428,15 @@ func getRoot(path string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAllTrashes() []string {
|
func getAllTrashes() []string {
|
||||||
var trashes []string
|
trashes := []string{homeTrash}
|
||||||
usr, _ := user.Current()
|
usr, err := user.Current()
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
_, err := mountinfo.GetMounts(func(mount *mountinfo.Info) (skip bool, stop bool) {
|
_, err = mountinfo.GetMounts(func(mount *mountinfo.Info) (skip bool, stop bool) {
|
||||||
point := mount.Mountpoint
|
point := mount.Mountpoint
|
||||||
trashDir := filepath.Clean(point + sep + trashName)
|
trashDir := filepath.Clean(filepath.Join(point, trashName))
|
||||||
userTrashDir := trashDir + "-" + usr.Uid
|
userTrashDir := trashDir + "-" + usr.Uid
|
||||||
|
|
||||||
if _, err := os.Lstat(trashDir); err == nil {
|
if _, err := os.Lstat(trashDir); err == nil {
|
||||||
|
|
|
@ -73,6 +73,7 @@ type model struct {
|
||||||
keys keyMap
|
keys keyMap
|
||||||
selected map[string]bool
|
selected map[string]bool
|
||||||
selectsize int64
|
selectsize int64
|
||||||
|
totalsize int64
|
||||||
readonly bool
|
readonly bool
|
||||||
once bool
|
once bool
|
||||||
filtering bool
|
filtering bool
|
||||||
|
@ -95,6 +96,7 @@ func newModel(fls files.Files, selectall, readonly, once bool, workdir string, m
|
||||||
selected: map[string]bool{},
|
selected: map[string]bool{},
|
||||||
selectsize: 0,
|
selectsize: 0,
|
||||||
files: fls,
|
files: fls,
|
||||||
|
totalsize: fls.TotalSize(),
|
||||||
}
|
}
|
||||||
|
|
||||||
m.termwidth, m.termheight = termSizes()
|
m.termwidth, m.termheight = termSizes()
|
||||||
|
@ -326,11 +328,13 @@ func (m model) header() string {
|
||||||
styleKey(m.keys.clfl),
|
styleKey(m.keys.clfl),
|
||||||
styleKey(m.keys.apfl),
|
styleKey(m.keys.apfl),
|
||||||
}
|
}
|
||||||
dot = darkesttext.Render("•")
|
dot = darkesttext.Render("•")
|
||||||
wideDot = darkesttext.Render(" • ")
|
wideDot = darkesttext.Render(" • ")
|
||||||
keysFmt = strings.Join(keys, wideDot)
|
keysFmt = strings.Join(keys, wideDot)
|
||||||
selectFmt = strings.Join(selectKeys, wideDot)
|
selectFmt = strings.Join(selectKeys, wideDot)
|
||||||
filterFmt = strings.Join(filterKeys, wideDot)
|
filterFmt = strings.Join(filterKeys, wideDot)
|
||||||
|
totalSize = humanize.Bytes(uint64(m.totalsize))
|
||||||
|
selectedSize = fmt.Sprintf("%s/%s", humanize.Bytes(uint64(m.selectsize)), totalSize)
|
||||||
)
|
)
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
|
@ -338,24 +342,23 @@ func (m model) header() string {
|
||||||
right = fmt.Sprintf(" Filtering %s %s", dot, filterFmt)
|
right = fmt.Sprintf(" Filtering %s %s", dot, filterFmt)
|
||||||
case m.mode == modes.Interactive:
|
case m.mode == modes.Interactive:
|
||||||
right = fmt.Sprintf(" %s %s %s", keysFmt, dot, selectFmt)
|
right = fmt.Sprintf(" %s %s %s", keysFmt, dot, selectFmt)
|
||||||
left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.fltrfiles), dot, humanize.Bytes(uint64(m.selectsize)))
|
left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.fltrfiles), dot, selectedSize)
|
||||||
case m.mode == modes.Listing:
|
case m.mode == modes.Listing:
|
||||||
var filtered string
|
var filtered string
|
||||||
if m.filter != "" || m.filtering {
|
if m.filter != "" || m.filtering {
|
||||||
filtered = " (filtered)"
|
filtered = " (filtered)"
|
||||||
}
|
}
|
||||||
right = fmt.Sprintf(" Showing%s %d files in trash", filtered, len(m.fltrfiles))
|
right = fmt.Sprintf(" Showing%s %d files in trash (%s)", filtered, len(m.fltrfiles), totalSize)
|
||||||
default:
|
default:
|
||||||
var wd string
|
var wd string
|
||||||
if m.workdir != "" {
|
if m.workdir != "" {
|
||||||
wd = " in " + dirs.UnExpand(m.workdir, "")
|
wd = " in " + dirs.UnExpand(m.workdir, "")
|
||||||
}
|
}
|
||||||
right = fmt.Sprintf(" %s%s %s %s", m.mode.String(), wd, dot, selectFmt)
|
right = fmt.Sprintf(" %s%s %s %s", m.mode.String(), wd, dot, selectFmt)
|
||||||
left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.fltrfiles), dot, humanize.Bytes(uint64(m.selectsize)))
|
left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.fltrfiles), dot, selectedSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
// offset of 2 again because of table border
|
spacerWidth = (m.termwidth - lipgloss.Width(right) - lipgloss.Width(left)) + 1
|
||||||
spacerWidth = m.termwidth - lipgloss.Width(right) - lipgloss.Width(left) - poffset
|
|
||||||
if spacerWidth <= 0 {
|
if spacerWidth <= 0 {
|
||||||
spacerWidth = 1 // always at least one space
|
spacerWidth = 1 // always at least one space
|
||||||
}
|
}
|
||||||
|
@ -662,14 +665,14 @@ func Show(fls files.Files, once bool, workdir string) error {
|
||||||
|
|
||||||
func newRow(file files.File, workdir string) table.Row {
|
func newRow(file files.File, workdir string) table.Row {
|
||||||
var time, size string
|
var time, size string
|
||||||
|
name := file.Name()
|
||||||
time = humanize.Time(file.Date())
|
time = humanize.Time(file.Date())
|
||||||
if file.IsDir() {
|
if file.IsDir() {
|
||||||
size = bar
|
name += string(os.PathSeparator)
|
||||||
} else {
|
|
||||||
size = humanize.Bytes(uint64(file.Filesize()))
|
|
||||||
}
|
}
|
||||||
|
size = humanize.Bytes(uint64(file.Filesize()))
|
||||||
return table.Row{
|
return table.Row{
|
||||||
dirs.PercentDecode(file.Name()),
|
dirs.PercentDecode(name),
|
||||||
dirs.UnExpand(filepath.Dir(file.Path()), workdir),
|
dirs.UnExpand(filepath.Dir(file.Path()), workdir),
|
||||||
time,
|
time,
|
||||||
size,
|
size,
|
||||||
|
|
2
justfile
2
justfile
|
@ -1,5 +1,5 @@
|
||||||
binary := "gt"
|
binary := "gt"
|
||||||
version := "0.0.2"
|
version := "0.0.3"
|
||||||
build_dir := "bin"
|
build_dir := "bin"
|
||||||
dist_dir := "dist"
|
dist_dir := "dist"
|
||||||
cmd := "."
|
cmd := "."
|
||||||
|
|
3
main.go
3
main.go
|
@ -25,7 +25,7 @@ import (
|
||||||
const (
|
const (
|
||||||
appname string = "gt"
|
appname string = "gt"
|
||||||
appsubtitle string = "xdg trash cli"
|
appsubtitle string = "xdg trash cli"
|
||||||
appversion string = "v0.0.2"
|
appversion string = "v0.0.3"
|
||||||
appdesc string = `A small command line program to interface with the
|
appdesc string = `A small command line program to interface with the
|
||||||
Freedesktop.org / XDG trash specification.
|
Freedesktop.org / XDG trash specification.
|
||||||
|
|
||||||
|
@ -195,6 +195,7 @@ var (
|
||||||
}
|
}
|
||||||
|
|
||||||
after = func(_ *cli.Context) error {
|
after = func(_ *cli.Context) error {
|
||||||
|
files.WriteDirectorySizes()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue