aboutsummaryrefslogtreecommitdiff
path: root/ui/room-list.go
blob: a70fd6843f5f7e3fad6c905b4a16415329369860 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2018 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package ui

import (
	"fmt"
	"regexp"
	"strconv"
	"strings"
	"time"

	"maunium.net/go/gomuks/matrix/rooms"
	"maunium.net/go/gomuks/ui/widget"
	"maunium.net/go/tcell"
	"maunium.net/go/tview"
)

type roomListItem struct {
	room     *rooms.Room
	priority float64
}

type RoomList struct {
	*tview.Box

	// The list of tags in display order.
	tags []string
	// The list of rooms, in reverse order.
	items map[string][]*rooms.Room
	// The selected room.
	selected    *rooms.Room
	selectedTag string

	// The item main text color.
	mainTextColor tcell.Color
	// The text color for selected items.
	selectedTextColor tcell.Color
	// The background color for selected items.
	selectedBackgroundColor tcell.Color
}

func NewRoomList() *RoomList {
	return &RoomList{
		Box:   tview.NewBox(),
		items: make(map[string][]*rooms.Room),

		mainTextColor:           tcell.ColorWhite,
		selectedTextColor:       tcell.ColorWhite,
		selectedBackgroundColor: tcell.ColorDarkGreen,
	}
}

func (list *RoomList) Contains(roomID string) bool {
	for _, roomList := range list.items {
		for _, room := range roomList {
			if room.ID == roomID {
				return true
			}
		}
	}
	return false
}

func (list *RoomList) Add(room *rooms.Room) {
	for _, tag := range room.Tags() {
		list.AddToTag(tag.Tag, room)
	}
}

func (list *RoomList) CheckTag(tag string) {
	index := list.IndexTag(tag)

	items, ok := list.items[tag]

	if len(items) == 0 {
		delete(list.items, tag)
		ok = false
	}

	if ok && index == -1 {
		list.tags = append(list.tags, tag)
	} else if index != -1 {
		list.tags = append(list.tags[0:index], list.tags[index+1:]...)
	}
}

func (list *RoomList) AddToTag(tag string, room *rooms.Room) {
	items, ok := list.items[tag]
	if !ok {
		list.items[tag] = []*rooms.Room{room}
		return
	}

	// Add space for new item.
	items = append(items, nil)
	// The default insert index is the newly added slot.
	// That index will be used if all other rooms in the list have the same LastReceivedMessage timestamp.
	insertAt := len(items) - 1
	// Find the spot where the new room should be put according to the last received message timestamps.
	for i := 0; i < len(items)-1; i++ {
		if items[i].LastReceivedMessage.After(room.LastReceivedMessage) {
			insertAt = i
			break
		}
	}
	// Move newer rooms forward in the array.
	for i := len(items) - 1; i > insertAt; i-- {
		items[i] = items[i-1]
	}
	// Insert room.
	items[insertAt] = room

	list.items[tag] = items
	list.CheckTag(tag)
}

func (list *RoomList) Remove(room *rooms.Room) {
	for _, tag := range room.Tags() {
		list.RemoveFromTag(tag.Tag, room)
	}
}

func (list *RoomList) RemoveFromTag(tag string, room *rooms.Room) {
	items, ok := list.items[tag]
	if !ok {
		return
	}

	index := list.indexInTag(tag, room)
	if index == -1 {
		return
	}

	items = append(items[0:index], items[index+1:]...)

	if len(items) == 0 {
		delete(list.items, tag)
	} else {
		list.items[tag] = items
	}

	if room == list.selected {
		// Room is currently selected, move selection to another room.
		if index > 0 {
			list.selected = items[index-1]
		} else if len(items) > 0 {
			list.selected = items[0]
		} else if len(list.items) > 0 {
			for _, tag := range list.tags {
				moreItems := list.items[tag]
				if len(moreItems) > 0 {
					list.selected = moreItems[0]
					list.selectedTag = tag
				}
			}
		} else {
			list.selected = nil
			list.selectedTag = ""
		}
	}
	list.CheckTag(tag)
}

func (list *RoomList) Bump(room *rooms.Room) {
	for _, tag := range room.Tags() {
		list.bumpInTag(tag.Tag, room)
	}
}

func (list *RoomList) bumpInTag(tag string, room *rooms.Room) {
	items, ok := list.items[tag]
	if !ok {
		return
	}

	found := false
	for i := 0; i < len(items)-1; i++ {
		if items[i] == room {
			found = true
		}
		if found {
			items[i] = items[i+1]
		}
	}
	if found {
		items[len(items)-1] = room
		room.LastReceivedMessage = time.Now()
	}
}

func (list *RoomList) Clear() {
	list.items = make(map[string][]*rooms.Room)
	list.selected = nil
	list.selectedTag = ""
}

func (list *RoomList) SetSelected(tag string, room *rooms.Room) {
	list.selected = room
	list.selectedTag = ""
}

func (list *RoomList) HasSelected() bool {
	return list.selected != nil
}

func (list *RoomList) Selected() (string, *rooms.Room) {
	return list.selectedTag, list.selected
}

func (list *RoomList) SelectedRoom() *rooms.Room {
	return list.selected
}

func (list *RoomList) First() (string, *rooms.Room) {
	for _, tag := range list.tags {
		items := list.items[tag]
		if len(items) > 0 {
			return tag, items[0]
		}
	}
	return "", nil
}

func (list *RoomList) Last() (string, *rooms.Room) {
	for tagIndex := len(list.tags) - 1; tagIndex >= 0; tagIndex-- {
		tag := list.tags[tagIndex]
		items := list.items[tag]
		if len(items) > 0 {
			return tag, items[len(items)-1]
		}
	}
	return "", nil
}

func (list *RoomList) IndexTag(tag string) int {
	for index, entry := range list.tags {
		if tag == entry {
			return index
		}
	}
	return -1
}

func (list *RoomList) Previous() (string, *rooms.Room) {
	if len(list.items) == 0 {
		return "", nil
	} else if list.selected == nil {
		return list.First()
	}

	items := list.items[list.selectedTag]
	index := list.indexInTag(list.selectedTag, list.selected)
	if index == len(items)-1 {
		tagIndex := list.IndexTag(list.selectedTag)
		tagIndex++
		for ; tagIndex < len(list.tags); tagIndex++ {
			nextTag := list.tags[tagIndex]
			nextTagItems := list.items[nextTag]
			if len(nextTagItems) > 0 {
				return nextTag, nextTagItems[0]
			}
		}
		return list.First()
	}
	return list.selectedTag, items[index+1]
}

func (list *RoomList) Next() (string, *rooms.Room) {
	if len(list.items) == 0 {
		return "", nil
	} else if list.selected == nil {
		return list.First()
	}

	items := list.items[list.selectedTag]
	index := list.indexInTag(list.selectedTag, list.selected)
	if index == 0 {
		tagIndex := list.IndexTag(list.selectedTag)
		tagIndex--
		for ; tagIndex >= 0; tagIndex-- {
			prevTag := list.tags[tagIndex]
			prevTagItems := list.items[prevTag]
			if len(prevTagItems) > 0 {
				return prevTag, prevTagItems[len(prevTagItems)-1]
			}
		}
		return list.Last()
	}
	return list.selectedTag, items[index-1]
}

func (list *RoomList) indexInTag(tag string, room *rooms.Room) int {
	roomIndex := -1
	items := list.items[tag]
	for index, entry := range items {
		if entry == room {
			roomIndex = index
			break
		}
	}
	return roomIndex
}

func (list *RoomList) Get(n int) (string, *rooms.Room) {
	if n < 0 {
		return "", nil
	}
	for _, tag := range list.tags {
		// Tag header
		n--

		items := list.items[tag]
		if n < 0 {
			return "", nil
		} else if n < len(items) {
			return tag, items[len(items)-1-n]
		}

		// Tag items
		n -= len(items)
	}
	return "", nil
}

var nsRegex = regexp.MustCompile("^[a-z]\\.[a-z](?:\\.[a-z])*$")

func (list *RoomList) GetTagDisplayName(tag string) string {
	switch {
	case len(tag) == 0:
		return "Rooms"
	case tag == "m.favourite":
		return "Favorites"
	case tag == "m.lowpriority":
		return "Low Priority"
	case strings.HasPrefix(tag, "m."):
		return strings.Title(strings.Replace(tag[len("m."):], "_", " ", -1))
	case strings.HasPrefix(tag, "u."):
		return tag[len("u."):]
	case !nsRegex.MatchString(tag):
		return tag
	default:
		return ""
	}
}

// Draw draws this primitive onto the screen.
func (list *RoomList) Draw(screen tcell.Screen) {
	list.Box.Draw(screen)

	x, y, width, height := list.GetInnerRect()
	bottomLimit := y + height

	var offset int
	/* TODO fix offset
	currentItemIndex := list.Index(list.selected)
	if currentItemIndex >= height {
		offset = currentItemIndex + 1 - height
	}*/

	// Draw the list items.
	for _, tag := range list.tags {
		items := list.items[tag]
		widget.WriteLine(screen, tview.AlignLeft, list.GetTagDisplayName(tag), x, y, width, tcell.StyleDefault.Underline(true).Bold(true))
		y++
		for i := len(items) - 1; i >= 0; i-- {
			item := items[i]
			index := len(items) - 1 - i

			if index < offset {
				continue
			}

			if y >= bottomLimit {
				break
			}

			text := item.GetTitle()

			lineWidth := width

			style := tcell.StyleDefault.Foreground(list.mainTextColor)
			if tag == list.selectedTag && item == list.selected {
				style = style.Foreground(list.selectedTextColor).Background(list.selectedBackgroundColor)
			}
			if item.HasNewMessages {
				style = style.Bold(true)
			}

			if item.UnreadMessages > 0 {
				unreadMessageCount := "99+"
				if item.UnreadMessages < 100 {
					unreadMessageCount = strconv.Itoa(item.UnreadMessages)
				}
				if item.Highlighted {
					unreadMessageCount += "!"
				}
				unreadMessageCount = fmt.Sprintf("(%s)", unreadMessageCount)
				widget.WriteLine(screen, tview.AlignRight, unreadMessageCount, x+lineWidth-6, y, 6, style)
				lineWidth -= len(unreadMessageCount) + 1
			}

			widget.WriteLine(screen, tview.AlignLeft, text, x, y, lineWidth, style)

			y++
			if y >= bottomLimit {
				break
			}
		}
	}
}