forked from acaloiaro/di-tui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
194 lines (158 loc) · 4.6 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
package main
import (
"fmt"
"os"
"time"
"github.com/acaloiaro/di-tui/app"
"github.com/acaloiaro/di-tui/config"
"github.com/acaloiaro/di-tui/context"
"github.com/acaloiaro/di-tui/difm"
"github.com/acaloiaro/di-tui/views"
"github.com/rivo/tview"
"github.com/gdamore/tcell/v2"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var ctx *context.AppContext
func main() {
pflag.String("username", "", "your di.fm username")
pflag.String("password", "", "your di.fm password")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
username := viper.GetString("username")
password := viper.GetString("password")
var token string
if len(username) > 0 && len(password) > 0 {
difm.Authenticate(ctx, username, password)
}
token = config.GetToken()
if token == "" {
fmt.Println("Authenticate by running: di-tui --username USER --password PASSWORD\n\n" +
"Or, visit https://github.com/acaloiaro/di-tui#authenticate for other options.")
os.Exit(1)
}
ctx = context.CreateAppContext(views.CreateViewContext())
ctx.DifmToken = token
run()
}
func run() {
configureEventHandling()
updateScreenLayout()
FetchFavoritesAndChannels()
ctx.View.Keybindings.Bindings = views.GetKeybindings()
err := ctx.View.App.Run()
if err != nil {
panic(err)
}
}
func updateScreenLayout() {
focusView := ctx.View.App.GetFocus()
main := tview.NewFlex()
main.SetDirection(tview.FlexRow)
favsAndChannels := tview.NewFlex().
AddItem(ctx.View.FavoriteList, 0, 1, false).
AddItem(ctx.View.ChannelList, 0, 2, false).
SetDirection(tview.FlexRow)
primaryView := tview.NewFlex()
primaryView.
AddItem(favsAndChannels, 30, 0, false).
AddItem(ctx.View.NowPlaying, 0, 4, false)
if ctx.ShowStatus {
main.AddItem(ctx.View.Status, 4, 0, false)
}
main.
AddItem(primaryView, 0, 3, false).
AddItem(ctx.View.Keybindings, 4, 0, false)
if focusView == nil {
focusView = ctx.View.FavoriteList
}
ctx.View.App.
SetRoot(main, true).
SetFocus(focusView)
}
func configureEventHandling() {
ctx.View.App.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
focus := ctx.View.App.GetFocus().(*tview.List)
switch event.Rune() {
case 'c':
if focus != ctx.View.ChannelList {
ctx.View.App.SetFocus(ctx.View.ChannelList)
}
case 'f':
if focus != ctx.View.FavoriteList {
ctx.View.App.SetFocus(ctx.View.FavoriteList)
}
case 'F':
current := focus.GetCurrentItem()
if focus == ctx.View.ChannelList {
ctx.HighlightedChannel = &ctx.ChannelList[current]
} else {
highlightedFavorite := ctx.FavoriteList[current]
ctx.HighlightedChannel = difm.FavoriteItemChannel(ctx, highlightedFavorite)
}
difm.ToggleFavorite(ctx)
FetchFavoritesAndChannels()
case 'q':
ctx.View.App.Stop()
case 'j': //scroll down
current := focus.GetCurrentItem() + 1
focus.SetCurrentItem(current)
case 'k': //scroll up
current := focus.GetCurrentItem()
if current > 0 {
focus.SetCurrentItem(current - 1)
}
case 'p': // pause/resume
app.TogglePause(ctx)
}
return event
})
// keep 'now playing' up to date second-by-second
go func() {
c := time.Tick(1 * time.Second)
for range c {
elapsed := time.Since(ctx.View.NowPlaying.Track.StartTime)
// If the current time is past the end of the track, then a new track is playing and the now playing track needs
// to be refreshed.
if ctx.View.NowPlaying.Track.Duration > 0 && ctx.View.NowPlaying.Track.Duration < elapsed.Seconds() {
app.UpdateNowPlaying(ctx.CurrentChannel, ctx)
}
if ctx.CurrentChannel != nil && elapsed.Seconds() > 0 {
ctx.View.NowPlaying.Elapsed = elapsed.Seconds()
}
ctx.View.App.QueueUpdateDraw(func() {})
}
}()
// display the status pane when new status messages arrive
go func() {
for {
status := <-ctx.StatusChannel
ctx.View.Status.Message = status.Message
updateScreenLayout() // add the status pane to the screen
<-time.Tick(status.Duration)
ctx.ShowStatus = false
updateScreenLayout() // remove the status pane from the screen
}
}()
}
func FetchFavoritesAndChannels() {
ctx.View.ChannelList.Clear()
ctx.View.FavoriteList.Clear()
channels := difm.ListChannels(ctx)
for _, chn := range channels {
ctx.View.ChannelList.AddItem(chn.Name, "", 0, func() {
chn := channels[ctx.View.ChannelList.GetCurrentItem()]
app.PlayChannel(&chn, ctx)
})
}
favorites := difm.ListFavorites(ctx)
for _, fav := range favorites {
ctx.View.FavoriteList.AddItem(fav.Name, "", 0, func() {
f := favorites[ctx.View.FavoriteList.GetCurrentItem()]
chn := difm.FavoriteItemChannel(ctx, f)
app.PlayChannel(chn, ctx)
})
}
ctx.ChannelList = channels
ctx.FavoriteList = favorites
}