From 4d929160d15b336220d6a269db0294b00782647f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Tue, 18 Jun 2024 17:02:49 -0700 Subject: [PATCH] add original-path flag for list/restore/clean files in trash --- internal/trash/trash.go | 6 +++++- main.go | 35 ++++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/internal/trash/trash.go b/internal/trash/trash.go index 0462fdc..ad1c77a 100644 --- a/internal/trash/trash.go +++ b/internal/trash/trash.go @@ -77,7 +77,7 @@ func (is Infos) Show(width int) { 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 { if err != nil { 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 } + if ogdir != "" && filepath.Dir(basepath) != ogdir { + return nil + } + if f.Match(filename, date) { log.Debugf("%s: deleted on %s", filename, date.Format(trash_info_date_fmt)) files = append(files, Info{ diff --git a/main.go b/main.go index d336a18..f318f3e 100644 --- a/main.go +++ b/main.go @@ -26,13 +26,13 @@ const ( ) var ( - loglvl string - f *filter.Filter - o, b, a, g, p string - ung, unp string - workdir string - recursive bool - termwidth int + loglvl string + f *filter.Filter + o, b, a, g, p string + ung, unp string + workdir, ogdir cli.Path + recursive bool + termwidth int trashDir = filepath.Join(xdg.DataHome, "Trash") @@ -111,13 +111,13 @@ var ( Name: "list", Aliases: []string{"ls"}, Usage: "list trashed files", - Flags: slices.Concat(filter_flags), + Flags: slices.Concat(alreadyintrash_flags, filter_flags), Before: before_commands, Action: func(ctx *cli.Context) error { log.Debugf("searching in directory %s for files", trashDir) // look for files - files, err := trash.FindFiles(trashDir, f) + files, err := trash.FindFiles(trashDir, ogdir, f) var msg string if f.Blank() { @@ -144,13 +144,13 @@ var ( Name: "restore", Aliases: []string{"re"}, Usage: "restore a trashed file or files", - Flags: slices.Concat(filter_flags), + Flags: slices.Concat(alreadyintrash_flags, filter_flags), Before: before_commands, Action: func(ctx *cli.Context) error { log.Debugf("searching in directory %s for files", trashDir) // look for files - files, err := trash.FindFiles(trashDir, f) + files, err := trash.FindFiles(trashDir, ogdir, f) if len(files) == 0 { fmt.Println("no files to restore") return nil @@ -178,10 +178,10 @@ var ( Name: "clean", Aliases: []string{"cl"}, Usage: "clean files from trash", - Flags: slices.Concat(filter_flags), + Flags: slices.Concat(alreadyintrash_flags, filter_flags), Before: before_commands, Action: func(ctx *cli.Context) error { - files, err := trash.FindFiles(trashDir, f) + files, err := trash.FindFiles(trashDir, ogdir, f) if len(files) == 0 { fmt.Println("no files to clean") return nil @@ -276,6 +276,15 @@ var ( 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() {