summary refs log tree commit diff stats
path: root/commands/msg/modify-labels.go
diff options
context:
space:
mode:
authorReto Brunner <reto@labrat.space>2019-09-11 21:01:05 +0200
committerDrew DeVault <sir@cmpwn.com>2019-09-12 16:17:24 -0400
commita93b4de6f3c362d6e0db0b1f6d3f2e1c9a5cd64d (patch)
treed5b2451f9a5bb6d79b284ea618361ed4d8eb4a33 /commands/msg/modify-labels.go
parent618a500341d54ec5bec6d035a86b1307ff1dad0a (diff)
downloadaerc-a93b4de6f3c362d6e0db0b1f6d3f2e1c9a5cd64d.tar.gz
Add modify-labels command
This adds the event type as well as the command implementation, but no backend
supports it yet.
Diffstat (limited to 'commands/msg/modify-labels.go')
-rw-r--r--commands/msg/modify-labels.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/commands/msg/modify-labels.go b/commands/msg/modify-labels.go
new file mode 100644
index 0000000..f33a9ca
--- /dev/null
+++ b/commands/msg/modify-labels.go
@@ -0,0 +1,69 @@
+package msg
+
+import (
+	"errors"
+	"time"
+
+	"git.sr.ht/~sircmpwn/aerc/widgets"
+	"git.sr.ht/~sircmpwn/aerc/worker/types"
+	"github.com/gdamore/tcell"
+)
+
+type ModifyLabels struct{}
+
+func init() {
+	register(ModifyLabels{})
+}
+
+func (ModifyLabels) Aliases() []string {
+	return []string{"modify-labels"}
+}
+
+func (ModifyLabels) Complete(aerc *widgets.Aerc, args []string) []string {
+	return nil
+}
+
+func (ModifyLabels) Execute(aerc *widgets.Aerc, args []string) error {
+	changes := args[1:]
+	if len(changes) == 0 {
+		return errors.New("Usage: modify-labels <[+-]label> ...")
+	}
+
+	widget := aerc.SelectedTab().(widgets.ProvidesMessage)
+	acct := widget.SelectedAccount()
+	if acct == nil {
+		return errors.New("No account selected")
+	}
+	store := widget.Store()
+	if store == nil {
+		return errors.New("Cannot perform action. Messages still loading")
+	}
+	msg, err := widget.SelectedMessage()
+	if err != nil {
+		return err
+	}
+	var add, remove []string
+	for _, l := range changes {
+		switch l[0] {
+		case '+':
+			add = append(add, l[1:])
+		case '-':
+			remove = append(remove, l[1:])
+		default:
+			// if no operand is given assume add
+			add = append(add, l)
+		}
+	}
+	store.ModifyLabels([]uint32{msg.Uid}, add, remove, func(
+		msg types.WorkerMessage) {
+
+		switch msg := msg.(type) {
+		case *types.Done:
+			aerc.PushStatus("labels updated", 10*time.Second)
+		case *types.Error:
+			aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second).
+				Color(tcell.ColorDefault, tcell.ColorRed)
+		}
+	})
+	return nil
+}