add original-path flag for list/restore/clean files in trash

This commit is contained in:
Lilian Jónsdóttir 2024-06-18 17:02:49 -07:00
parent dddc6f6d33
commit 4d929160d1
2 changed files with 27 additions and 14 deletions

View file

@ -77,7 +77,7 @@ func (is Infos) Show(width int) {
fmt.Println(is.Table(width)) fmt.Println(is.Table(width))
} }
func FindFiles(trashdir string, f *filter.Filter) (files Infos, outerr error) { func FindFiles(trashdir, ogdir string, f *filter.Filter) (files Infos, outerr error) {
outerr = filepath.WalkDir(trashdir, func(path string, d fs.DirEntry, err error) error { outerr = filepath.WalkDir(trashdir, func(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
log.Debugf("what happened?? what is %s?", err) log.Debugf("what happened?? what is %s?", err)
@ -107,6 +107,10 @@ func FindFiles(trashdir string, f *filter.Filter) (files Infos, outerr error) {
return err return err
} }
if ogdir != "" && filepath.Dir(basepath) != ogdir {
return nil
}
if f.Match(filename, date) { if f.Match(filename, date) {
log.Debugf("%s: deleted on %s", filename, date.Format(trash_info_date_fmt)) log.Debugf("%s: deleted on %s", filename, date.Format(trash_info_date_fmt))
files = append(files, Info{ files = append(files, Info{

35
main.go
View file

@ -26,13 +26,13 @@ const (
) )
var ( var (
loglvl string loglvl string
f *filter.Filter f *filter.Filter
o, b, a, g, p string o, b, a, g, p string
ung, unp string ung, unp string
workdir string workdir, ogdir cli.Path
recursive bool recursive bool
termwidth int termwidth int
trashDir = filepath.Join(xdg.DataHome, "Trash") trashDir = filepath.Join(xdg.DataHome, "Trash")
@ -111,13 +111,13 @@ var (
Name: "list", Name: "list",
Aliases: []string{"ls"}, Aliases: []string{"ls"},
Usage: "list trashed files", Usage: "list trashed files",
Flags: slices.Concat(filter_flags), Flags: slices.Concat(alreadyintrash_flags, filter_flags),
Before: before_commands, Before: before_commands,
Action: func(ctx *cli.Context) error { Action: func(ctx *cli.Context) error {
log.Debugf("searching in directory %s for files", trashDir) log.Debugf("searching in directory %s for files", trashDir)
// look for files // look for files
files, err := trash.FindFiles(trashDir, f) files, err := trash.FindFiles(trashDir, ogdir, f)
var msg string var msg string
if f.Blank() { if f.Blank() {
@ -144,13 +144,13 @@ var (
Name: "restore", Name: "restore",
Aliases: []string{"re"}, Aliases: []string{"re"},
Usage: "restore a trashed file or files", Usage: "restore a trashed file or files",
Flags: slices.Concat(filter_flags), Flags: slices.Concat(alreadyintrash_flags, filter_flags),
Before: before_commands, Before: before_commands,
Action: func(ctx *cli.Context) error { Action: func(ctx *cli.Context) error {
log.Debugf("searching in directory %s for files", trashDir) log.Debugf("searching in directory %s for files", trashDir)
// look for files // look for files
files, err := trash.FindFiles(trashDir, f) files, err := trash.FindFiles(trashDir, ogdir, f)
if len(files) == 0 { if len(files) == 0 {
fmt.Println("no files to restore") fmt.Println("no files to restore")
return nil return nil
@ -178,10 +178,10 @@ var (
Name: "clean", Name: "clean",
Aliases: []string{"cl"}, Aliases: []string{"cl"},
Usage: "clean files from trash", Usage: "clean files from trash",
Flags: slices.Concat(filter_flags), Flags: slices.Concat(alreadyintrash_flags, filter_flags),
Before: before_commands, Before: before_commands,
Action: func(ctx *cli.Context) error { Action: func(ctx *cli.Context) error {
files, err := trash.FindFiles(trashDir, f) files, err := trash.FindFiles(trashDir, ogdir, f)
if len(files) == 0 { if len(files) == 0 {
fmt.Println("no files to clean") fmt.Println("no files to clean")
return nil return nil
@ -276,6 +276,15 @@ var (
Destination: &workdir, Destination: &workdir,
}, },
} }
alreadyintrash_flags = []cli.Flag{
&cli.PathFlag{
Name: "original-path",
Usage: "restore files trashed from this `DIRECTORY`",
Aliases: []string{"O"},
Destination: &ogdir,
},
}
) )
func main() { func main() {