summary refs log tree commit diff stats
path: root/commands
diff options
context:
space:
mode:
authorGalen Abell <galen@galenabell.com>2019-07-16 16:48:25 -0400
committerDrew DeVault <sir@cmpwn.com>2019-07-19 10:30:47 -0400
commit7899d15d607cd9122e731cd2d2a8e52ee523ce0c (patch)
treefa83a1ae3721f7201229d1dd5324863561ab8e2f /commands
parentfe7230bb9acc5dc9cc8a982a35196dd6796b5360 (diff)
downloadaerc-7899d15d607cd9122e731cd2d2a8e52ee523ce0c.tar.gz
Add :attach command for compose
Allow users to add attachments to emails in the Compose view. Syntax is
:attach <path>, where path is a valid file. Attachments will show up in
the pre-send review screen.
Diffstat (limited to 'commands')
-rw-r--r--commands/compose/attach.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/commands/compose/attach.go b/commands/compose/attach.go
new file mode 100644
index 0000000..43aa32d
--- /dev/null
+++ b/commands/compose/attach.go
@@ -0,0 +1,56 @@
+package compose
+
+import (
+	"fmt"
+	"os"
+	"time"
+
+	"git.sr.ht/~sircmpwn/aerc/widgets"
+	"github.com/gdamore/tcell"
+	"github.com/mitchellh/go-homedir"
+)
+
+type Attach struct{}
+
+func init() {
+	register(Attach{})
+}
+
+func (_ Attach) Aliases() []string {
+	return []string{"attach"}
+}
+
+func (_ Attach) Complete(aerc *widgets.Aerc, args []string) []string {
+	return nil
+}
+
+func (_ Attach) Execute(aerc *widgets.Aerc, args []string) error {
+	if len(args) != 2 {
+		return fmt.Errorf("Usage: :attach <path>")
+	}
+
+	path := args[1]
+
+	path, err := homedir.Expand(path)
+	if err != nil {
+		aerc.PushError(" " + err.Error())
+		return err
+	}
+
+	pathinfo, err := os.Stat(path)
+	if err != nil {
+		aerc.PushError(" " + err.Error())
+		return err
+	} else if pathinfo.IsDir() {
+		aerc.PushError("Attachment must be a file, not a directory")
+		return nil
+	}
+
+	composer, _ := aerc.SelectedTab().(*widgets.Composer)
+	composer.AddAttachment(path)
+
+	aerc.PushStatus(fmt.Sprintf("Attached %s", pathinfo.Name()), 10*time.Second).
+		Color(tcell.ColorDefault, tcell.ColorGreen)
+
+	return nil
+}