diff options
author | Jason Cox <me@jasoncarloscox.com> | 2024-06-16 09:34:52 -0400 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2024-06-23 22:13:38 +0200 |
commit | 2c06df8720ccf9619fbc293be3c33965672667a9 (patch) | |
tree | 2f60d9677014057403658815ab1e6630f4b34074 | |
parent | e2a9cd0881593c3b3a24863ceb535367c5570888 (diff) | |
download | aerc-2c06df8720ccf9619fbc293be3c33965672667a9.tar.gz |
rmdir: allow specifying folder to delete
It's useful to delete folders other than the current one. If a folder is
specified, delete that one; otherwise, delete the current one.
Changelog-added: Allow specifying the folder to delete with `:rmdir`.
Signed-off-by: Jason Cox <me@jasoncarloscox.com>
Tested-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
-rw-r--r-- | commands/account/rmdir.go | 68 | ||||
-rw-r--r-- | doc/aerc.1.scd | 4 |
2 files changed, 49 insertions, 23 deletions
diff --git a/commands/account/rmdir.go b/commands/account/rmdir.go index b08a2a1e..cd70aa62 100644 --- a/commands/account/rmdir.go +++ b/commands/account/rmdir.go @@ -2,16 +2,19 @@ package account import ( "errors" + "fmt" "time" "git.sr.ht/~rjarry/aerc/app" "git.sr.ht/~rjarry/aerc/commands" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/worker/types" + "git.sr.ht/~rjarry/go-opt" ) type RemoveDir struct { - Force bool `opt:"-f"` + Force bool `opt:"-f"` + Folder string `opt:"folder" complete:"CompleteFolder" required:"false"` } func init() { @@ -26,19 +29,33 @@ func (RemoveDir) Aliases() []string { return []string{"rmdir"} } +func (RemoveDir) CompleteFolder(arg string) []string { + acct := app.SelectedAccount() + if acct == nil { + return nil + } + return commands.FilterList(acct.Directories().List(), arg, opt.QuoteArg) +} + func (r RemoveDir) Execute(args []string) error { acct := app.SelectedAccount() if acct == nil { return errors.New("No account selected") } - var role models.Role - if d := acct.Directories().SelectedDirectory(); d != nil { - role = d.Role + current := acct.Directories().SelectedDirectory() + toRemove := current + if r.Folder != "" { + toRemove = acct.Directories().Directory(r.Folder) + if toRemove == nil { + return fmt.Errorf("No such directory: %s", r.Folder) + } } + role := toRemove.Role + // Check for any messages in the directory. - if role != models.QueryRole && !acct.Messages().Empty() && !r.Force { + if role != models.QueryRole && toRemove.Exists > 0 && !r.Force { return errors.New("Refusing to remove non-empty directory; use -f") } @@ -46,7 +63,12 @@ func (r RemoveDir) Execute(args []string) error { return errors.New("Cannot remove a virtual node") } - curDir := acct.SelectedDirectory() + if toRemove != current { + r.remove(acct, toRemove, func() {}) + return nil + } + + curDir := current.Name var newDir string dirFound := false @@ -102,22 +124,26 @@ func (r RemoveDir) Execute(args []string) error { default: return } - acct.Worker().PostAction(&types.RemoveDirectory{ - Directory: curDir, - Quiet: r.Force, - }, func(msg types.WorkerMessage) { - switch msg := msg.(type) { - case *types.Done: - app.PushStatus("Directory removed.", 10*time.Second) - case *types.Error: - app.PushError(msg.Error.Error()) - reopenCurrentDir() - case *types.Unsupported: - app.PushError(":rmdir is not supported by the backend.") - reopenCurrentDir() - } - }) + r.remove(acct, toRemove, reopenCurrentDir) }, false) return nil } + +func (r RemoveDir) remove(acct *app.AccountView, dir *models.Directory, onErr func()) { + acct.Worker().PostAction(&types.RemoveDirectory{ + Directory: dir.Name, + Quiet: r.Force, + }, func(msg types.WorkerMessage) { + switch msg := msg.(type) { + case *types.Done: + app.PushStatus("Directory removed.", 10*time.Second) + case *types.Error: + app.PushError(msg.Error.Error()) + onErr() + case *types.Unsupported: + app.PushError(":rmdir is not supported by the backend.") + onErr() + } + }) +} diff --git a/doc/aerc.1.scd b/doc/aerc.1.scd index 1da6be3c..60b8dd27 100644 --- a/doc/aerc.1.scd +++ b/doc/aerc.1.scd @@ -577,8 +577,8 @@ message list, the message in the message viewer, etc). *:mkdir* _<name>_ Creates a new folder for this account and changes to that folder. -*:rmdir* [*-f*] - Removes the current folder. +*:rmdir* [*-f*] [_<folder>_] + Removes the folder _<folder>_, or the current folder if not specified. By default, it will fail if the directory is non-empty (see *-f*). |