forked from SoMuchForSubtlety/f1viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
264 lines (224 loc) · 6.11 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/SoMuchForSubtlety/keyring"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"gopkg.in/alecthomas/kingpin.v2"
)
var activeTheme = struct {
CategoryNodeColor tcell.Color
FolderNodeColor tcell.Color
ItemNodeColor tcell.Color
ActionNodeColor tcell.Color
LoadingColor tcell.Color
LiveColor tcell.Color
MultiCommandColor tcell.Color
UpdateColor tcell.Color
NoContentColor tcell.Color
InfoColor tcell.Color
ErrorColor tcell.Color
TerminalAccentColor tcell.Color
TerminalTextColor tcell.Color
}{
CategoryNodeColor: tcell.ColorOrange,
FolderNodeColor: tcell.ColorWhite,
ItemNodeColor: tcell.ColorLightGreen,
ActionNodeColor: tcell.ColorDarkCyan,
LoadingColor: tcell.ColorDarkCyan,
LiveColor: tcell.ColorRed,
MultiCommandColor: tcell.ColorAquaMarine,
UpdateColor: tcell.ColorDarkRed,
NoContentColor: tcell.ColorOrangeRed,
InfoColor: tcell.ColorGreen,
ErrorColor: tcell.ColorRed,
TerminalAccentColor: tcell.ColorGreen,
TerminalTextColor: tview.Styles.PrimaryTextColor,
}
type viewerSession struct {
cfg config
ring keyring.Keyring
username string
password string
authtoken string
// tview
app *tview.Application
textWindow *tview.TextView
tree *tview.TreeView
commands map[string]bool
}
var (
version = "dev"
commit = ""
date = ""
)
func main() {
app := kingpin.New("f1viewer", "a TUI client for F1TV")
app.Version(buildVersion(version, commit, date))
app.VersionFlag.Short('v')
app.HelpFlag.Short('h')
kingpin.MustParse(app.Parse(os.Args[1:]))
session, logfile, err := newSession()
defer logfile.Close()
if err != nil {
fmt.Println("[ERROR]", err)
log.Fatal(err)
}
go func() {
if err := session.app.Run(); err != nil {
log.Fatal(err)
}
os.Exit(0)
}()
go session.checkCommands("vlc", "mpv")
go session.checkLive()
go session.CheckUpdate()
// set vod types nodes
session.tree.GetRoot().AddChild(session.getCollectionsNode())
nodes, err := session.getVodTypeNodes()
if err != nil {
session.logError(err)
} else {
appendNodes(session.tree.GetRoot(), nodes...)
session.app.Draw()
}
logOutNode := tview.NewTreeNode("Log Out")
logOutNode = logOutNode.SetColor(activeTheme.ActionNodeColor)
logOutNode = logOutNode.SetSelectedFunc(func() {
session.logout()
session.initUIWithForm()
})
session.tree.GetRoot().AddChild(logOutNode)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
}
func newSession() (*viewerSession, *os.File, error) {
var err error
session := &viewerSession{}
session.commands = make(map[string]bool)
session.cfg, err = loadConfig()
if err != nil {
return nil, nil, fmt.Errorf("Could not open config: %w", err)
}
logFile, err := configureLogging(session.cfg)
if err != nil {
return nil, nil, err
}
err = session.openRing()
if err != nil {
session.logError(fmt.Errorf("Could not access credential store: %w", err))
}
err = session.loadCredentials()
if err != nil {
session.logError(err)
}
session.app = tview.NewApplication()
session.app.EnableMouse(true)
root := tview.NewTreeNode("Categories").SetSelectable(false)
root.AddChild(session.getFullSessionsNode())
session.tree = tview.NewTreeView().
SetRoot(root).
SetCurrentNode(root).
SetTopLevel(1)
session.textWindow = tview.NewTextView().
SetWordWrap(false).
SetWrap(false).
SetDynamicColors(true).
SetChangedFunc(func() { session.app.Draw() })
session.textWindow.SetBorder(true)
session.tree.SetSelectedFunc(session.toggleVisibility)
token, err := session.login()
if err != nil {
session.initUIWithForm()
} else {
session.logInfo("logged in!")
session.authtoken = token
session.initUI()
}
return session, logFile, nil
}
func (session *viewerSession) initUIWithForm() {
form := tview.NewForm().
AddInputField("username", session.username, 30, nil, session.updateUsername).
AddPasswordField("password", "", 30, '*', session.updatePassword).
AddButton("test", session.testAuth).
AddButton("save", session.closeForm)
formTreeFlex := tview.NewFlex()
if !session.cfg.HorizontalLayout {
formTreeFlex.SetDirection(tview.FlexRow)
}
if session.cfg.HorizontalLayout {
formTreeFlex.
AddItem(form, 50, 0, true).
AddItem(session.tree, 0, 1, false)
} else {
formTreeFlex.
AddItem(form, 7, 0, true).
AddItem(session.tree, 0, 1, false)
}
masterFlex := tview.NewFlex()
if session.cfg.HorizontalLayout {
masterFlex.SetDirection(tview.FlexRow)
}
masterFlex.
AddItem(formTreeFlex, 0, session.cfg.TreeRatio, true).
AddItem(session.textWindow, 0, session.cfg.OutputRatio, false)
session.app.SetRoot(masterFlex, true)
}
func (session *viewerSession) initUI() {
flex := tview.NewFlex().
AddItem(session.tree, 0, session.cfg.TreeRatio, true).
AddItem(session.textWindow, 0, session.cfg.OutputRatio, false)
if session.cfg.HorizontalLayout {
flex.SetDirection(tview.FlexRow)
}
session.app.SetRoot(flex, true)
}
func (session *viewerSession) closeForm() {
session.testAuth()
err := session.saveCredentials()
if err != nil {
session.logError(err)
}
session.initUI()
}
func (session *viewerSession) checkLive() {
for {
session.logInfo("checking for live session")
isLive, liveNode, err := session.getLiveNode()
if err != nil {
session.logError("error looking for live session: ", err)
if session.cfg.LiveRetryTimeout <= 0 {
return
}
} else if isLive {
insertNodeAtTop(session.tree.GetRoot(), liveNode)
if session.app != nil {
session.app.Draw()
}
return
} else if session.cfg.LiveRetryTimeout <= 0 {
session.logInfo("no live session found")
return
} else {
session.logInfo("no live session found")
}
time.Sleep(time.Second * time.Duration(session.cfg.LiveRetryTimeout))
}
}
func buildVersion(version, commit, date string) string {
result := fmt.Sprintf("Version: %s", version)
if commit != "" {
result += fmt.Sprintf("\nGit commit: %s", commit)
}
if date != "" {
result += fmt.Sprintf("\nBuilt: %s", date)
}
return result
}