2019-09-11 21:01:05 +02:00
|
|
|
package msg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-05-28 16:32:32 +02:00
|
|
|
"time"
|
2019-09-11 21:01:05 +02:00
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/commands"
|
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
2019-09-11 21:01:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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(
|
2022-07-31 22:16:40 +02:00
|
|
|
msg types.WorkerMessage,
|
|
|
|
) {
|
2019-09-11 21:01:05 +02:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *types.Done:
|
2020-05-28 16:32:32 +02:00
|
|
|
aerc.PushStatus("labels updated", 10*time.Second)
|
2022-08-08 22:21:41 +02:00
|
|
|
store.Marker().ClearVisualMark()
|
2019-09-11 21:01:05 +02:00
|
|
|
case *types.Error:
|
2021-01-30 13:51:32 +01:00
|
|
|
aerc.PushError(msg.Error.Error())
|
2019-09-11 21:01:05 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|