~chrisppy/titan

ref: 723d8c78cdfebe669e51d6e5396c78b05941aabc titan/ui.go -rw-r--r-- 9.2 KiB
723d8c78Chris Palmer set tab colors based on content and title colors 3 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
// This file is part of titan.
//
// 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 main

import (
	"fmt"
	"strconv"
	"strings"

	"code.rocketnine.space/tslocum/cbind"
	"code.rocketnine.space/tslocum/cview"
	"github.com/gdamore/tcell/v2"
)

type mode int

const (
	normalMode mode = iota
	commandMode
	inputMode
)

type status int

const (
	helpStatus status = iota
	browserStatus
)

type ui struct {
	InputMode    mode
	InputLink    string
	Status       status
	Bindings     *cbind.Configuration
	Application  *cview.Application
	MainView     *cview.Flex
	View         *cview.Flex
	TitleLine    *cview.TextView
	StatusLine   *cview.TextView
	CommandLine  *cview.InputField
	Panels       *cview.Panels
	HelpPanel    helpPanel
	BrowserPanel browserPanel
}

type helpPanel struct {
	View    *cview.Flex
	Content *cview.TextView
}

type tab struct {
	Name         string
	CurrentIndex int
	Content      *cview.TextView
	Capsules     []*capsule
}

type capsule struct {
	Label string
	URL   string
	Host  string
	Text  string
	Links []string
}

type browserPanel struct {
	View         *cview.Flex
	TabbedPanels *cview.TabbedPanels
	TabNames     []string
	Tabs         map[string]*tab
	CurrentIndex int
}

func (a *app) loadUI(links []string) {
	a.UI = &ui{
		InputMode:   normalMode,
		Status:      browserStatus,
		Bindings:    cbind.NewConfiguration(),
		Application: cview.NewApplication(),
		MainView:    cview.NewFlex(),
		View:        cview.NewFlex(),
		TitleLine:   cview.NewTextView(),
		StatusLine:  cview.NewTextView(),
		CommandLine: cview.NewInputField(),
		Panels:      cview.NewPanels(),
	}

	cview.Styles.ScrollBarColor = a.Config.Theme.TitleBGColor
	cview.Styles.PrimaryTextColor = a.Config.Theme.ContentTextColor
	cview.Styles.PrimitiveBackgroundColor = a.Config.Theme.ContentBGColor

	a.addBindings()
	a.UI.Application.SetInputCapture(a.UI.Bindings.Capture)

	a.loadHelpPanel()
	a.loadBrowserPanel(links)

	a.UI.TitleLine.SetTextAlign(cview.AlignCenter)
	a.UI.TitleLine.SetText("titan")
	a.UI.TitleLine.SetBackgroundColor(a.Config.Theme.TitleBGColor)
	a.UI.TitleLine.SetTextColor(a.Config.Theme.TitleFGColor)

	a.UI.StatusLine.SetTextAlign(cview.AlignLeft)
	a.UI.StatusLine.SetText("-- BROWSER --")
	a.UI.StatusLine.SetBackgroundColor(a.Config.Theme.StatusBGColor)
	a.UI.StatusLine.SetTextColor(a.Config.Theme.StatusFGColor)

	a.UI.CommandLine.SetFieldBackgroundColor(a.Config.Theme.CMDBGColor)
	a.UI.CommandLine.SetFieldTextColor(a.Config.Theme.CMDFGColor)
	a.UI.CommandLine.SetChangedFunc(func(text string) {
		if text == "" && a.UI.InputMode != normalMode && a.UI.InputMode != inputMode {
			a.UI.Application.QueueUpdateDraw(func() {
				switch a.UI.Status {
				case helpStatus:
					a.UI.StatusLine.SetText("-- HELP --")
					a.UI.Application.SetFocus(a.UI.HelpPanel.Content)
				case browserStatus:
					a.UI.StatusLine.SetText("-- BROWSER --")
					a.UI.Application.SetFocus(a.UI.BrowserPanel.TabbedPanels)
				}
			})
			a.UI.InputLink = ""
			a.UI.InputMode = normalMode
		}
	})
	a.UI.CommandLine.SetDoneFunc(func(key tcell.Key) {
		input := strings.TrimSpace(a.UI.CommandLine.GetText())

		a.UI.Application.QueueUpdateDraw(func() {
			a.UI.CommandLine.SetText("")
		})

		switch a.UI.InputMode {
		case inputMode:
			a.openCapsule(a.UI.InputLink, a.UI.CommandLine.GetText())

			a.UI.Application.QueueUpdateDraw(func() {
				a.UI.CommandLine.SetText("")
				a.UI.CommandLine.SetFieldTextColor(a.Config.Theme.CMDFGColor)
				switch a.UI.Status {
				case helpStatus:
					a.UI.StatusLine.SetText("-- HELP --")
					a.UI.Application.SetFocus(a.UI.HelpPanel.Content)
				case browserStatus:
					a.UI.StatusLine.SetText("-- BROWSER --")
					a.UI.Application.SetFocus(a.UI.BrowserPanel.TabbedPanels)
				}
				a.UI.CommandLine.SetMaskCharacter(0)
			})

			a.UI.InputLink = ""
			a.UI.InputMode = normalMode
		case commandMode:
			args := strings.Split(input, " ")
			switch args[0] {
			case ":open":
				a.UI.Application.QueueUpdateDraw(func() {
					a.UI.Status = browserStatus
					a.UI.StatusLine.SetText("-- BROWSER --")
					a.UI.Panels.SetCurrentPanel("browser")
					a.UI.Application.SetFocus(a.UI.BrowserPanel.TabbedPanels)

					if len(args) != 2 {
						a.UI.CommandLine.SetFieldTextColor(a.Config.Theme.ErrColor)
						a.UI.CommandLine.SetText("remove must contain at most one path")
						return
					}

					c, err := a.getCapsule(args[1], "")
					if err != nil {
						a.UI.CommandLine.SetFieldTextColor(a.Config.Theme.ErrColor)
						a.UI.CommandLine.SetText(err.Error())
						return
					} else if c == nil {
						return
					}

					name := a.UI.BrowserPanel.TabbedPanels.GetCurrentTab()
					if c.Label == "" {
						c.Label = name
					}

					a.UI.BrowserPanel.TabbedPanels.SetTabLabel(name, c.Label)
					a.UI.BrowserPanel.Tabs[name].Content.SetText(c.Text)
					a.UI.BrowserPanel.Tabs[name].Content.ScrollToBeginning()
					a.UI.BrowserPanel.Tabs[name].CurrentIndex++
					a.UI.BrowserPanel.Tabs[name].Capsules = append(a.UI.BrowserPanel.Tabs[name].Capsules[:a.UI.BrowserPanel.Tabs[name].CurrentIndex], c)
				})
			case ":open-tab":
				a.UI.Status = browserStatus
				a.UI.StatusLine.SetText("-- BROWSER --")
				a.UI.Panels.SetCurrentPanel("browser")
				a.UI.Application.SetFocus(a.UI.BrowserPanel.TabbedPanels)

				if len(args) != 2 {
					a.UI.CommandLine.SetFieldTextColor(a.Config.Theme.ErrColor)
					a.UI.CommandLine.SetText("open-tab must contain at most one path")
					return
				}

				t := tab{
					Content:      cview.NewTextView(),
					CurrentIndex: 0,
					Capsules:     make([]*capsule, 0),
				}
				t.Content.SetPadding(0, 0, 6, 6)
				t.Content.SetTextAlign(cview.AlignLeft)
				t.Content.SetScrollable(true)
				t.Content.SetWrap(true)
				t.Content.SetWordWrap(true)
				t.Content.SetDynamicColors(true)
				t.Content.SetBackgroundColor(a.Config.Theme.ContentBGColor)
				t.Content.SetTextColor(a.Config.Theme.ContentTextColor)
				t.Content.ScrollToBeginning()

				i := 0
				for {
					n := strconv.Itoa(i)
					if _, ok := a.UI.BrowserPanel.Tabs[n]; !ok {
						t.Name = n
						break
					}
					i++
				}

				c, err := a.getCapsule(args[1], "")
				if err != nil {
					a.UI.CommandLine.SetFieldTextColor(a.Config.Theme.ErrColor)
					a.UI.CommandLine.SetText(fmt.Sprintf("unable to open capsule %s", err.Error()))
					return
				}

				if c.Label == "" {
					c.Label = t.Name
				}
				t.Content.SetText(c.Text)

				t.Capsules = append(t.Capsules, c)

				a.UI.BrowserPanel.TabNames = append(a.UI.BrowserPanel.TabNames, t.Name)

				a.UI.BrowserPanel.TabbedPanels.AddTab(t.Name, c.Label, t.Content)
				a.UI.BrowserPanel.Tabs[t.Name] = &t
				a.UI.BrowserPanel.CurrentIndex = len(a.UI.BrowserPanel.TabNames) - 1
				a.UI.BrowserPanel.TabbedPanels.SetCurrentTab(a.UI.BrowserPanel.TabNames[a.UI.BrowserPanel.CurrentIndex])
			case ":trust":
				switch a.UI.Status {
				case browserStatus:
					if t, ok := a.UI.BrowserPanel.Tabs[a.UI.BrowserPanel.TabbedPanels.GetCurrentTab()]; ok {
						if host, ok := a.Gemini.Hosts.Lookup(t.Capsules[t.CurrentIndex].Host); ok {
							if err := a.Gemini.HostsFile.WriteHost(host); err != nil {
								a.UI.Application.QueueUpdateDraw(func() {
									a.UI.CommandLine.SetFieldTextColor(a.Config.Theme.ErrColor)
									a.UI.CommandLine.SetText(fmt.Sprintf("error trusting site %s", err.Error()))
								})
							}
						}
					}

				}
			case ":help", ":h":
				a.UI.Application.QueueUpdateDraw(func() {
					a.UI.Status = helpStatus
					a.UI.StatusLine.SetText("-- HELP --")
					a.UI.Panels.SetCurrentPanel("help")
					a.UI.Application.SetFocus(a.UI.HelpPanel.Content)
				})
			case ":quit", ":q":
				a.UI.Application.Stop()
			}

			a.UI.InputMode = normalMode
		}

	})

	a.UI.View.SetDirection(cview.FlexRow)
	a.UI.View.AddItem(a.UI.TitleLine, 1, 0, false)
	a.UI.View.AddItem(a.UI.Panels, 0, 1, false)
	a.UI.View.AddItem(a.UI.StatusLine, 1, 1, false)
	a.UI.View.AddItem(a.UI.CommandLine, 1, 0, false)

	a.UI.MainView.AddItem(a.UI.View, 0, 1, false)
}

func (a *app) openCapsule(link, query string) {
	a.UI.Application.QueueUpdateDraw(func() {
		c, err := a.getCapsule(link, query)
		if err != nil {
			a.UI.CommandLine.SetFieldTextColor(a.Config.Theme.ErrColor)
			a.UI.CommandLine.SetText(err.Error())
			return
		} else if c == nil {
			return
		}

		name := a.UI.BrowserPanel.TabbedPanels.GetCurrentTab()
		if c.Label == "" {
			c.Label = name
		}

		a.UI.BrowserPanel.TabbedPanels.SetTabLabel(name, c.Label)
		a.UI.BrowserPanel.Tabs[name].Content.SetText(c.Text)
		a.UI.BrowserPanel.Tabs[name].Content.ScrollToBeginning()
		a.UI.BrowserPanel.Tabs[name].CurrentIndex++
		a.UI.BrowserPanel.Tabs[name].Capsules = append(a.UI.BrowserPanel.Tabs[name].Capsules[:a.UI.BrowserPanel.Tabs[name].CurrentIndex], c)
	})
}