Compare commits

..

No commits in common. "1339d642dd5a4225d5b717c94452d811c67d5a30" and "03c09e1da6253a4b468b2c44b01c26e4f09ed394" have entirely different histories.

14 changed files with 264 additions and 661 deletions

View file

@ -23,7 +23,7 @@ Find files on the filesystem based on the filter flags and any filename args.
*--recursive*, *-r* *--recursive*, *-r*
operate on files recursively operate on files recursively
*--work-dir* **dir**, *-w* **dir** *--work-dir* dir, *-w* dir
operate on files in this directory operate on files in this directory
*--hidden*, *-h* *--hidden*, *-h*
@ -38,7 +38,7 @@ Find files in the trash based on the filter flags and any filename args.
*--non-interactive*, *-n* *--non-interactive*, *-n*
list files and quit list files and quit
*--original-path* **dir**, *-O* **dir** *--original-path* dir, *-O* dir
list files trashed from this directory list files trashed from this directory
### restore / re ### restore / re
@ -50,8 +50,8 @@ Find files in the trash based on the filter flags and any filename args.
*--all*, *-a* *--all*, *-a*
restore all files in trash restore all files in trash
*--original-path* **dir**, *-O* **dir** *--original-path* dir, *-O* dir
*restore* files trashed from this directory restore files trashed from this directory
### clean / cl ### clean / cl
@ -62,7 +62,7 @@ Find files in the trash based on the filter flags and any filename args.
*--all*, *-a* *--all*, *-a*
clean all files in trash clean all files in trash
*--original-path* **dir**, *-O* **dir** *--original-path* dir, *-O* dir
remove files trashed from this directory remove files trashed from this directory
## Flags ## Flags
@ -72,30 +72,30 @@ remove files trashed from this directory
*--confirm*, *-c* *--confirm*, *-c*
ask for confirmation before executing any action ask for confirmation before executing any action
*--log* **level**, *-l* **level** *--log* level, *-l* level
set log level set log level
### Filter flags (usable with all commands) ### Filter flags (usable with all commands)
*--match* **pattern**, *-m* **pattern** *--match* pattern, *-m* pattern
operate on files matching regex pattern operate on files matching regex pattern
*--glob* **pattern**, *-m* **pattern** *--glob* pattern, *-m* pattern
operate on files matching glob operate on files matching glob
*--not-match* **pattern**, *-M* **pattern** *--not-match* pattern, *-M* pattern
operate on files not matching regex pattern operate on files not matching regex pattern
*--not-glob* **pattern**, *-G* **pattern** *--not-glob* pattern, *-G* pattern
operate on files not matching glob operate on files not matching glob
*--on* **date**, *-O* **date** *--on* date, *-O* date
operate on files modified on date operate on files modified on date
*--before* **date**, *-B* **date** *--before* date, *-B* date
operate on files modified before date operate on files modified before date
*--after* **date**, *-A* **date** *--after* date, *-A* date
operate on files modified after date operate on files modified after date
*--files-only*, *-F* *--files-only*, *-F*
@ -104,13 +104,13 @@ operate on files only
*--dirs-only*, *-D* *--dirs-only*, *-D*
operate on directories only operate on directories only
*--min-size* **size**, *-N* **size** *--min-size* size, *-N* size
operate on files larger than size operate on files larger than size
*--max-size* **size**, *-X* **size** *--max-size* size, *-X* size
operate on files smaller than size operate on files smaller than size
*--mode* **mode**, *-x* **mode** *--mode* mode, *-x* mode
operate on files matching mode mode operate on files matching mode mode
See also gt(1) or `gt --help`. See also gt(1) or `gt --help`.

1
go.mod
View file

@ -12,7 +12,6 @@ require (
github.com/dustin/go-humanize v1.0.1 github.com/dustin/go-humanize v1.0.1
github.com/ijt/go-anytime v1.9.2 github.com/ijt/go-anytime v1.9.2
github.com/lithammer/fuzzysearch v1.1.8 github.com/lithammer/fuzzysearch v1.1.8
github.com/moby/sys/mountinfo v0.7.2
github.com/urfave/cli/v2 v2.27.3 github.com/urfave/cli/v2 v2.27.3
gitlab.com/tymonx/go-formatter v1.5.1 gitlab.com/tymonx/go-formatter v1.5.1
golang.org/x/term v0.22.0 golang.org/x/term v0.22.0

2
go.sum
View file

@ -58,8 +58,6 @@ github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6T
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4= github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE= github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
github.com/moby/sys/mountinfo v0.7.2 h1:1shs6aH5s4o5H2zQLn796ADW1wMrIwHsyJ2v9KouLrg=
github.com/moby/sys/mountinfo v0.7.2/go.mod h1:1YOa8w8Ih7uW0wALDUgT1dTTSBrZ+HiBLGws92L2RU4=
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=

2
gt.1
View file

@ -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.3" "User Commands" .TH "gt" "1" "2024-07-31" "gt version v0.0.2" "User Commands"
.PP .PP
.SH NAME .SH NAME
.PP .PP

View file

@ -1,8 +1,8 @@
gt(1) ["gt version v0.0.3" ["User Commands"]] gt(1) ["gt version v0.0.2" ["User Commands"]]
# NAME # NAME
gt \- manual page for gt version 0.0.3 gt \- manual page for gt version 0.0.2
# 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.3 0.0.2
## AUTHOR: ## AUTHOR:

View file

@ -1,4 +1,4 @@
// Package dirs provides functions to sanitize directory and file names. // Package dirs provides functions sanitize directory names.
package dirs package dirs
import ( import (
@ -7,20 +7,14 @@ import (
"strings" "strings"
) )
const ( const sep = string(os.PathSeparator)
sep = string(os.PathSeparator)
space = " "
spacep = "%20"
newline = "\n"
newlinep = "%0A"
)
var ( var (
home = os.Getenv("HOME") home = os.Getenv("HOME")
pwd, _ = os.Getwd() pwd, _ = os.Getwd()
) )
// UnExpand returns dir after unexpanding some directory shortcuts // UnExpand returns dir after expanding some directory shortcuts
// //
// $HOME -> ~ // $HOME -> ~
// //
@ -41,7 +35,7 @@ func UnExpand(dir, workdir string) (outdir string) {
outdir = strings.Replace(outdir, home, "~", 1) outdir = strings.Replace(outdir, home, "~", 1)
outdir = PercentDecode(outdir) outdir = UnEscape(outdir)
if outdir == "" { if outdir == "" {
outdir = "/" outdir = "/"
@ -50,25 +44,17 @@ func UnExpand(dir, workdir string) (outdir string) {
return return
} }
func PercentDecode(input string) (output string) { func UnEscape(input string) string {
output = strings.ReplaceAll(input, spacep, space) return strings.ReplaceAll(input, "%20", " ")
output = strings.ReplaceAll(output, newlinep, newline)
return
} }
func PercentEncode(input string) (output string) { func cleanDir(dir, pwd string) (out string) {
output = strings.ReplaceAll(input, space, spacep)
output = strings.ReplaceAll(output, newline, newlinep)
return
}
func cleanDir(dir, pwd string) string {
if strings.HasPrefix(dir, ".") { if strings.HasPrefix(dir, ".") {
return filepath.Clean(dir) out = filepath.Clean(dir)
} else if !strings.HasPrefix(dir, sep) { } else if !strings.HasPrefix(dir, sep) {
return filepath.Join(pwd, dir) out = filepath.Join(pwd, dir)
} else {
out = dir
} }
return dir return
} }

View file

@ -1,154 +0,0 @@
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)
}
}
}

View file

@ -26,7 +26,12 @@ 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 { return f.filesize } func (f DiskFile) Filesize() int64 {
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
@ -48,29 +53,21 @@ 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(size)), info.ModTime()) name, basePath, humanize.Bytes(uint64(info.Size())), info.ModTime())
return DiskFile{ return DiskFile{
name: name, name: name,
path: actualPath, path: filepath.Join(basePath, name),
filesize: size, filesize: info.Size(),
modified: info.ModTime(), modified: info.ModTime(),
isdir: info.IsDir(), isdir: info.IsDir(),
mode: info.Mode(), mode: info.Mode(),
}, nil }, nil
} }
func FindDisk(dir string, recursive bool, fltr *filter.Filter) Files { func FindDisk(dir string, recursive bool, fltr *filter.Filter) (Files, error) {
var files Files var files Files
dir = filepath.Clean(dir) dir = filepath.Clean(dir)
if dir == "." || dir == "" { if dir == "." || dir == "" {
@ -94,7 +91,7 @@ func FindDisk(dir string, recursive bool, fltr *filter.Filter) Files {
files = append(files, readDir(dir, fltr)...) files = append(files, readDir(dir, fltr)...)
} }
return files return files, nil
} }
// isInHiddenDir checks `path` and parent directories // isInHiddenDir checks `path` and parent directories
@ -133,17 +130,10 @@ 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: size, filesize: info.Size(),
modified: info.ModTime(), modified: info.ModTime(),
isdir: info.IsDir(), isdir: info.IsDir(),
mode: info.Mode(), mode: info.Mode(),
@ -153,7 +143,7 @@ func walkDir(dir string, fltr *filter.Filter) Files {
}) })
if err != nil { if err != nil {
log.Errorf("error walking directory %s: %s", dir, err) log.Errorf("error walking directory %s: %s", dir, err)
return nil return Files{}
} }
return files return files
} }
@ -162,7 +152,7 @@ func readDir(dir string, fltr *filter.Filter) Files {
var files Files var files Files
fs, err := os.ReadDir(dir) fs, err := os.ReadDir(dir)
if err != nil { if err != nil {
return nil return Files{}
} }
for _, file := range fs { for _, file := range fs {
name := file.Name() name := file.Name()
@ -173,28 +163,17 @@ func readDir(dir string, fltr *filter.Filter) Files {
info, err := file.Info() info, err := file.Info()
if err != nil { if err != nil {
return nil return 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(actualPath, name), path: filepath.Join(path, name),
modified: info.ModTime(), modified: info.ModTime(),
filesize: size, filesize: info.Size(),
isdir: info.IsDir(), isdir: info.IsDir(),
mode: info.Mode(), mode: info.Mode(),
}) })

View file

@ -3,15 +3,11 @@ package files
import ( import (
"cmp" "cmp"
"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 {
@ -26,35 +22,6 @@ type File interface {
type Files []File type Files []File
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()
}
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
}
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
@ -74,11 +41,15 @@ func SortByModifiedReverse(a, b File) int {
} }
func SortBySize(a, b File) int { func SortBySize(a, b File) int {
return cmp.Compare(a.Filesize(), b.Filesize()) as := getSortingSize(a)
bs := getSortingSize(b)
return cmp.Compare(as, bs)
} }
func SortBySizeReverse(a, b File) int { func SortBySizeReverse(a, b File) int {
return cmp.Compare(b.Filesize(), a.Filesize()) as := getSortingSize(a)
bs := getSortingSize(b)
return cmp.Compare(bs, as)
} }
func SortByName(a, b File) int { func SortByName(a, b File) int {
@ -141,36 +112,9 @@ func doNameSort(a, b File) int {
return cmp.Compare(aname, bname) return cmp.Compare(aname, bname)
} }
func calculateDirSize(path string) int64 { func getSortingSize(f File) int64 {
var size int64 if f.IsDir() {
info, err := os.Lstat(path) return -1
if err != nil {
log.Error(err)
return 0
} }
if !info.IsDir() { return f.Filesize()
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
} }

View file

@ -5,9 +5,7 @@ import (
"io/fs" "io/fs"
"math/rand" "math/rand"
"os" "os"
"os/user"
"path/filepath" "path/filepath"
"slices"
"strings" "strings"
"time" "time"
@ -15,20 +13,16 @@ import (
"git.burning.moe/celediel/gt/internal/filter" "git.burning.moe/celediel/gt/internal/filter"
"git.burning.moe/celediel/gt/internal/prompt" "git.burning.moe/celediel/gt/internal/prompt"
"github.com/adrg/xdg"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
"github.com/moby/sys/mountinfo"
"gitlab.com/tymonx/go-formatter/formatter" "gitlab.com/tymonx/go-formatter/formatter"
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
) )
const ( const (
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"
trashInfoExt string = ".trashinfo" trashInfoExt string = ".trashinfo"
trashInfoSec string = "Trash Info" trashInfoSec string = "Trash Info"
trashInfoPath string = "Path" trashInfoPath string = "Path"
@ -40,8 +34,6 @@ 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
@ -58,139 +50,54 @@ 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 { return t.filesize } func (t TrashInfo) Filesize() int64 {
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
} }
func FindInAllTrashes(ogdir string, fltr *filter.Filter) Files { func FindTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
var files Files var files Files
outerr := filepath.WalkDir(trashdir, func(path string, dirEntry fs.DirEntry, err error) error {
for _, trash := range getAllTrashes() {
fls, err := findTrash(trash, ogdir, fltr)
if err != nil { if err != nil {
log.Errorf("error reading trash dir '%s': %s", trash, err) log.Debugf("what happened?? what is %s?", err)
continue return err
}
files = append(files, fls...)
} }
return files // ignore self, directories, and non trashinfo files
} if path == trashdir || dirEntry.IsDir() || filepath.Ext(path) != trashInfoExt {
func ConfirmRestore(confirm bool, fs Files) error {
if !confirm || prompt.YesNo(fmt.Sprintf("restore %d selected files?", len(fs))) {
log.Info("doing the thing")
restored, err := restore(fs)
if err != nil {
return fmt.Errorf("restored %d files before error %w", restored, err)
}
fmt.Fprintf(os.Stdout, "restored %d files\n", restored)
} else {
fmt.Fprintf(os.Stdout, "not doing anything\n")
}
return nil
}
func ConfirmClean(confirm bool, fs Files) error {
if prompt.YesNo(fmt.Sprintf("remove %d selected files permanently from the trash?", len(fs))) &&
(!confirm || prompt.YesNo(fmt.Sprintf("really remove all these %d selected files permanently from the trash forever??", len(fs)))) {
removed, err := remove(fs)
if err != nil {
return fmt.Errorf("removed %d files before error %w", removed, err)
}
fmt.Fprintf(os.Stdout, "removed %d files\n", removed)
} else {
fmt.Fprintf(os.Stdout, "not doing anything\n")
}
return nil
}
func ConfirmTrash(confirm bool, fs Files) error {
if !confirm || prompt.YesNo(fmt.Sprintf("trash %d selected files?", len(fs))) {
tfs := make([]string, 0, len(fs))
for _, file := range fs {
tfs = append(tfs, file.Path())
}
trashed := trashFiles(tfs)
var s string
if trashed > 1 {
s = "s"
}
fmt.Fprintf(os.Stdout, "trashed %d file%s\n", trashed, s)
} else {
fmt.Fprintf(os.Stdout, "not doing anything\n")
return nil return nil
} }
return nil
}
func findTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
log.Debugf("searching for trashinfo files in %s", trashdir)
var files Files
infodir := filepath.Join(trashdir, "info")
entries, err := os.ReadDir(infodir)
if err != nil {
return nil, err
}
for _, entry := range entries {
if entry.IsDir() || filepath.Ext(entry.Name()) != trashInfoExt {
continue
}
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) c, err := ini.Load(path)
if err != nil { if err != nil {
log.Errorf("error reading %s: %s", path, err) return err
continue
}
section := trashInfo.Section(trashInfoSec)
if section == nil {
continue
}
basepath := dirs.PercentDecode(section.Key(trashInfoPath).String())
if !strings.HasPrefix(basepath, string(os.PathSeparator)) {
root, err := getRoot(trashdir)
if err == nil {
basepath = filepath.Join(root, basepath)
}
} }
if section := c.Section(trashInfoSec); section != nil {
basepath := section.Key(trashInfoPath).String()
filename := filepath.Base(basepath) filename := filepath.Base(basepath)
trashedpath := strings.Replace(strings.Replace(path, "info", "files", 1), trashInfoExt, "", 1) trashedpath := strings.Replace(strings.Replace(path, "info", "files", 1), trashInfoExt, "", 1)
info, err := os.Lstat(trashedpath) info, err := os.Lstat(trashedpath)
if err != nil { if err != nil {
log.Errorf("error reading '%s': %s", trashedpath, err) log.Errorf("error reading %s: %s", trashedpath, err)
continue return nil
} }
s := section.Key(trashInfoDate).Value() s := section.Key(trashInfoDate).Value()
date, err := time.ParseInLocation(trashInfoDateFmt, s, time.Local) date, err := time.ParseInLocation(trashInfoDateFmt, s, time.Local)
if err != nil { if err != nil {
log.Errorf("error parsing date '%s' in trashinfo file '%s': %s", s, path, err) return err
continue
} }
if ogdir != "" && filepath.Dir(basepath) != ogdir { if ogdir != "" && filepath.Dir(basepath) != ogdir {
continue return nil
}
var size int64
if d, ok := loadedDirSizes[info.Name()]; ok {
size = d.size
} else if info.IsDir() {
size = calculateDirSize(trashedpath)
} else {
size = info.Size()
} }
if fltr.Match(info) { if fltr.Match(info) {
@ -201,66 +108,19 @@ func findTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
trashinfo: path, trashinfo: path,
trashed: date, trashed: date,
isdir: info.IsDir(), isdir: info.IsDir(),
filesize: size, filesize: info.Size(),
}) })
} }
} }
return nil
})
if outerr != nil {
return Files{}, outerr
}
return files, nil return files, nil
} }
func trashFile(filename string) error { func Restore(files Files) (restored int, err error) {
trashDir, err := getTrashDir(filename)
if err != nil {
return err
}
trashInfoFilename, outPath := getTrashFilenames(filepath.Base(filename), trashDir)
if err := os.Rename(filename, outPath); err != nil {
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{
"path": path,
"date": time.Now().Format(trashInfoDateFmt),
})
if err != nil {
return err
}
if err := os.WriteFile(trashInfoFilename, []byte(trashInfo), noExecuteUserPerm); err != nil {
return err
}
return nil
}
func trashFiles(files []string) (trashed int) {
for _, file := range files {
if err := trashFile(file); err != nil {
log.Errorf("cannot trash '%s': %s", file, err)
continue
}
trashed++
}
return
}
func restore(files Files) (restored int, err error) {
for _, maybeFile := range files { for _, maybeFile := range files {
file, ok := maybeFile.(TrashInfo) file, ok := maybeFile.(TrashInfo)
if !ok { if !ok {
@ -268,7 +128,7 @@ func restore(files Files) (restored int, err error) {
} }
var cancel bool var cancel bool
outpath := dirs.PercentDecode(file.ogpath) outpath := dirs.UnEscape(file.ogpath)
log.Infof("restoring %s back to %s\n", file.name, outpath) log.Infof("restoring %s back to %s\n", file.name, outpath)
if _, e := os.Lstat(outpath); e == nil { if _, e := os.Lstat(outpath); e == nil {
outpath, cancel = prompt.NewPath(outpath) outpath, cancel = prompt.NewPath(outpath)
@ -279,7 +139,7 @@ func restore(files Files) (restored int, err error) {
} }
basedir := filepath.Dir(outpath) basedir := filepath.Dir(outpath)
if _, e := os.Lstat(basedir); e != nil { if _, e := os.Stat(basedir); e != nil {
if err = os.MkdirAll(basedir, executePerm); err != nil { if err = os.MkdirAll(basedir, executePerm); err != nil {
return restored, err return restored, err
} }
@ -298,7 +158,21 @@ func restore(files Files) (restored int, err error) {
return restored, err return restored, err
} }
func remove(files Files) (removed int, err error) { func ConfirmRestore(confirm bool, fs Files) error {
if !confirm || prompt.YesNo(fmt.Sprintf("restore %d selected files?", len(fs))) {
log.Info("doing the thing")
restored, err := Restore(fs)
if err != nil {
return fmt.Errorf("restored %d files before error %w", restored, err)
}
fmt.Fprintf(os.Stdout, "restored %d files\n", restored)
} else {
fmt.Fprintf(os.Stdout, "not doing anything\n")
}
return nil
}
func Remove(files Files) (removed int, err error) {
for _, maybeFile := range files { for _, maybeFile := range files {
file, ok := maybeFile.(TrashInfo) file, ok := maybeFile.(TrashInfo)
if !ok { if !ok {
@ -323,7 +197,82 @@ func remove(files Files) (removed int, err error) {
return removed, err return removed, err
} }
func randomString(length int) string { func ConfirmClean(confirm bool, fs Files) error {
if prompt.YesNo(fmt.Sprintf("remove %d selected files permanently from the trash?", len(fs))) &&
(!confirm || prompt.YesNo(fmt.Sprintf("really remove all these %d selected files permanently from the trash forever??", len(fs)))) {
removed, err := Remove(fs)
if err != nil {
return fmt.Errorf("removed %d files before error %w", removed, err)
}
fmt.Fprintf(os.Stdout, "removed %d files\n", removed)
} else {
fmt.Fprintf(os.Stdout, "not doing anything\n")
}
return nil
}
func TrashFile(trashDir, name string) error {
trashinfoFilename, outPath := ensureUniqueName(filepath.Base(name), trashDir)
if err := os.Rename(name, outPath); err != nil {
if strings.Contains(err.Error(), "invalid cross-device link") {
// TODO: use $topdir/.Trash as per XDG spec
// TODO: maybe figure out if filesystem is truly different or is a btrfs subvolume
return err
}
return err
}
trashInfo, err := formatter.Format(trashInfoTemplate, formatter.Named{
"path": name,
"date": time.Now().Format(trashInfoDateFmt),
})
if err != nil {
return err
}
if err := os.WriteFile(trashinfoFilename, []byte(trashInfo), noExecuteUserPerm); err != nil {
return err
}
return nil
}
func TrashFiles(trashDir string, files ...string) (trashed int) {
for _, file := range files {
if err := TrashFile(trashDir, file); err != nil {
log.Errorf("cannot trash '%s': %s", file, err)
continue
}
trashed++
}
return trashed
}
func ConfirmTrash(confirm bool, fs Files, trashDir string) error {
if !confirm || prompt.YesNo(fmt.Sprintf("trash %d selected files?", len(fs))) {
tfs := make([]string, 0, len(fs))
for _, file := range fs {
log.Debugf("gonna trash %s", file.Path())
tfs = append(tfs, file.Path())
}
trashed := TrashFiles(trashDir, tfs...)
var files string
if trashed == 1 {
files = "file"
} else {
files = "files"
}
fmt.Fprintf(os.Stdout, "trashed %d %s\n", trashed, files)
} else {
fmt.Fprintf(os.Stdout, "not doing anything\n")
return nil
}
return nil
}
func randomFilename(length int) string {
out := strings.Builder{} out := strings.Builder{}
for range length { for range length {
out.WriteByte(randomChar()) out.WriteByte(randomChar())
@ -336,17 +285,17 @@ func randomChar() byte {
return chars[rand.Intn(len(chars))] return chars[rand.Intn(len(chars))]
} }
func getTrashFilenames(filename, trashDir string) (string, string) { func ensureUniqueName(filename, trashDir string) (string, string) {
var ( var (
filedir = filepath.Join(trashDir, "files") filedir = filepath.Join(trashDir, "files")
infodir = filepath.Join(trashDir, "info") infodir = filepath.Join(trashDir, "info")
) )
info := filepath.Join(infodir, filename+trashInfoExt) info := filepath.Join(infodir, filename+trashInfoExt)
if _, err := os.Lstat(info); os.IsNotExist(err) { if _, err := os.Stat(info); os.IsNotExist(err) {
// doesn't exist, so use it // doesn't exist, so use it
path := filepath.Join(filedir, filename) path := filepath.Join(filedir, filename)
return dirs.PercentEncode(info), dirs.PercentEncode(path) return info, path
} }
// otherwise, try random suffixes until one works // otherwise, try random suffixes until one works
@ -354,106 +303,12 @@ func getTrashFilenames(filename, trashDir string) (string, string) {
var tries int var tries int
for { for {
tries++ tries++
rando := randomString(randomStrLength) rando := randomFilename(randomStrLength)
newInfo := filepath.Join(infodir, filename+rando+trashInfoExt) newName := filepath.Join(infodir, filename+rando+trashInfoExt)
newFile := filepath.Join(filedir, filename+rando) if _, err := os.Stat(newName); os.IsNotExist(err) {
_, infoErr := os.Lstat(newInfo)
_, fileErr := os.Lstat(newFile)
if os.IsNotExist(infoErr) && os.IsNotExist(fileErr) {
path := filepath.Join(filedir, filename+rando) path := filepath.Join(filedir, filename+rando)
log.Debugf("settled on random name %s%s on the %s try", filename, rando, humanize.Ordinal(tries)) log.Debugf("settled on random name %s%s on the %s try", filename, rando, humanize.Ordinal(tries))
return dirs.PercentEncode(newInfo), dirs.PercentEncode(path) return newName, path
} }
} }
} }
func getTrashDir(filename string) (string, error) {
root, err := getRoot(filename)
if err != nil {
return "", err
}
var trashDir string
if strings.Contains(filename, xdg.Home) {
trashDir = filepath.Join(xdg.DataHome, trashName[1:])
} else {
trashDir = filepath.Clean(filepath.Join(root, trashName))
}
if _, err := os.Lstat(trashDir); err != nil {
usr, _ := user.Current()
trashDir += "-" + usr.Uid
if err := os.Mkdir(trashDir, executePerm); err != nil {
return "", fmt.Errorf("%s%s does not exist and creation of %s failed", root, trashName, trashDir)
}
}
if link, err := os.Readlink(trashDir); err == nil && link != "" {
return "", fmt.Errorf("trash dir %s is a symbolic link", trashDir)
}
return trashDir, nil
}
func getRoot(path string) (string, error) {
var roots []string
// populate a list of mountpoints on the system
_, err := mountinfo.GetMounts(func(i *mountinfo.Info) (skip bool, stop bool) {
roots = append(roots, i.Mountpoint)
return false, false
})
if err != nil {
log.Error(err)
}
var depth uint8 = 1 // 255 seems a reasonable recursion maximum
current := path
// recursively search upwards by using filepath.Clean and ..
for {
if depth == 0 {
return path, fmt.Errorf("reached max depth getting root of %s", path)
}
current = filepath.Clean(current)
if current == string(os.PathSeparator) || slices.Contains(roots, current) {
return current, nil
}
current += string(os.PathSeparator) + ".."
depth++
}
}
func getAllTrashes() []string {
trashes := []string{homeTrash}
usr, err := user.Current()
if err != nil {
log.Error(err)
}
_, err = mountinfo.GetMounts(func(mount *mountinfo.Info) (skip bool, stop bool) {
point := mount.Mountpoint
trashDir := filepath.Clean(filepath.Join(point, trashName))
userTrashDir := trashDir + "-" + usr.Uid
if _, err := os.Lstat(trashDir); err == nil {
trashes = append(trashes, trashDir)
}
if _, err := os.Lstat(userTrashDir); err == nil {
trashes = append(trashes, userTrashDir)
}
return false, false
})
if err != nil {
log.Errorf("error reading mounts: %s", err)
return nil
}
return trashes
}

View file

@ -73,7 +73,6 @@ 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
@ -87,8 +86,8 @@ type model struct {
fltrfiles files.Files fltrfiles files.Files
} }
func newModel(fls files.Files, selectall, readonly, once bool, workdir string, mode modes.Mode) model { func newModel(fls []files.File, selectall, readonly, once bool, workdir string, mode modes.Mode) (m model) {
m := model{ m = model{
keys: defaultKeyMap(), keys: defaultKeyMap(),
readonly: readonly, readonly: readonly,
once: once, once: once,
@ -96,7 +95,6 @@ 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()
@ -118,7 +116,7 @@ func newModel(fls files.Files, selectall, readonly, once bool, workdir string, m
m.selectAll() m.selectAll()
} }
return m return
} }
type keyMap struct { type keyMap struct {
@ -333,8 +331,6 @@ func (m model) header() string {
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 {
@ -342,23 +338,24 @@ 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, selectedSize) left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.fltrfiles), dot, humanize.Bytes(uint64(m.selectsize)))
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 (%s)", filtered, len(m.fltrfiles), totalSize) right = fmt.Sprintf(" Showing%s %d files in trash", filtered, len(m.fltrfiles))
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, selectedSize) left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.fltrfiles), dot, humanize.Bytes(uint64(m.selectsize)))
} }
spacerWidth = (m.termwidth - lipgloss.Width(right) - lipgloss.Width(left)) + 1 // offset of 2 again because of table border
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
} }
@ -665,14 +662,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() {
name += string(os.PathSeparator) size = bar
} } else {
size = humanize.Bytes(uint64(file.Filesize())) size = humanize.Bytes(uint64(file.Filesize()))
}
return table.Row{ return table.Row{
dirs.PercentDecode(name), dirs.UnEscape(file.Name()),
dirs.UnExpand(filepath.Dir(file.Path()), workdir), dirs.UnExpand(filepath.Dir(file.Path()), workdir),
time, time,
size, size,

View file

@ -52,7 +52,7 @@ func NewPath(path string) (string, bool) {
Run(); err != nil { Run(); err != nil {
return path, false return path, false
} }
if _, e := os.Lstat(path); e != nil { if _, e := os.Stat(path); e != nil {
return path, false return path, false
} }
default: default:

View file

@ -1,5 +1,5 @@
binary := "gt" binary := "gt"
version := "0.0.3" version := "0.0.2"
build_dir := "bin" build_dir := "bin"
dist_dir := "dist" dist_dir := "dist"
cmd := "." cmd := "."

75
main.go
View file

@ -4,7 +4,6 @@ package main
import ( import (
"fmt" "fmt"
"io/fs" "io/fs"
"math"
"os" "os"
"path/filepath" "path/filepath"
"slices" "slices"
@ -15,7 +14,6 @@ import (
"git.burning.moe/celediel/gt/internal/filter" "git.burning.moe/celediel/gt/internal/filter"
"git.burning.moe/celediel/gt/internal/interactive" "git.burning.moe/celediel/gt/internal/interactive"
"git.burning.moe/celediel/gt/internal/interactive/modes" "git.burning.moe/celediel/gt/internal/interactive/modes"
"golang.org/x/term"
"github.com/adrg/xdg" "github.com/adrg/xdg"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
@ -25,7 +23,7 @@ import (
const ( const (
appname string = "gt" appname string = "gt"
appsubtitle string = "xdg trash cli" appsubtitle string = "xdg trash cli"
appversion string = "v0.0.3" appversion string = "v0.0.2"
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.
@ -36,8 +34,8 @@ See gt(1) for more information.`
) )
var ( var (
fltr *filter.Filter
loglvl string loglvl string
fltr *filter.Filter
onArg, beforeArg, afterArg string onArg, beforeArg, afterArg string
globArg, patternArg string globArg, patternArg string
unGlobArg, unPatternArg string unGlobArg, unPatternArg string
@ -47,13 +45,10 @@ var (
askconfirm, all bool askconfirm, all bool
workdir, ogdir cli.Path workdir, ogdir cli.Path
recursive bool recursive bool
isTerminal bool
trashDir = filepath.Join(xdg.DataHome, "Trash")
beforeAll = func(_ *cli.Context) error { beforeAll = func(_ *cli.Context) error {
if term.IsTerminal(int(os.Stdout.Fd())) && term.IsTerminal(int(os.Stdin.Fd())) {
isTerminal = true
}
// setup log // setup log
log.SetReportTimestamp(true) log.SetReportTimestamp(true)
log.SetTimeFormat(time.TimeOnly) log.SetTimeFormat(time.TimeOnly)
@ -67,19 +62,19 @@ var (
log.Errorf("unknown log level '%s' (possible values: debug, info, warn, error, fatal, default: warn)", loglvl) log.Errorf("unknown log level '%s' (possible values: debug, info, warn, error, fatal, default: warn)", loglvl)
} }
if !isTerminal { // ensure trash directories exist
log.SetLevel(math.MaxInt32) if _, e := os.Stat(trashDir); os.IsNotExist(e) {
} if err := os.Mkdir(trashDir, executePerm); err != nil {
// ensure personal trash directories exist
homeTrash := filepath.Join(xdg.DataHome, "Trash")
if _, e := os.Lstat(filepath.Join(homeTrash, "info")); os.IsNotExist(e) {
if err := os.MkdirAll(filepath.Join(homeTrash, "info"), executePerm); err != nil {
return err return err
} }
} }
if _, e := os.Lstat(filepath.Join(homeTrash, "files")); os.IsNotExist(e) { if _, e := os.Stat(filepath.Join(trashDir, "info")); os.IsNotExist(e) {
if err := os.MkdirAll(filepath.Join(homeTrash, "files"), executePerm); err != nil { if err := os.Mkdir(filepath.Join(trashDir, "info"), executePerm); err != nil {
return err
}
}
if _, e := os.Stat(filepath.Join(trashDir, "files")); os.IsNotExist(e) {
if err := os.Mkdir(filepath.Join(trashDir, "files"), executePerm); err != nil {
return err return err
} }
} }
@ -112,8 +107,10 @@ var (
mode modes.Mode mode modes.Mode
err error err error
) )
infiles, err = files.FindTrash(trashDir, ogdir, fltr)
infiles = files.FindInAllTrashes(ogdir, fltr) if err != nil {
return err
}
if len(infiles) <= 0 { if len(infiles) <= 0 {
var msg string var msg string
if fltr.Blank() { if fltr.Blank() {
@ -124,11 +121,6 @@ var (
fmt.Fprintln(os.Stdout, msg) fmt.Fprintln(os.Stdout, msg)
return nil return nil
} }
if !isTerminal {
fmt.Fprint(os.Stdout, infiles.String())
return nil
}
selected, mode, err = interactive.Select(infiles, false, false, workdir, modes.Interactive) selected, mode, err = interactive.Select(infiles, false, false, workdir, modes.Interactive)
if err != nil { if err != nil {
return err return err
@ -166,7 +158,7 @@ var (
} }
filesToTrash = append(filesToTrash, file) filesToTrash = append(filesToTrash, file)
} }
return files.ConfirmTrash(askconfirm, filesToTrash) return files.ConfirmTrash(askconfirm, filesToTrash, trashDir)
} }
beforeCommands = func(ctx *cli.Context) (err error) { beforeCommands = func(ctx *cli.Context) (err error) {
@ -195,7 +187,6 @@ var (
} }
after = func(_ *cli.Context) error { after = func(_ *cli.Context) error {
files.WriteDirectorySizes()
return nil return nil
} }
@ -220,7 +211,10 @@ var (
// if none of the args were files, then find files based on filter // if none of the args were files, then find files based on filter
if len(filesToTrash) == 0 { if len(filesToTrash) == 0 {
fls := files.FindDisk(workdir, recursive, fltr) fls, err := files.FindDisk(workdir, recursive, fltr)
if err != nil {
return err
}
if len(fls) == 0 { if len(fls) == 0 {
fmt.Fprintln(os.Stdout, "no files to trash") fmt.Fprintln(os.Stdout, "no files to trash")
return nil return nil
@ -237,7 +231,7 @@ var (
return nil return nil
} }
return files.ConfirmTrash(askconfirm, selected) return files.ConfirmTrash(askconfirm, selected, trashDir)
}, },
} }
@ -248,7 +242,9 @@ var (
Flags: slices.Concat(listFlags, trashedFlags, filterFlags), Flags: slices.Concat(listFlags, trashedFlags, filterFlags),
Before: beforeCommands, Before: beforeCommands,
Action: func(_ *cli.Context) error { Action: func(_ *cli.Context) error {
fls := files.FindInAllTrashes(ogdir, fltr) log.Debugf("searching in directory %s for files", trashDir)
fls, err := files.FindTrash(trashDir, ogdir, fltr)
var msg string var msg string
log.Debugf("filter '%s' is blank? %t in %s", fltr, fltr.Blank(), ogdir) log.Debugf("filter '%s' is blank? %t in %s", fltr, fltr.Blank(), ogdir)
@ -261,11 +257,8 @@ var (
if len(fls) == 0 { if len(fls) == 0 {
fmt.Fprintln(os.Stdout, msg) fmt.Fprintln(os.Stdout, msg)
return nil return nil
} } else if err != nil {
return err
if !isTerminal {
fmt.Fprint(os.Stdout, fls.String())
return nil
} }
return interactive.Show(fls, noInterArg, workdir) return interactive.Show(fls, noInterArg, workdir)
@ -280,10 +273,14 @@ var (
Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags), Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags),
Before: beforeCommands, Before: beforeCommands,
Action: func(_ *cli.Context) error { Action: func(_ *cli.Context) error {
fls := files.FindInAllTrashes(ogdir, fltr) log.Debugf("searching in directory %s for files", trashDir)
fls, err := files.FindTrash(trashDir, ogdir, fltr)
if len(fls) == 0 { if len(fls) == 0 {
fmt.Fprintln(os.Stdout, "no files to restore") fmt.Fprintln(os.Stdout, "no files to restore")
return nil return nil
} else if err != nil {
return err
} }
selected, _, err := interactive.Select(fls, all, all, workdir, modes.Restoring) selected, _, err := interactive.Select(fls, all, all, workdir, modes.Restoring)
@ -307,10 +304,12 @@ var (
Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags), Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags),
Before: beforeCommands, Before: beforeCommands,
Action: func(_ *cli.Context) error { Action: func(_ *cli.Context) error {
fls := files.FindInAllTrashes(ogdir, fltr) fls, err := files.FindTrash(trashDir, ogdir, fltr)
if len(fls) == 0 { if len(fls) == 0 {
fmt.Fprintln(os.Stdout, "no files to clean") fmt.Fprintln(os.Stdout, "no files to clean")
return nil return nil
} else if err != nil {
return err
} }
selected, _, err := interactive.Select(fls, all, all, workdir, modes.Cleaning) selected, _, err := interactive.Select(fls, all, all, workdir, modes.Cleaning)