~chrisppy/beagles

ref: 175af2c65fd91aa2084dde988f7dfe536eb760bc beagles/ui.go -rw-r--r-- 8.9 KiB
175af2c6Chris Palmer Re-add badge in README 1 year, 10 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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// This file is part of beagles.
//
// Copyright © 2020 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 main

import (
	"fmt"
	"strings"

	"github.com/gdamore/tcell"
	tui "gitlab.com/tslocum/cview"
)

type statusType int

const (
	listStatus statusType = iota
	contentStatus
	commandStatus
	helpStatus
)

type ui struct {
	InCmdMode   bool
	Status      statusType
	App         *app
	CommandLine *commandLine
	List        *list
	Content     *content
	StatusLine  *statusLine
	DB          *Storage
	Pages       *tui.Pages
}

func initUI(db *Storage, theme themeType) *ui {
	i := &ui{
		DB:          db,
		Content:     newContent(&theme),
		List:        newList(&theme),
		StatusLine:  newStatusLine(&theme),
		CommandLine: newCommandLine(&theme),
		App:         newApp(),
		Pages:       tui.NewPages(),
	}

	i.App.processInput(func(event *tcell.EventKey) *tcell.EventKey {
		switch event.Key() {
		case tcell.KeyEsc:
			if i.InCmdMode {
				i.App.draw(func() {
					i.CommandLine.clear()
				})
				i.setStatus(i.Status)
			}

			return event
		case tcell.KeyLeft:
			return i.moveLeft(event)
		case tcell.KeyDown:
			return i.moveDown(event)
		case tcell.KeyUp:
			return i.moveUp(event)
		case tcell.KeyRight:
			return i.moveRight(event)
		case tcell.KeyRune:
			switch event.Rune() {
			case ':':
				if i.InCmdMode {
					return event
				}

				i.setStatus(commandStatus)
				return nil
			case 'h':
				return i.moveLeft(event)
			case 'j':
				return i.moveDown(event)
			case 'k':
				return i.moveUp(event)
			case 'l':
				return i.moveRight(event)
			case 'm':
				return i.markRead(event)
			case 'n':
				return i.openLink(event)
			}
		}

		return event
	})

	i.CommandLine.onChange(func(text string) {
		if text == "" {
			i.setStatus(listStatus)
		}
	})

	i.CommandLine.onEnter(func(key tcell.Key) {
		input := i.CommandLine.getText()

		args := strings.Split(input, " ")

		switch args[0] {
		case ":quit", ":q":
			i.App.exit()
		case ":help", ":h", ":?":
			i.setStatus(helpStatus)
		case ":list":
			i.setStatus(listStatus)
		case ":add":
			i.addFeed(args)
		case ":remove", ":rm":
			i.deleteFeed(args)
		case ":update", ":up":
			i.updateFeed(args)
		}
		i.InCmdMode = false
	})
	i.createHelpPage(&theme)
	i.createListPage(&theme)
	i.setStatus(listStatus)
	return i
}

func (i *ui) updateFeed(args []string) {
	i.App.draw(func() {
		if err := i.DB.Update(); err != nil {
			i.CommandLine.setError(err.Error())
			i.setStatus(listStatus)
			return
		}
		i.Content.clear()
		i.List.clear()
		for x, item := range i.DB.Queue {
			if x == 0 {
				i.Content.setText(item.Item.Link, item.Item.Content)
			}
			i.List.add(item.Item)
		}
		i.CommandLine.clear()
	})
}

func (i *ui) addFeed(args []string) {
	i.App.draw(func() {
		if err := i.DB.CreateFeed(args[1]); err != nil {
			i.CommandLine.setError(err.Error())
			i.setStatus(listStatus)
			return
		}
		i.Content.clear()
		i.List.clear()
		for x, item := range i.DB.Queue {
			if x == 0 {
				i.Content.setText(item.Item.Link, item.Item.Content)
			}
			i.List.add(item.Item)
		}
		i.CommandLine.clear()
	})
}
func (i *ui) deleteFeed(args []string) {
	i.App.draw(func() {
		if err := i.DB.DeleteFeed(args[1]); err != nil {
			i.CommandLine.setError(err.Error())
			i.setStatus(listStatus)
			return
		}
		i.Content.clear()
		i.List.clear()
		for x, item := range i.DB.Queue {
			if x == 0 {
				i.Content.setText(item.Item.Link, item.Item.Content)
			}
			i.List.add(item.Item)
		}
		i.CommandLine.clear()
	})
}

func (i *ui) openLink(event *tcell.EventKey) *tcell.EventKey {
	switch i.Status {
	case listStatus, contentStatus:
		if i.InCmdMode {
			return event
		}

		if i.List.size() != 0 {
			index := i.List.index()
			item := i.DB.Queue[index].Item

			go openBrowser(item.Link)
		}
		return nil
	}

	return event
}

func (i *ui) markRead(event *tcell.EventKey) *tcell.EventKey {
	switch i.Status {
	case listStatus, contentStatus:
		if i.InCmdMode || i.List.size() == 0 {
			return event
		}

		i.App.draw(func() {
			index := i.List.remove()
			if err := i.DB.MarkRead(i.DB.Queue[index]); err != nil {
				i.CommandLine.setError(err.Error())
				i.setStatus(listStatus)
				return
			}

			size := i.List.size()

			if size > 0 && index != -1 {
				switch index {
				case size:
					index--
					i.List.setIndex(index)
				case 0:
					i.List.setIndex(0)
				default:
					i.List.setIndex(index)
				}
				item := i.DB.Queue[index].Item
				i.Content.setText(item.Link, item.Content)
			}

			if size == 0 {
				i.Content.setText("", "")
			}
		})
		return nil
	}

	return event
}

func (i *ui) moveLeft(event *tcell.EventKey) *tcell.EventKey {
	switch i.Status {
	case contentStatus:
		if i.InCmdMode {
			return event
		}
		i.setStatus(listStatus)

		return nil
	}
	return event
}
func (i *ui) moveRight(event *tcell.EventKey) *tcell.EventKey {
	switch i.Status {
	case listStatus:
		if i.InCmdMode {
			return event
		}

		i.setStatus(contentStatus)

		return nil
	}
	return event
}
func (i *ui) moveUp(event *tcell.EventKey) *tcell.EventKey {
	switch i.Status {
	case listStatus:
		if i.InCmdMode {
			return event
		}

		index := i.List.decrement()
		if index != -1 {
			item := i.DB.Queue[index].Item
			i.Content.setText(item.Link, item.Content)
		}

		return nil
	}
	return event
}
func (i *ui) moveDown(event *tcell.EventKey) *tcell.EventKey {
	switch i.Status {
	case listStatus:
		if i.InCmdMode {
			return event
		}
		index := i.List.increment()
		if index != -1 {
			item := i.DB.Queue[index].Item
			i.Content.setText(item.Link, item.Content)
		}

		return nil
	}
	return event
}

func (i *ui) createListPage(theme *themeType) {
	qc := tui.NewFlex()
	qc.SetDirection(tui.FlexColumn)
	qc.AddItem(i.List.Widget, 0, 3, false)
	qc.AddItem(newSeparator(theme, true), 1, 0, false)
	qc.AddItem(i.Content.Widget, 0, 6, false)

	cv := tui.NewFlex()
	cv.SetDirection(tui.FlexRow)
	cv.AddItem(newTitleLine(theme), 1, 0, false)
	cv.AddItem(qc, 0, 1, false)
	cv.AddItem(i.StatusLine.Widget, 1, 1, false)
	cv.AddItem(i.CommandLine.Widget, 1, 0, false)

	view := tui.NewFlex()
	view.AddItem(cv, 0, 1, false)

	resize := true
	visable := true
	i.Pages.AddPage("list", view, resize, visable)
}

func (i *ui) createHelpPage(theme *themeType) {
	text := `beagles v%s is available under the terms of the GPL-3.0 license.
Copyright © 2020  Chris Palmer <chris@red-oxide.org>
https://git.sr.ht/~chrisppy/beagles

KEYS:
	h,🠔
		move to the left section (if applicable)
	
	j,🠗
		navigate down in the current section (if applicable)
	
	k,🠕
		navigate up in the current section (if applicable)
	
	l,🠖
		move the the right section (if applicable)
	
	m
		mark current post as read when in --LIST-- or --CONTENT--

	n
		open post url when in --LIST-- or --CONTENT--

	:
		enter command mode

COMMANDS:
	add [url]
		add a feed

	remove,rm [url]
		remove the feed

	update,up
		update all feeds

	help,h,?
		display help page

	list
		display list page
	
	quit,q
		exit the application

SEE ALSO:
	Additional information can be found in the manual pages:
		beagles(1)
		beagles-config(5)
`

	h := tui.NewTextView()
	h.SetText(fmt.Sprintf(text, Version))
	h.SetTextAlign(tui.AlignLeft)

	bgColor := theme.BackgroundColor
	fgColor := theme.ForegroundColor
	if theme.Help != nil {
		bgColor = theme.Help.BackgroundColor
		fgColor = theme.Help.ForegroundColor
	}

	if bgColor == "transparent" {
		h.SetBackgroundColor(tcell.ColorDefault)
	} else {
		h.SetBackgroundColor(tcell.GetColor(bgColor))
	}

	h.SetTextColor(tcell.GetColor(fgColor))

	cv := tui.NewFlex()
	cv.SetDirection(tui.FlexRow)
	cv.AddItem(newTitleLine(theme), 1, 0, false)
	cv.AddItem(h, 0, 1, false)
	cv.AddItem(i.StatusLine.Widget, 1, 1, false)
	cv.AddItem(i.CommandLine.Widget, 1, 0, false)

	view := tui.NewFlex()
	view.AddItem(cv, 0, 1, false)

	resize := true
	visable := true
	i.Pages.AddPage("help", view, resize, visable)
}

func (i *ui) setStatus(s statusType) {
	switch s {
	case commandStatus:
		i.InCmdMode = true
		i.CommandLine.setText(":")
		i.StatusLine.setText("-- COMMAND --")
		i.App.switchTo(i.CommandLine.Widget)
		return
	case contentStatus:
		i.InCmdMode = false
		i.StatusLine.setText("-- CONTENT --")
		i.App.switchTo(i.Content.Widget)
	case helpStatus:
		i.InCmdMode = false
		i.StatusLine.setText("-- HELP --")
		i.Pages.SwitchToPage("help")
	default:
		i.InCmdMode = false
		i.StatusLine.setText("-- LIST --")
		i.Pages.SwitchToPage("list")
	}
	i.Status = s
}