Debian Codegraph @

commands/msg/read.go

raw
1 package msg
2
3 import (
4 "fmt"
5 "strings"
6 "time"
7
8 "git.sr.ht/~rjarry/aerc/app"
9 "git.sr.ht/~rjarry/aerc/commands"
10 "git.sr.ht/~rjarry/aerc/models"
11 "git.sr.ht/~rjarry/aerc/worker/types"
12 )
13
14 type FlagMsg struct {
15 Toggle bool `opt:"-t" desc:"Toggle between set and unset."`
16 Answered bool `opt:"-a" aliases:"flag,unflag" desc:"Set/unset the answered flag."`
17 Forwarded bool `opt:"-f" aliases:"flag,unflag" desc:"Set/unset the forwarded flag."`
18 Flag models.Flags `opt:"-x" aliases:"flag,unflag" action:"ParseFlag" complete:"CompleteFlag" desc:"Flag name."`
19 FlagName string
20 }
21
22 func init() {
23 commands.Register(FlagMsg{})
24 }
25
26 func (FlagMsg) Description() string {
27 return "Set or unset a flag on the marked or selected messages."
28 }
29
30 func (FlagMsg) Context() commands.CommandContext {
31 return commands.MESSAGE_LIST | commands.MESSAGE_VIEWER
32 }
33
34 func (FlagMsg) Aliases() []string {
35 return []string{"flag", "unflag", "read", "unread"}
36 }
37
38 func (f *FlagMsg) ParseFlag(arg string) error {
39 switch strings.ToLower(arg) {
40 case "seen":
41 f.Flag = models.SeenFlag
42 f.FlagName = "seen"
43 case "answered":
44 f.Flag = models.AnsweredFlag
45 f.FlagName = "answered"
46 case "forwarded":
47 f.Flag = models.ForwardedFlag
48 f.FlagName = "forwarded"
49 case "flagged":
50 f.Flag = models.FlaggedFlag
51 f.FlagName = "flagged"
52 case "draft":
53 f.Flag = models.DraftFlag
54 f.FlagName = "draft"
55 default:
56 return fmt.Errorf("Unknown flag %q", arg)
57 }
58 return nil
59 }
60
61 var validFlags = []string{"seen", "answered", "forwarded", "flagged", "draft"}
62
63 func (*FlagMsg) CompleteFlag(arg string) []string {
64 return commands.FilterList(validFlags, arg, nil)
65 }
66
67 // If this was called as 'flag' or 'unflag', without the toggle (-t)
68 // option, then it will flag the corresponding messages with the given
69 // flag. If the toggle option was given, it will individually toggle
70 // the given flag for the corresponding messages.
71 //
72 // If this was called as 'read' or 'unread', it has the same effect as
73 // 'flag' or 'unflag', respectively, but the 'Seen' flag is affected.
74 func (f FlagMsg) Execute(args []string) error {
75 // User-readable name for the action being performed
76 var actionName string
77
78 switch args[0] {
79 case "read", "unread":
80 f.Flag = models.SeenFlag
81 f.FlagName = "seen"
82 case "flag", "unflag":
83 if f.Answered {
84 f.Flag = models.AnsweredFlag
85 f.FlagName = "answered"
86 }
87 if f.Forwarded {
88 f.Flag = models.ForwardedFlag
89 f.FlagName = "forwarded"
90 }
91 if f.Flag == 0 {
92 f.Flag = models.FlaggedFlag
93 f.FlagName = "flagged"
94 }
95 }
96
97 h := newHelper()
98 store, err := h.store()
99 if err != nil {
100 return err
101 }
102
103 // UIDs of messages to enable or disable the flag for.
104 var toEnable []models.UID
105 var toDisable []models.UID
106
107 if f.Toggle {
108 // If toggling, split messages into those that need to
109 // be enabled / disabled.
110 msgs, err := h.messages()
111 if err != nil {
112 return err
113 }
114 for _, m := range msgs {
115 if m.Flags.Has(f.Flag) {
116 toDisable = append(toDisable, m.Uid)
117 } else {
118 toEnable = append(toEnable, m.Uid)
119 }
120 }
121 actionName = "Toggling"
122 } else {
123 msgUids, err := h.markedOrSelectedUids()
124 if err != nil {
125 return err
126 }
127 switch args[0] {
128 case "read", "flag":
129 toEnable = msgUids
130 actionName = "Setting"
131 default:
132 toDisable = msgUids
133 actionName = "Unsetting"
134 }
135 }
136
137 status := fmt.Sprintf("%s flag %q successful", actionName, f.FlagName)
138
139 if len(toEnable) != 0 {
140 store.Flag(toEnable, f.Flag, true, func(msg types.WorkerMessage) {
141 switch msg := msg.(type) {
142 case *types.Done:
143 app.PushStatus(status, 10*time.Second)
144 store.Marker().ClearVisualMark()
145 case *types.Error:
146 app.PushError(msg.Error.Error())
147 }
148 })
149 }
150 if len(toDisable) != 0 {
151 store.Flag(toDisable, f.Flag, false, func(msg types.WorkerMessage) {
152 switch msg := msg.(type) {
153 case *types.Done:
154 app.PushStatus(status, 10*time.Second)
155 store.Marker().ClearVisualMark()
156 case *types.Error:
157 app.PushError(msg.Error.Error())
158 }
159 })
160 }
161 return nil
162 }