~chrisppy/beagles

ref: 880621c285b8eeca18abc334a79c5e25347005b3 beagles/ui/content.go -rw-r--r-- 3.0 KiB
880621c2Chris Palmer make content pages adhere to config keys 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
// 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 (
	"strings"
	"time"

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

type content struct {
	Widget   *tui.TextView
	TxtColor tcell.Color
	ErrColor tcell.Color
}

func (i *UI) newContent() *content {
	view := tui.NewTextView()
	view.SetTextAlign(tui.AlignLeft)
	view.SetScrollable(true)
	view.SetWrap(true)
	view.SetWordWrap(true)
	view.SetDynamicColors(true)
	view.SetBackgroundColor(i.Config.Theme.ContentBGColor)
	view.SetTextColor(i.Config.Theme.ContentFGColor)
	view.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { return nil })

	return &content{
		Widget:   view,
		TxtColor: i.Config.Theme.ContentFGColor,
		ErrColor: i.Config.Theme.ErrColor,
	}
}
func (w *content) increment() {
	r, c := w.Widget.GetScrollOffset()
	w.Widget.ScrollTo(r+1, c)
}

func (w *content) decrement() {
	r, c := w.Widget.GetScrollOffset()
	w.Widget.ScrollTo(r-1, c)
}

func (w *content) pageUp() {
	r, c := w.Widget.GetScrollOffset()
	s := w.Widget.GetPageSize()
	w.Widget.ScrollTo(r-s, c)
}

func (w *content) pageDown() {
	r, c := w.Widget.GetScrollOffset()
	s := w.Widget.GetPageSize()
	w.Widget.ScrollTo(r+s, c)
}

func (w *content) setDescription(text string, updated *time.Time, isHTML bool) {
	// TODO: Handle gemini content to text
	w.Widget.SetTextColor(w.TxtColor)

	var sb strings.Builder

	if updated != nil {
		sb.WriteString("Feed Updated: ")
		sb.WriteString(updated.Format("2006-01-02"))
		sb.WriteByte('\n')
		sb.WriteByte('\n')
	}

	sb.WriteString(text)

	w.Widget.SetText(sb.String())
	w.Widget.ScrollToBeginning()
}

func (w *content) setText(item *db.Item) {
	// TODO: Handle gemini content to text
	w.Widget.SetTextColor(w.TxtColor)

	if item == nil {
		w.Widget.SetText("")
		return
	}

	var sb strings.Builder
	if item.Title != "" {
		sb.WriteString(item.Title)
		sb.WriteByte('\n')
		sb.WriteByte('\n')
	}

	sb.WriteString(item.Date.Format("2006-01-02"))
	sb.WriteByte('\n')
	sb.WriteByte('\n')

	if item.Link != "" {
		sb.WriteString(item.Link)
		sb.WriteByte('\n')
		sb.WriteByte('\n')
	}

	c := item.Content
	if c == "" {
		sb.WriteString("No Content")
		sb.WriteByte('\n')
	} else {
		sb.WriteString(c)
		sb.WriteByte('\n')
	}

	w.Widget.SetText(sb.String())
	w.Widget.ScrollToBeginning()
}

func (w *content) clear() {
	w.Widget.Clear()
}