1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
package marker
// TODO: fix headers for message that are nil
// Marker provides the interface for the marking behavior of messages
type Marker interface {
Mark(uint32)
Unmark(uint32)
ToggleMark(uint32)
Remark()
Marked() []uint32
IsMarked(uint32) bool
ToggleVisualMark()
UpdateVisualMark()
ClearVisualMark()
}
// UIDProvider provides the underlying uids and the selected message index
type UIDProvider interface {
Uids() []uint32
SelectedIndex() int
}
type controller struct {
uidProvider UIDProvider
marked map[uint32]struct{}
lastMarked map[uint32]struct{}
visualStartUID uint32
visualMarkMode bool
}
// New returns a new Marker
func New(up UIDProvider) Marker {
return &controller{
uidProvider: up,
marked: make(map[uint32]struct{}),
lastMarked: make(map[uint32]struct{}),
}
}
// Mark markes the uid as marked
func (mc *controller) Mark(uid uint32) {
if mc.visualMarkMode {
// visual mode has override, bogus input from user
return
}
mc.marked[uid] = struct{}{}
}
// Unmark unmarks the uid
func (mc *controller) Unmark(uid uint32) {
if mc.visualMarkMode {
// user probably wanted to clear the visual marking
mc.ClearVisualMark()
return
}
delete(mc.marked, uid)
}
// Remark restores the previous marks
func (mc *controller) Remark() {
mc.marked = mc.lastMarked
}
// ToggleMark toggles the marked state for the given uid
func (mc *controller) ToggleMark(uid uint32) {
if mc.visualMarkMode {
// visual mode has override, bogus input from user
return
}
if mc.IsMarked(uid) {
mc.Unmark(uid)
} else {
mc.Mark(uid)
}
}
// resetMark removes the marking from all messages
func (mc *controller) resetMark() {
mc.lastMarked = mc.marked
mc.marked = make(map[uint32]struct{})
}
// removeStaleUID removes uids that are no longer presents in the UIDProvider
func (mc *controller) removeStaleUID() {
for mark := range mc.marked {
present := false
for _, uid := range mc.uidProvider.Uids() {
if mark == uid {
present = true
break
}
}
if !present {
delete(mc.marked, mark)
}
}
}
// IsMarked checks whether the given uid has been marked
func (mc *controller) IsMarked(uid uint32) bool {
_, marked := mc.marked[uid]
return marked
}
// Marked returns the uids of all marked messages
func (mc *controller) Marked() []uint32 {
mc.removeStaleUID()
marked := make([]uint32, len(mc.marked))
i := 0
for uid := range mc.marked {
marked[i] = uid
i++
}
return marked
}
// ToggleVisualMark enters or leaves the visual marking mode
func (mc *controller) ToggleVisualMark() {
mc.visualMarkMode = !mc.visualMarkMode
if mc.visualMarkMode {
// just entered visual mode, reset whatever marking was already done
mc.resetMark()
uids := mc.uidProvider.Uids()
if idx := mc.uidProvider.SelectedIndex(); idx >= 0 && idx < len(uids) {
mc.visualStartUID = uids[idx]
mc.marked[mc.visualStartUID] = struct{}{}
}
}
}
// ClearVisualMark leaves the visual marking mode and resets any marking
func (mc *controller) ClearVisualMark() {
mc.resetMark()
mc.visualMarkMode = false
mc.visualStartUID = 0
}
// UpdateVisualMark updates the index with the currently selected message
func (mc *controller) UpdateVisualMark() {
if !mc.visualMarkMode {
// nothing to do
return
}
startIdx := mc.visualStartIdx()
if startIdx < 0 {
// something deleted the startuid, abort the marking process
mc.ClearVisualMark()
return
}
selectedIdx := mc.uidProvider.SelectedIndex()
if selectedIdx < 0 {
return
}
uids := mc.uidProvider.Uids()
var visUids []uint32
if selectedIdx > startIdx {
visUids = uids[startIdx : selectedIdx+1]
} else {
visUids = uids[selectedIdx : startIdx+1]
}
mc.resetMark()
for _, uid := range visUids {
mc.marked[uid] = struct{}{}
}
}
// returns the index of needle in haystack or -1 if not found
func (mc *controller) visualStartIdx() int {
for idx, u := range mc.uidProvider.Uids() {
if u == mc.visualStartUID {
return idx
}
}
return -1
}
|