2019-09-11 21:01:05 +02:00
|
|
|
package msg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2019-12-21 16:21:27 +01:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/commands"
|
2019-09-11 21:01:05 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/widgets"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/worker/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ModifyLabels struct{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(ModifyLabels{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ModifyLabels) Aliases() []string {
|
|
|
|
return []string{"modify-labels"}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ModifyLabels) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-12-21 16:21:27 +01:00
|
|
|
return commands.GetLabels(aerc, args)
|
2019-09-11 21:01:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ModifyLabels) Execute(aerc *widgets.Aerc, args []string) error {
|
|
|
|
changes := args[1:]
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return errors.New("Usage: modify-labels <[+-]label> ...")
|
|
|
|
}
|
|
|
|
|
2019-12-18 06:34:06 +01:00
|
|
|
h := newHelper(aerc)
|
|
|
|
store, err := h.store()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-09-11 21:01:05 +02:00
|
|
|
}
|
2020-05-09 11:50:30 +02:00
|
|
|
uids, err := h.markedOrSelectedUids()
|
2019-09-11 21:01:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-18 06:34:06 +01:00
|
|
|
|
2019-09-11 21:01:05 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2019-12-18 06:34:06 +01:00
|
|
|
store.ModifyLabels(uids, add, remove, func(
|
2019-09-11 21:01:05 +02:00
|
|
|
msg types.WorkerMessage) {
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *types.Done:
|
2020-05-27 07:52:13 +02:00
|
|
|
aerc.PushStatus("labels updated")
|
2019-09-11 21:01:05 +02:00
|
|
|
case *types.Error:
|
2020-05-27 07:52:13 +02:00
|
|
|
aerc.PushError(" " + msg.Error.Error())
|
2019-09-11 21:01:05 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|