aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Partin <tristan@partin.io>2024-03-07 16:52:48 +0000
committerRobin Jarry <robin@jarry.cc>2024-03-10 19:19:16 +0100
commit95bf78ed72e59ec01c3497767b7dabdf4e2cfa85 (patch)
tree1a5837e790edacc85f57d17aa117e3c2e654ac6a
parentf9b41b24b719310be756a77f9f9d49e69fa5aa72 (diff)
downloadaerc-95bf78ed72e59ec01c3497767b7dabdf4e2cfa85.tar.gz
aerc-95bf78ed72e59ec01c3497767b7dabdf4e2cfa85.zip
detach: add glob support
This matches the behavior of :attach. Changelog-added: The `:detach` command now understands globs similar to `:attach`. Signed-off-by: Tristan Partin <tristan@partin.io> Acked-by: Robin Jarry <robin@jarry.cc>
-rw-r--r--commands/compose/detach.go44
-rw-r--r--doc/aerc.1.scd3
2 files changed, 43 insertions, 4 deletions
diff --git a/commands/compose/detach.go b/commands/compose/detach.go
index 36c7c314..69f8538d 100644
--- a/commands/compose/detach.go
+++ b/commands/compose/detach.go
@@ -2,9 +2,12 @@ package compose
import (
"fmt"
+ "path/filepath"
"git.sr.ht/~rjarry/aerc/app"
"git.sr.ht/~rjarry/aerc/commands"
+ "git.sr.ht/~rjarry/aerc/lib/log"
+ "github.com/pkg/errors"
)
type Detach struct {
@@ -41,11 +44,46 @@ func (d Detach) Execute(args []string) error {
}
}
- if err := composer.DeleteAttachment(d.Path); err != nil {
- return err
+ return d.removePath(d.Path)
+}
+
+func (d Detach) removePath(path string) error {
+ composer, _ := app.SelectedTabContent().(*app.Composer)
+
+ // If we don't get an error here, the path was not a pattern.
+ if err := composer.DeleteAttachment(path); err == nil {
+ log.Debugf("detaching '%s'", path)
+ app.PushSuccess(fmt.Sprintf("Detached %s", path))
+
+ return nil
+ }
+
+ currentAttachments := composer.GetAttachments()
+ detached := make([]string, 0, len(currentAttachments))
+ for _, a := range currentAttachments {
+ // Don't use filepath.Glob like :attach does. Not all files
+ // that match the glob are already attached to the message.
+ matches, err := filepath.Match(path, a)
+ if err != nil && errors.Is(err, filepath.ErrBadPattern) {
+ log.Warnf("failed to parse as globbing pattern: %v", err)
+ return err
+ }
+
+ if matches {
+ log.Debugf("detaching '%s'", a)
+ if err := composer.DeleteAttachment(a); err != nil {
+ return err
+ }
+
+ detached = append(detached, a)
+ }
}
- app.PushSuccess(fmt.Sprintf("Detached %s", d.Path))
+ if len(detached) == 1 {
+ app.PushSuccess(fmt.Sprintf("Detached %s", detached[0]))
+ } else {
+ app.PushSuccess(fmt.Sprintf("Detached %d files", len(detached)))
+ }
return nil
}
diff --git a/doc/aerc.1.scd b/doc/aerc.1.scd
index a2d4c922..af1ce31c 100644
--- a/doc/aerc.1.scd
+++ b/doc/aerc.1.scd
@@ -803,7 +803,8 @@ message list, the message in the message viewer, etc).
*:detach* [_<path>_]
Detaches the file with the given path from the composed email. If no path is
- specified, detaches the first attachment instead.
+ specified, detaches the first attachment instead. The path can contain
+ globbing syntax described at https://godocs.io/path/filepath#Match.
*:cc* _<addresses>_++
*:bcc* _<addresses>_