aerc/commands/msg/modify-labels.go

65 lines
1.2 KiB
Go
Raw Normal View History

package msg
import (
"errors"
2019-12-21 16:21:27 +01:00
"git.sr.ht/~sircmpwn/aerc/commands"
"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)
}
func (ModifyLabels) Execute(aerc *widgets.Aerc, args []string) error {
changes := args[1:]
if len(changes) == 0 {
return errors.New("Usage: modify-labels <[+-]label> ...")
}
h := newHelper(aerc)
store, err := h.store()
if err != nil {
return err
}
uids, err := h.markedOrSelectedUids()
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(uids, add, remove, func(
msg types.WorkerMessage) {
switch msg := msg.(type) {
case *types.Done:
aerc.PushStatus("labels updated")
case *types.Error:
aerc.PushError(" " + msg.Error.Error())
}
})
return nil
}