From 379428509c5cf420c1152266aa994b1142b5f061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Fri, 21 Jun 2024 21:52:59 -0700 Subject: [PATCH] trash command: if args are existing files, don't find files based on filter, just trash args --- internal/files/files.go | 28 +++++++++++++++++++++++++++ main.go | 42 +++++++++++++++++++++++++++++++---------- 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/internal/files/files.go b/internal/files/files.go index ac629d4..d323117 100644 --- a/internal/files/files.go +++ b/internal/files/files.go @@ -10,6 +10,7 @@ import ( "git.burning.moe/celediel/gt/internal/filter" "github.com/charmbracelet/log" + "github.com/dustin/go-humanize" ) type File struct { @@ -28,6 +29,33 @@ func (f File) Modified() time.Time { return f.modified } func (f File) Filesize() int64 { return f.filesize } func (f File) IsDir() bool { return f.isdir } +func New(path string) (File, error) { + info, err := os.Stat(path) + if err != nil { + return File{}, err + } + + abs, err := filepath.Abs(path) + if err != nil { + log.Errorf("couldn't get absolute path for %s", path) + abs = path + } + + name := filepath.Base(abs) + base_path := filepath.Dir(abs) + + log.Debugf("%s (base:%s) (size:%s) (modified:%s) exists", + name, base_path, humanize.Bytes(uint64(info.Size())), info.ModTime()) + + return File{ + name: name, + path: base_path, + filesize: info.Size(), + modified: info.ModTime(), + isdir: info.IsDir(), + }, nil +} + func Find(dir string, recursive bool, f *filter.Filter) (files Files, err error) { if dir == "." || dir == "" { var d string diff --git a/main.go b/main.go index 81a3743..6defd98 100644 --- a/main.go +++ b/main.go @@ -93,6 +93,14 @@ var ( return } + before_trash = func(_ *cli.Context) (err error) { + if f == nil { + f, err = filter.New(o, b, a, g, p, ung, unp, fo, do, ih) + } + log.Debugf("filter: %s", f.String()) + return + } + after = func(ctx *cli.Context) error { return nil } @@ -102,26 +110,41 @@ var ( Aliases: []string{"tr"}, Usage: "Trash a file or files", Flags: slices.Concat(trash_flags, filter_flags), - Before: before_commands, + Before: before_trash, Action: func(ctx *cli.Context) error { - fls, err := files.Find(workdir, recursive, f) - if err != nil { - return err + var files_to_trash files.Files + for _, arg := range ctx.Args().Slice() { + file, e := files.New(arg) + if e != nil { + log.Debugf("%s wasn't really a file", arg) + f.AddFileName(arg) + continue + } + files_to_trash = append(files_to_trash, file) } - if len(fls) == 0 { - fmt.Println("no files to trash") - return nil + + // if none of the args were files, then process find files based on filter + if len(files_to_trash) == 0 { + fls, err := files.Find(workdir, recursive, f) + if err != nil { + return err + } + if len(fls) == 0 { + fmt.Println("no files to trash") + return nil + } + files_to_trash = append(files_to_trash, fls...) } log.Debugf("what is workdir? it's %s", workdir) - indices, err := tables.FilesTable(fls, termwidth, termheight, false, !f.Blank(), workdir) + indices, err := tables.FilesTable(files_to_trash, termwidth, termheight, false, !f.Blank(), workdir) if err != nil { return err } var selected files.Files for _, i := range indices { - selected = append(selected, fls[i]) + selected = append(selected, files_to_trash[i]) } if len(selected) <= 0 { @@ -146,7 +169,6 @@ var ( } if len(ctx.Args().Slice()) != 0 { - f.AddFileNames(ctx.Args().Slice()...) return do_trash.Action(ctx) } else { return interactive_mode()