-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm.go
318 lines (283 loc) · 8.05 KB
/
term.go
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
package main
import (
"errors"
"github.com/nsf/termbox-go"
"strconv"
"strings"
"unicode"
)
// Colors is a map of all the colors available through termbox.
var colors = map[rune]termbox.Attribute{
'd': termbox.ColorDefault, 'D': termbox.ColorDefault | termbox.AttrBold,
'k': termbox.ColorBlack, 'K': termbox.ColorBlack | termbox.AttrBold,
'r': termbox.ColorRed, 'R': termbox.ColorRed | termbox.AttrBold,
'g': termbox.ColorGreen, 'G': termbox.ColorGreen | termbox.AttrBold,
'y': termbox.ColorYellow, 'Y': termbox.ColorYellow | termbox.AttrBold,
'b': termbox.ColorBlue, 'B': termbox.ColorBlue | termbox.AttrBold,
'm': termbox.ColorMagenta, 'M': termbox.ColorMagenta | termbox.AttrBold,
'c': termbox.ColorCyan, 'C': termbox.ColorCyan | termbox.AttrBold,
'w': termbox.ColorWhite, 'W': termbox.ColorWhite | termbox.AttrBold,
}
// Tabber is an interface used by the different tabs.
type Tabber interface {
Name() string
Status() string
HandleKeyEvent(*termbox.Event)
Draw()
Query(string)
}
// Apollo is the main object of the application.
type Apollo struct {
running bool
width int
height int
events chan termbox.Event
c *Configuration
d *Database
currentTab int
tabs []Tabber
input []rune
inputCursor int
inputActive bool
}
// NewApollo creates a new Apollo, initializing a new Configuration and new Database in the process.
// It opens the default tabs and then returns itself.
func newApollo() *Apollo {
width, height := termbox.Size()
var tabs []Tabber
a := &Apollo{
running: true,
width: width,
height: height,
events: make(chan termbox.Event, 20),
c: newConfiguration(),
d: newDatabase(),
tabs: tabs,
}
a.tabs = append(a.tabs, Tabber(newStatusTab(a)))
autoOpenTabs := strings.Split(a.c.get("tabs-startup"), ",")
for _, name := range autoOpenTabs {
a.openTab(name)
}
a.printWelcome()
a.currentTab = 0
return a
}
// HandleEvent changes the size of the terminal if it's been resized. On all other types of events,
// it sends them to the key handler. It returns in case of an error.
func (a *Apollo) handleEvent(ev *termbox.Event) error {
switch ev.Type {
case termbox.EventKey:
a.handleKeyEvent(ev)
case termbox.EventResize:
a.width, a.height = termbox.Size()
case termbox.EventError:
return ev.Err
}
return nil
}
// HandleKeyEvent handles the current key event. It will handle events related to the input bar
// and the changes of current tab. If none of those happens, it'll forward the event to the
// current tab's event handler.
func (a *Apollo) handleKeyEvent(ev *termbox.Event) {
if ev.Mod == termbox.ModAlt {
indexes := map[rune]int{'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
if i, exist := indexes[ev.Ch]; exist {
if len(a.tabs) > i {
a.currentTab = i
a.tabs[a.currentTab].Query("!focused")
}
}
return
}
switch ev.Key {
case termbox.KeyCtrlC:
a.running = false
case termbox.KeyEnter:
if len(a.input) > 0 {
if a.input[0] == '/' {
a.handleCommand()
} else if a.currentTab != 0 {
a.tabs[a.currentTab].Query(string(a.input))
}
a.input = a.input[:0]
a.inputCursor = 0
} else {
a.inputActive = !a.inputActive
}
}
if a.inputActive {
switch ev.Key {
case termbox.KeyBackspace, termbox.KeyBackspace2:
if a.inputCursor > 0 {
a.input = append(a.input[:a.inputCursor-1], a.input[a.inputCursor:]...)
a.inputCursor--
}
case termbox.KeySpace:
a.input = append(a.input, ' ')
copy(a.input[a.inputCursor+1:], a.input[a.inputCursor:])
a.input[a.inputCursor] = ' '
a.inputCursor++
case termbox.KeyArrowLeft:
a.inputCursor--
if a.inputCursor < 0 {
a.inputCursor = 0
}
case termbox.KeyArrowRight:
a.inputCursor++
if a.inputCursor > len(a.input) {
a.inputCursor = len(a.input)
}
default:
if unicode.IsPrint(ev.Ch) {
a.input = append(a.input, ' ')
copy(a.input[a.inputCursor+1:], a.input[a.inputCursor:])
a.input[a.inputCursor] = ev.Ch
a.inputCursor++
}
}
} else {
a.tabs[a.currentTab].HandleKeyEvent(ev)
}
}
// DrawString draws a given string on a given row.
func (a *Apollo) drawString(x, y int, str string) {
fg := colors['d']
runes := []rune(str)
for i := 0; i < len(runes); i++ {
if runes[i] == '{' {
fg = colors[runes[i+1]]
i += 3
}
termbox.SetCell(x, y, runes[i], fg, colors['d'])
x++
}
}
// DrawStringRightAlign draws a string aligned to the right.
func (a *Apollo) drawStringRightAlign(x, y int, str string) {
strLen := 0
runes := []rune(str)
for i := 0; i < len(runes); i++ {
if runes[i] == '{' {
i += 2
} else {
strLen++
}
}
a.drawString(x-strLen, y, str)
}
// DrawStatusBars draws the background color of the two status rows.
func (a *Apollo) drawStatusBars() {
for i := 0; i < a.width; i++ {
termbox.SetCell(i, 0, ' ', colors['d'], colors['b'])
termbox.SetCell(i, a.height-2, ' ', colors['d'], colors['b'])
}
}
// DrawTopStatus draws the top status row.
func (a *Apollo) drawTopStatus() {
runes := []rune(version + " - " + a.tabs[a.currentTab].Status())
for i := 0; i < len(runes); i++ {
termbox.SetCell(i, 0, runes[i], colors['W'], colors['b'])
}
}
// DrawBottomStatus draws the tab status row of the terminal.
func (a *Apollo) drawBottomStatus() {
var str string
for i := range a.tabs {
if i == a.currentTab {
str += "{" + strconv.Itoa(i) + "." + a.tabs[i].Name() + "} "
} else {
str += strconv.Itoa(i) + "." + a.tabs[i].Name() + " "
}
}
fg := colors['w']
x := 0
runes := []rune(str)
for i := 0; i < len(runes); i++ {
if runes[i] == '{' {
fg = colors['W']
i++
} else if runes[i] == '}' {
fg = colors['w']
i++
}
termbox.SetCell(x, a.height-2, runes[i], fg, colors['b'])
x++
}
}
// DrawInput draws the input row of the terminal.
func (a *Apollo) drawInput() {
if len(a.input) < a.width {
for i := 0; i < len(a.input); i++ {
termbox.SetCell(i, a.height-1, a.input[i], colors['w'], colors['d'])
}
} else {
offset := len(a.input) - a.width + 1
for i := 0; i < a.width-1; i++ {
termbox.SetCell(i, a.height-1, a.input[i+offset], colors['w'], colors['d'])
}
}
if a.inputActive {
termbox.SetCursor(a.inputCursor, a.height-1)
} else {
termbox.HideCursor()
}
}
// Draw calls the different drawing functions for the terminal.
func (a *Apollo) draw() {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
a.tabs[a.currentTab].Draw()
a.drawStatusBars()
a.drawTopStatus()
a.drawBottomStatus()
a.drawInput()
termbox.Flush()
}
// Log prints a message to the logs.
func (a *Apollo) log(str string) {
a.tabs[0].Query(str)
}
// LogError prints a message to the logs and stderr.
func (a *Apollo) logError(str string) {
a.log("{r}│ ERROR: {d}" + str)
}
// LogDebug logs the given string if the debug flag is on.
func (a *Apollo) logDebug(str string) {
if a.c.get("debug") == "true" {
a.log("{y}│ DEBUG: {d}" + str)
}
}
// OpenTab opens the given tab, if it's not already opened. If it is, it'll switch to it.
func (a *Apollo) openTab(name string) error {
for i := range a.tabs {
if a.tabs[i].Name() == name {
a.currentTab = i
return nil
}
}
switch name {
case "movies":
a.tabs = append(a.tabs, Tabber(newEntriesTab(a, &a.d.Movies, "movies", "default", "")))
case "series":
a.tabs = append(a.tabs, Tabber(newEntriesTab(a, &a.d.Series, "series", "episodic", "")))
case "anime":
a.tabs = append(a.tabs, Tabber(newEntriesTab(a, &a.d.Anime, "anime", "episodic", "")))
case "games":
a.tabs = append(a.tabs, Tabber(newEntriesTab(a, &a.d.Games, "games", "additional", "platform")))
case "books":
a.tabs = append(a.tabs, Tabber(newEntriesTab(a, &a.d.Books, "books", "additional", "author")))
default:
return errors.New("term: tab does not exist")
}
a.currentTab = len(a.tabs) - 1
return nil
}
// CloseCurrentTab closes the tab currently being viewed, with the exception of the logs tab.
func (a *Apollo) closeCurrentTab() error {
if a.tabs[a.currentTab].Name() == "(status)" {
return errors.New("term: cannot close status tab")
}
a.tabs = append(a.tabs[:a.currentTab], a.tabs[a.currentTab+1:]...)
a.currentTab--
return nil
}