aerc/commands/msg/mark.go

107 lines
1.7 KiB
Go
Raw Permalink Normal View History

2019-12-18 05:34:01 +00:00
package msg
import (
"fmt"
"git.sr.ht/~rjarry/aerc/widgets"
2019-12-18 05:34:01 +00:00
"git.sr.ht/~sircmpwn/getopt"
)
type Mark struct{}
func init() {
register(Mark{})
}
func (Mark) Aliases() []string {
return []string{"mark", "unmark", "remark"}
2019-12-18 05:34:01 +00:00
}
func (Mark) Complete(aerc *widgets.Aerc, args []string) []string {
return nil
}
func (Mark) Execute(aerc *widgets.Aerc, args []string) error {
h := newHelper(aerc)
selected, err := h.msgProvider.SelectedMessage()
if err != nil {
return err
}
store, err := h.store()
if err != nil {
return err
}
opts, _, err := getopt.Getopts(args, "atv")
if err != nil {
return err
}
var all bool
var toggle bool
var visual bool
for _, opt := range opts {
switch opt.Option {
case 'a':
all = true
case 'v':
visual = true
case 't':
toggle = true
}
}
switch args[0] {
case "mark":
if all && visual {
return fmt.Errorf("-a and -v are mutually exclusive")
}
var modFunc func(uint32)
if toggle {
modFunc = store.ToggleMark
} else {
modFunc = store.Mark
}
switch {
case all:
2019-12-18 05:34:01 +00:00
uids := store.Uids()
for _, uid := range uids {
modFunc(uid)
}
return nil
case visual:
2019-12-18 05:34:01 +00:00
store.ToggleVisualMark()
return nil
default:
2019-12-18 05:34:01 +00:00
modFunc(selected.Uid)
return nil
}
case "unmark":
if visual {
2020-05-20 00:48:23 +00:00
return fmt.Errorf("visual mode not supported for this command")
2019-12-18 05:34:01 +00:00
}
switch {
case all && toggle:
2019-12-18 05:34:01 +00:00
uids := store.Uids()
for _, uid := range uids {
store.ToggleMark(uid)
}
return nil
case all && !toggle:
2019-12-18 05:34:01 +00:00
store.ClearVisualMark()
return nil
default:
2019-12-18 05:34:01 +00:00
store.Unmark(selected.Uid)
return nil
}
case "remark":
if all || visual || toggle {
return fmt.Errorf("Usage: :remark")
}
store.Remark()
return nil
2019-12-18 05:34:01 +00:00
}
return nil // never reached
}