~chrisppy/beagles

ref: 6754a4c2d075f4bb2eddd7e9a59aff9901934b13 beagles/ui/tree.go -rw-r--r-- 7.2 KiB
6754a4c2Chris Palmer Suppress cview default tree and list input handler 8 months ago
                                                                                
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
// This file is part of beagles.
//
// Copyright © 2020-2021 Chris Palmer <chris@red-oxide.org>
//
// 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"
	"sort"
	"strconv"
	"strings"
	"time"

	"git.sr.ht/~chrisppy/beagles/db"
	"github.com/gdamore/tcell/v2"
	tui "gitlab.com/tslocum/cview"
)

type tree struct {
	Widget      *tui.TreeView
	TextColor   tcell.Color
	ReadColor   tcell.Color
	Dates       map[string]time.Time
	ChildParent map[string]*tui.TreeNode
	AudioIcon   string
}

func (i *UI) newTree() *tree {
	view := tui.NewTreeView()
	view.SetGraphics(true)
	view.SetTopLevel(1)

	view.SetBackgroundColor(i.Config.Theme.TreeBGColor)
	view.SetGraphicsColor(i.Config.Theme.TreeFGColor)
	view.SetInputCapture(nil)
	node := tui.NewTreeNode("subscriptions")
	node.SetColor(i.Config.Theme.TreeFGColor)

	childNodes := make([]*tui.TreeNode, 0)
	node.SetChildren(childNodes)

	view.SetRoot(node)
	view.SetCurrentNode(node)
	view.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { return nil })

	w := &tree{
		Widget:      view,
		TextColor:   i.Config.Theme.TreeFGColor,
		ReadColor:   i.Config.Theme.ReadColor,
		Dates:       make(map[string]time.Time),
		ChildParent: make(map[string]*tui.TreeNode),
		AudioIcon:   i.Config.Theme.AudioIcon,
	}

	index := 0
	feeds := make([]*db.Feed, len(i.DB.Feeds))
	for _, feed := range i.DB.Feeds {
		feeds[index] = feed
		index++
	}

	sort.Slice(feeds, func(i, j int) bool {
		return feeds[i].Compare(feeds[j])
	})

	for _, feed := range feeds {
		w.add(feed, i.DB.Items, false)
	}

	return w
}

func (w *tree) getRoot() *tui.TreeNode {
	return w.Widget.GetRoot()
}

func (w *tree) convert(item *db.Item) (*tui.TreeNode, bool) {
	name := item.Title

	if strings.Contains(item.Type, "audio") {
		name = fmt.Sprintf("%s %s", w.AudioIcon, name)
	} else {
		name = fmt.Sprintf("  %s", name)
	}

	n := tui.NewTreeNode(name)
	n.SetReference(item.Link)

	n.SetColor(w.TextColor)
	read := item.Read
	if read {
		n.SetColor(w.ReadColor)
	}

	w.Dates[item.Link] = item.Date

	return n, read
}

func (w *tree) add(f *db.Feed, itemMap db.Items, hide bool) {
	unread := 0
	node := tui.NewTreeNode("")
	items := make([]*tui.TreeNode, 0)
	for k := range f.Items {
		it := itemMap[k]

		n, read := w.convert(it)
		if read {
			if hide {
				continue
			}
		} else {
			unread++
		}

		items = append(items, n)
		w.ChildParent[it.Link] = node
	}

	if len(items) == 0 {
		return
	}

	sort.Slice(items, func(i, j int) bool {
		a := fmt.Sprintf("%v", items[i].GetReference())
		b := fmt.Sprintf("%v", items[j].GetReference())
		return w.Dates[a].Sub(w.Dates[b]) > 0
	})

	node.SetText(getTitle(f.Title, unread, false, false))
	node.SetColor(w.TextColor)
	node.SetReference(f.UpdateURL)
	node.SetChildren(items)
	node.Collapse()

	w.getRoot().AddChild(node)
}

func getTitle(title string, unread int, increment bool, decrement bool) string {
	p := regexp.MustCompilePOSIX(` \(\*[0-9]*\)`)
	found := p.FindAllString(title, -1)
	if found == nil {
		if unread == 0 {
			if !increment {
				return title
			}
			unread = 1
		}

		return fmt.Sprintf("%s (*%d)", title, unread)
	}

	title = strings.Replace(title, found[0], "", -1)

	p = regexp.MustCompilePOSIX(`[0-9]*`)
	ds := p.FindAllString(found[0], -1)

	var d string
	for _, a := range ds {
		if a != "" {
			d = a
			break
		}
	}

	digit, err := strconv.Atoi(d)
	if err != nil {
		// Should never happen
		panic(err)
	}

	if !increment && !decrement {
		if unread == 0 {
			return title
		}
		return fmt.Sprintf("%s (*%d)", title, unread)
	}

	if increment {
		digit++
	}

	if decrement {
		digit--
	}

	if digit < 1 {
		return title
	}

	return fmt.Sprintf("%s (*%d)", title, digit)
}

func (w *tree) size() int {
	return len(w.getRoot().GetChildren())
}

func (w *tree) ref(node *tui.TreeNode) string {
	if node == nil {
		return ""
	}

	return fmt.Sprintf("%v", node.GetReference())
}

func (w *tree) getKey() string {
	n := w.Widget.GetCurrentNode()

	return w.ref(n)
}

func (w *tree) onChange(handler func(node *tui.TreeNode)) {
	w.Widget.SetChangedFunc(handler)
}

func (w *tree) increment() {
	w.Widget.Transform(tui.TransformNextItem)
}

func (w *tree) decrement() {
	w.Widget.Transform(tui.TransformPreviousItem)
}

func (w *tree) pageUp() {
	w.Widget.Transform(tui.TransformPreviousPage)
}

func (w *tree) pageDown() {
	w.Widget.Transform(tui.TransformNextPage)
}

func (w *tree) markReadByKey(key string) {
	node, _ := w.find(key, w.getRoot().GetChildren())
	if node == nil {
		return
	}
	node.SetColor(w.ReadColor)

	p, ok := w.ChildParent[key]
	if !ok {
		return
	}

	p.SetText(getTitle(p.GetText(), 0, false, true))
}

func (w *tree) markRead() {
	n := w.Widget.GetCurrentNode()
	c := n.GetChildren()
	if len(c) != 0 {
		n.SetText(getTitle(n.GetText(), 0, false, false))

		for _, cn := range c {
			cn.SetColor(w.ReadColor)
		}

		return
	}

	n.SetColor(w.ReadColor)

	p, ok := w.ChildParent[w.getKey()]
	if !ok {
		return
	}

	p.SetText(getTitle(p.GetText(), 0, false, true))
}

func (w *tree) markUnread() {
	n := w.Widget.GetCurrentNode()
	c := n.GetChildren()
	if len(c) != 0 {
		n.SetText(getTitle(n.GetText(), len(c), false, false))

		for _, cn := range c {
			cn.SetColor(w.TextColor)
		}

		return
	}

	n.SetColor(w.TextColor)

	p, ok := w.ChildParent[w.getKey()]
	if !ok {
		return
	}

	p.SetText(getTitle(p.GetText(), 0, true, false))
}

func (w *tree) toggle() {
	n := w.Widget.GetCurrentNode()
	if n.IsExpanded() {
		n.Collapse()
		return
	}
	n.Expand()
}

func (w *tree) insert(item *db.Item, hide bool, appendItem bool) {
	node, _ := w.find(item.FeedURL, w.getRoot().GetChildren())
	if node == nil {
		return
	}

	w.ChildParent[item.Link] = node

	n, read := w.convert(item)
	if hide && read {
		return
	}

	node.SetText(getTitle(node.GetText(), 1, true, false))
	if appendItem {
		node.AddChild(n)
		return
	}

	ch := append([]*tui.TreeNode{n}, node.GetChildren()...)
	//node.ClearChildren()
	node.SetChildren(ch)
}

func (w *tree) find(key string, nodes []*tui.TreeNode) (*tui.TreeNode, int) {
	for i, node := range nodes {
		if key == w.ref(node) {
			return node, i
		} else if n, j := w.find(key, node.GetChildren()); n != nil {
			return n, j
		}
	}
	return nil, 0
}

func (w *tree) removeFeedByKey(feed *db.Feed) string {
	node, i := w.find(feed.UpdateURL, w.getRoot().GetChildren())
	if node == nil {
		return ""
	}

	for k := range feed.Items {
		delete(w.ChildParent, k)
	}

	ch := w.getRoot().GetChildren()
	ch = append(ch[:i], ch[i+1:]...)
	//w.getRoot().ClearChildren()
	w.getRoot().SetChildren(ch)
	size := len(ch)

	var n *tui.TreeNode
	if size == 0 {
		return ""
	} else if size == 1 {
		n = ch[0]
	} else if i == 0 {
		n = ch[1]
	} else {
		n = ch[i-1]
	}
	w.Widget.SetCurrentNode(n)
	return w.ref(n)
}