2019-09-11 19:01:05 +00:00
|
|
|
package msg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-05-28 14:32:32 +00:00
|
|
|
"time"
|
2019-09-11 19:01:05 +00:00
|
|
|
|
2019-12-21 15:21:27 +00:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/commands"
|
2019-09-11 19:01:05 +00: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 15:21:27 +00:00
|
|
|
return commands.GetLabels(aerc, args)
|
2019-09-11 19:01:05 +00: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 05:34:06 +00:00
|
|
|
h := newHelper(aerc)
|
|
|
|
store, err := h.store()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-09-11 19:01:05 +00:00
|
|
|
}
|
2020-05-09 09:50:30 +00:00
|
|
|
uids, err := h.markedOrSelectedUids()
|
2019-09-11 19:01:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-18 05:34:06 +00:00
|
|
|
|
2019-09-11 19:01:05 +00: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 05:34:06 +00:00
|
|
|
store.ModifyLabels(uids, add, remove, func(
|
2019-09-11 19:01:05 +00:00
|
|
|
msg types.WorkerMessage) {
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *types.Done:
|
2020-05-28 14:32:32 +00:00
|
|
|
aerc.PushStatus("labels updated", 10*time.Second)
|
2019-09-11 19:01:05 +00:00
|
|
|
case *types.Error:
|
2021-01-30 12:51:32 +00:00
|
|
|
aerc.PushError(msg.Error.Error())
|
2019-09-11 19:01:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|