trash command: if args are existing files, don't find files based on filter, just trash args

This commit is contained in:
Lilian Jónsdóttir 2024-06-21 21:52:59 -07:00
parent 7fdb8052c4
commit 379428509c
2 changed files with 60 additions and 10 deletions

View file

@ -10,6 +10,7 @@ import (
"git.burning.moe/celediel/gt/internal/filter" "git.burning.moe/celediel/gt/internal/filter"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
"github.com/dustin/go-humanize"
) )
type File struct { 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) Filesize() int64 { return f.filesize }
func (f File) IsDir() bool { return f.isdir } 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) { func Find(dir string, recursive bool, f *filter.Filter) (files Files, err error) {
if dir == "." || dir == "" { if dir == "." || dir == "" {
var d string var d string

42
main.go
View file

@ -93,6 +93,14 @@ var (
return 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 { after = func(ctx *cli.Context) error {
return nil return nil
} }
@ -102,26 +110,41 @@ var (
Aliases: []string{"tr"}, Aliases: []string{"tr"},
Usage: "Trash a file or files", Usage: "Trash a file or files",
Flags: slices.Concat(trash_flags, filter_flags), Flags: slices.Concat(trash_flags, filter_flags),
Before: before_commands, Before: before_trash,
Action: func(ctx *cli.Context) error { Action: func(ctx *cli.Context) error {
fls, err := files.Find(workdir, recursive, f) var files_to_trash files.Files
if err != nil { for _, arg := range ctx.Args().Slice() {
return err 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") // if none of the args were files, then process find files based on filter
return nil 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) 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 { if err != nil {
return err return err
} }
var selected files.Files var selected files.Files
for _, i := range indices { for _, i := range indices {
selected = append(selected, fls[i]) selected = append(selected, files_to_trash[i])
} }
if len(selected) <= 0 { if len(selected) <= 0 {
@ -146,7 +169,6 @@ var (
} }
if len(ctx.Args().Slice()) != 0 { if len(ctx.Args().Slice()) != 0 {
f.AddFileNames(ctx.Args().Slice()...)
return do_trash.Action(ctx) return do_trash.Action(ctx)
} else { } else {
return interactive_mode() return interactive_mode()