add flag to clean/restore all with confirmation

also swap case on date filter flags
This commit is contained in:
Lilian Jónsdóttir 2024-07-16 11:12:37 -07:00
parent 5c02d96dc2
commit 2d0d5bad1e
2 changed files with 36 additions and 26 deletions

View file

@ -180,7 +180,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd var cmd tea.Cmd
if m.once { if m.once {
return m.quit(true) return m.quit(m.readonly)
} }
switch msg := msg.(type) { switch msg := msg.(type) {

60
main.go
View file

@ -34,7 +34,7 @@ var (
sm, lg string sm, lg string
ung, unp string ung, unp string
fo, do, sh, ni bool fo, do, sh, ni bool
askconfirm bool askconfirm, all bool
workdir, ogdir cli.Path workdir, ogdir cli.Path
recursive bool recursive bool
termwidth int termwidth int
@ -90,7 +90,7 @@ var (
) )
if f == nil { if f == nil {
md, e := getMode(m) md, e := parseMode(m)
if e != nil { if e != nil {
return e return e
} }
@ -109,7 +109,7 @@ var (
} }
files_to_trash = append(files_to_trash, file) files_to_trash = append(files_to_trash, file)
} }
return confirmTrash(files_to_trash) return confirmTrash(askconfirm, files_to_trash)
} else { } else {
return interactiveMode() return interactiveMode()
} }
@ -118,7 +118,7 @@ var (
beforeCommands = func(ctx *cli.Context) (err error) { beforeCommands = func(ctx *cli.Context) (err error) {
// setup filter // setup filter
if f == nil { if f == nil {
md, e := getMode(m) md, e := parseMode(m)
if e != nil { if e != nil {
return e return e
} }
@ -130,7 +130,7 @@ var (
beforeTrash = func(_ *cli.Context) (err error) { beforeTrash = func(_ *cli.Context) (err error) {
if f == nil { if f == nil {
md, e := getMode(m) md, e := parseMode(m)
if e != nil { if e != nil {
return e return e
} }
@ -187,7 +187,7 @@ var (
return nil return nil
} }
return confirmTrash(selected) return confirmTrash(askconfirm, selected)
}, },
} }
@ -229,7 +229,7 @@ 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(alreadyintrashFlags, filterFlags), Flags: slices.Concat(cleanRestoreFlags, alreadyintrashFlags, filterFlags),
Before: beforeCommands, Before: beforeCommands,
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)
@ -243,7 +243,7 @@ var (
return err return err
} }
selected, _, err := tables.Select(fls, termwidth, termheight, false, !f.Blank(), ni, workdir, modes.Restoring) selected, _, err := tables.Select(fls, termwidth, termheight, false, all, all, workdir, modes.Restoring)
if err != nil { if err != nil {
return err return err
} }
@ -252,7 +252,7 @@ var (
return nil return nil
} }
return confirmRestore(selected) return confirmRestore(askconfirm || all, selected)
}, },
} }
@ -260,7 +260,7 @@ var (
Name: "clean", Name: "clean",
Aliases: []string{"cl"}, Aliases: []string{"cl"},
Usage: "Clean files from trash", Usage: "Clean files from trash",
Flags: slices.Concat(alreadyintrashFlags, filterFlags), Flags: slices.Concat(cleanRestoreFlags, alreadyintrashFlags, filterFlags),
Before: beforeCommands, Before: beforeCommands,
Action: func(ctx *cli.Context) error { Action: func(ctx *cli.Context) error {
fls, err := files.FindTrash(trashDir, ogdir, f) fls, err := files.FindTrash(trashDir, ogdir, f)
@ -271,7 +271,7 @@ var (
return err return err
} }
selected, _, err := tables.Select(fls, termwidth, termheight, false, !f.Blank(), ni, workdir, modes.Cleaning) selected, _, err := tables.Select(fls, termwidth, termheight, false, all, all, workdir, modes.Cleaning)
if err != nil { if err != nil {
return err return err
} }
@ -280,7 +280,7 @@ var (
return nil return nil
} }
return confirmClean(selected) return confirmClean(askconfirm, selected)
}, },
} }
@ -330,19 +330,19 @@ var (
&cli.StringFlag{ &cli.StringFlag{
Name: "on", Name: "on",
Usage: "operate on files modified on `DATE`", Usage: "operate on files modified on `DATE`",
Aliases: []string{"o"}, Aliases: []string{"O"},
Destination: &o, Destination: &o,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "after", Name: "after",
Usage: "operate on files modified before `DATE`", Usage: "operate on files modified before `DATE`",
Aliases: []string{"a"}, Aliases: []string{"A"},
Destination: &a, Destination: &a,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "before", Name: "before",
Usage: "operate on files modified after `DATE`", Usage: "operate on files modified after `DATE`",
Aliases: []string{"b"}, Aliases: []string{"B"},
Destination: &b, Destination: &b,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
@ -407,7 +407,7 @@ var (
&cli.PathFlag{ &cli.PathFlag{
Name: "original-path", Name: "original-path",
Usage: "operate on files trashed from this `DIRECTORY`", Usage: "operate on files trashed from this `DIRECTORY`",
Aliases: []string{"O"}, Aliases: []string{"o"},
Destination: &ogdir, Destination: &ogdir,
}, },
} }
@ -420,6 +420,16 @@ var (
Destination: &ni, Destination: &ni,
}, },
} }
cleanRestoreFlags = []cli.Flag{
&cli.BoolFlag{
Name: "all",
Usage: "operate on all files in trash",
Aliases: []string{"a"},
Destination: &all,
DisableDefaultText: true,
},
}
) )
func main() { func main() {
@ -475,14 +485,14 @@ func interactiveMode() error {
for _, file := range selected { for _, file := range selected {
log.Debugf("gonna clean %s", file.Name()) log.Debugf("gonna clean %s", file.Name())
} }
if err := confirmClean(selected); err != nil { if err := confirmClean(askconfirm, selected); err != nil {
return err return err
} }
case modes.Restoring: case modes.Restoring:
for _, file := range selected { for _, file := range selected {
log.Debugf("gonna restore %s", file.Name()) log.Debugf("gonna restore %s", file.Name())
} }
if err := confirmRestore(selected); err != nil { if err := confirmRestore(askconfirm, selected); err != nil {
return err return err
} }
case modes.Interactive: case modes.Interactive:
@ -493,8 +503,8 @@ func interactiveMode() error {
return nil return nil
} }
func confirmRestore(fs files.Files) error { func confirmRestore(confirm bool, fs files.Files) error {
if !askconfirm || prompt.YesNo(fmt.Sprintf("restore %d selected files?", len(fs))) { if !confirm || prompt.YesNo(fmt.Sprintf("restore %d selected files?", len(fs))) {
log.Info("doing the thing") log.Info("doing the thing")
restored, err := files.Restore(fs) restored, err := files.Restore(fs)
if err != nil { if err != nil {
@ -507,9 +517,9 @@ func confirmRestore(fs files.Files) error {
return nil return nil
} }
func confirmClean(fs files.Files) error { func confirmClean(confirm bool, fs files.Files) error {
if prompt.YesNo(fmt.Sprintf("remove %d selected files permanently from the trash?", len(fs))) && if prompt.YesNo(fmt.Sprintf("remove %d selected files permanently from the trash?", len(fs))) &&
(!askconfirm || prompt.YesNo(fmt.Sprintf("really remove all these %d selected files permanently from the trash forever??", len(fs)))) { (!confirm || prompt.YesNo(fmt.Sprintf("really remove all these %d selected files permanently from the trash forever??", len(fs)))) {
log.Info("gonna remove some files forever") log.Info("gonna remove some files forever")
removed, err := files.Remove(fs) removed, err := files.Remove(fs)
if err != nil { if err != nil {
@ -522,8 +532,8 @@ func confirmClean(fs files.Files) error {
return nil return nil
} }
func confirmTrash(fs files.Files) error { func confirmTrash(confirm bool, fs files.Files) error {
if !askconfirm || prompt.YesNo(fmt.Sprintf("trash %d selected files?", len(fs))) { if !confirm || prompt.YesNo(fmt.Sprintf("trash %d selected files?", len(fs))) {
tfs := make([]string, 0, len(fs)) tfs := make([]string, 0, len(fs))
for _, file := range fs { for _, file := range fs {
log.Debugf("gonna trash %s", file.Path()) log.Debugf("gonna trash %s", file.Path())
@ -548,7 +558,7 @@ func confirmTrash(fs files.Files) error {
return nil return nil
} }
func getMode(in string) (fs.FileMode, error) { func parseMode(in string) (fs.FileMode, error) {
if in == "" { if in == "" {
return fs.FileMode(0), nil return fs.FileMode(0), nil
} }