0ee7d30187920751c6e79facbd87ebce86d62ec9 — Galen Abell 3 months ago a669233
Add :detach command

Add a command for removing attachments from a composed message. Syntax
is :detach [path], with path being an optional argument specifying the
path of one existing attachment. If no path is specified, the first
attachment is removed.
3 files changed, 79 insertions(+), 0 deletions(-)

A commands/compose/detach.go
M doc/aerc.1.scd
M widgets/compose.go
A commands/compose/detach.go => commands/compose/detach.go +55 -0
@@ 0,0 1,55 @@
+package compose
+
+import (
+	"fmt"
+	"time"
+
+	"git.sr.ht/~sircmpwn/aerc/widgets"
+	"github.com/gdamore/tcell"
+)
+
+type Detach struct{}
+
+func init() {
+	register(Detach{})
+}
+
+func (_ Detach) Aliases() []string {
+	return []string{"detach"}
+}
+
+func (_ Detach) Complete(aerc *widgets.Aerc, args []string) []string {
+	composer, _ := aerc.SelectedTab().(*widgets.Composer)
+
+	return composer.GetAttachments()
+}
+
+func (_ Detach) Execute(aerc *widgets.Aerc, args []string) error {
+	var path string
+	composer, _ := aerc.SelectedTab().(*widgets.Composer)
+
+	if len(args) > 2 {
+		return fmt.Errorf("Usage: :detach [path]")
+	}
+
+	if len(args) == 2 {
+		path = args[1]
+	} else {
+		// if no attachment is specified, delete the first in the list
+		atts := composer.GetAttachments()
+		if len(atts) > 0 {
+			path = atts[0]
+		} else {
+			return fmt.Errorf("No attachments to delete")
+		}
+	}
+
+	if err := composer.DeleteAttachment(path); err != nil {
+		return err
+	}
+
+	aerc.PushStatus(fmt.Sprintf("Detached %s", path), 10*time.Second).
+		Color(tcell.ColorDefault, tcell.ColorGreen)
+
+	return nil
+}

M doc/aerc.1.scd => doc/aerc.1.scd +4 -0
@@ 210,6 210,10 @@ message list, the message in the message viewer, etc).
 *attach* <path>
 	Attaches the file at the given path to the email.
 
+*detach* [path]
+	Detaches the file with the given path from the composed email. If no path is
+	specified, detaches the first attachment instead.
+
 *edit*
 	(Re-) opens your text editor to edit the message in progress.
 

M widgets/compose.go => widgets/compose.go +20 -0
@@ 434,8 434,28 @@ func writeAttachment(path string, writer *mail.Writer) error {
 	return nil
 }
 
+func (c *Composer) GetAttachments() []string {
+	return c.attachments
+}
+
 func (c *Composer) AddAttachment(path string) {
 	c.attachments = append(c.attachments, path)
+	c.resetReview()
+}
+
+func (c *Composer) DeleteAttachment(path string) error {
+	for i, a := range c.attachments {
+		if a == path {
+			c.attachments = append(c.attachments[:i], c.attachments[i+1:]...)
+			c.resetReview()
+			return nil
+		}
+	}
+
+	return errors.New("attachment does not exist")
+}
+
+func (c *Composer) resetReview() {
 	if c.review != nil {
 		c.grid.RemoveChild(c.review)
 		c.review = newReviewMessage(c, nil)