forked from And678/goPlayer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.go
326 lines (276 loc) · 7.11 KB
/
ui.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
319
320
321
322
323
324
325
326
package main
import (
"fmt"
termui "gopkg.in/gizak/termui.v2"
)
type uiState int
const (
Stopped uiState = iota
Playing
Paused
)
type selectCallback func(Song) (int, error)
type pauseCallback func(bool)
type seekCallback func(int) error
type volumeCallback func(int)
type Ui struct {
infoList *termui.List
playList *termui.List
scrollerGauge *termui.Gauge
volumeGauge *termui.Gauge
controlsPar *termui.Par
songs []Song
songNames []string
volume int
songNum int
songSel int
songPos int
songLen int
OnSelect selectCallback
OnPause pauseCallback
OnSeek seekCallback
OnVolume volumeCallback
state uiState
}
func NewUi(songList []Song, pathPrefix int) (*Ui, error) {
err := termui.Init()
if err != nil {
return nil, err
}
ui := new(Ui)
ui.volume = 100
ui.songs = songList
ui.songNum = -1
ui.infoList = termui.NewList()
ui.infoList.BorderLabel = "Song info"
ui.infoList.BorderFg = termui.ColorGreen
ui.infoList.Overflow = "wrap"
ui.playList = termui.NewList()
ui.playList.BorderLabel = "Playlist"
ui.playList.BorderFg = termui.ColorGreen
ui.scrollerGauge = termui.NewGauge()
ui.scrollerGauge.BorderLabel = "Stopped"
ui.scrollerGauge.Height = 3
ui.volumeGauge = termui.NewGauge()
ui.volumeGauge.BorderLabel = "Volume"
ui.volumeGauge.Height = 3
ui.volumeGauge.Percent = ui.volume
ui.controlsPar = termui.NewPar(
"[ Enter ](fg-black,bg-white)[Select](fg-black,bg-green) " +
"[ p ](fg-black,bg-white)[Play/Pause](fg-black,bg-green) " +
"[Esc](fg-black,bg-white)[Stop](fg-black,bg-green) " +
"[Right](fg-black,bg-white)[+10s](fg-black,bg-green) " +
"[Left](fg-black,bg-white)[-10s](fg-black,bg-green) " +
"[ + ](fg-black,bg-white)[+Volume](fg-black,bg-green) " +
"[ - ](fg-black,bg-white)[-Volume](fg-black,bg-green) " +
"[ q ](fg-black,bg-white)[Exit](fg-black,bg-green) ")
ui.controlsPar.Border = false
ui.controlsPar.Height = 1
termui.Body.AddRows(
termui.NewRow(
termui.NewCol(6, 0, ui.infoList, ui.scrollerGauge, ui.volumeGauge),
termui.NewCol(6, 0, ui.playList)),
termui.NewRow(
termui.NewCol(12, 0, ui.controlsPar)))
ui.realign()
termui.Handle("/sys/kbd/q", func(termui.Event) {
termui.StopLoop()
})
termui.Handle("/sys/kbd/p", func(termui.Event) {
if ui.songNum != -1 {
if ui.state == Playing {
ui.OnPause(true)
ui.state = Paused
} else {
ui.OnPause(false)
ui.state = Playing
}
ui.renderStatus()
}
})
termui.Handle("timer/1s", func(termui.Event) {
if ui.state == Playing {
ui.songPos++
if ui.songLen != 0 {
ui.scrollerGauge.Percent = int(float32(ui.songPos) / float32(ui.songLen) * 100)
ui.scrollerGauge.Label = fmt.Sprintf("%d:%.2d / %d:%.2d", ui.songPos/60, ui.songPos%60, ui.songLen/60, ui.songLen%60)
if ui.scrollerGauge.Percent >= 100 {
ui.songNum++
if ui.songNum >= len(ui.songs) {
ui.songNum = 0
}
ui.playSong(ui.songNum)
}
termui.Clear()
termui.Render(termui.Body)
}
} else if ui.state == Stopped {
ui.songPos = 0
}
})
termui.Handle("/sys/kbd/<right>", func(termui.Event) {
if ui.songNum != -1 {
ui.songPos += 10
ui.OnSeek(ui.songPos)
}
})
termui.Handle("/sys/kbd/<left>", func(termui.Event) {
if ui.songNum != -1 {
ui.songPos -= 10
if ui.songPos < 0 {
ui.songPos = 0
}
ui.OnSeek(ui.songPos)
}
})
termui.Handle("/sys/kbd/<escape>", func(termui.Event) {
ui.playSong(ui.songNum)
ui.OnPause(true)
ui.state = Stopped
ui.scrollerGauge.Percent = 0
ui.scrollerGauge.Label = "0:00 / 0:00"
ui.renderStatus()
})
termui.Handle("/sys/kbd/<enter>", func(termui.Event) {
ui.songNum = ui.songSel
ui.playSong(ui.songNum)
})
termui.Handle("/sys/kbd/<up>", func(termui.Event) {
ui.songUp()
termui.Clear()
termui.Render(termui.Body)
})
termui.Handle("/sys/kbd/=", func(termui.Event) {
ui.volumeUp()
})
termui.Handle("/sys/kbd/+", func(termui.Event) {
ui.volumeUp()
})
termui.Handle("/sys/kbd/-", func(termui.Event) {
ui.volumeDown()
})
termui.Handle("/sys/kbd/_", func(termui.Event) {
ui.volumeDown()
})
termui.Handle("/sys/kbd/<down>", func(termui.Event) {
ui.songDown()
termui.Clear()
termui.Render(termui.Body)
})
termui.Handle("/sys/wnd/resize", func(termui.Event) {
ui.realign()
})
ui.songNames = make([]string, len(ui.songs))
for i, v := range ui.songs {
if v.Metadata != nil {
ui.songNames[i] = fmt.Sprintf("[%d] %s - %s", i+1, v.Artist(), v.Title())
} else {
ui.songNames[i] = fmt.Sprintf("[%d] %s", i+1, v.path[pathPrefix:])
}
}
ui.playList.Items = ui.songNames
ui.setSong(0, false)
return ui, nil
}
func (ui *Ui) Start() {
termui.Loop()
}
func (ui *Ui) Close() {
termui.Close()
}
func (ui *Ui) playSong(number int) {
ui.songPos = 0
var err error
ui.songLen, err = ui.OnSelect(ui.songs[number])
if err == nil {
ui.state = Playing
ui.renderSong()
ui.renderStatus()
}
}
// Rendering
func (ui *Ui) realign() {
termHeight := termui.TermHeight()
ui.playList.Height = termHeight - ui.controlsPar.Height
ui.infoList.Height = termHeight - ui.controlsPar.Height - ui.scrollerGauge.Height - ui.volumeGauge.Height
termui.Body.Width = termui.TermWidth()
termui.Body.Align()
termui.Clear()
termui.Render(termui.Body)
}
func (ui *Ui) renderSong() {
if ui.songSel != -1 {
lyrics := ui.songs[ui.songSel].Lyrics()
trackNum, _ := ui.songs[ui.songSel].Track()
ui.infoList.Items = []string{
"[Artist:](fg-green) " + ui.songs[ui.songSel].Artist(),
"[Title:](fg-green) " + ui.songs[ui.songSel].Title(),
"[Album:](fg-green) " + ui.songs[ui.songSel].Album(),
fmt.Sprintf("[Track:](fg-green) %d", trackNum),
"[Genre:](fg-green) " + ui.songs[ui.songSel].Genre(),
fmt.Sprintf("[Year:](fg-green) %d", ui.songs[ui.songSel].Year()),
}
if lyrics != "" {
ui.infoList.Items = append(ui.infoList.Items, "Lyrics: "+lyrics)
}
} else {
ui.infoList.Items = []string{}
}
termui.Clear()
termui.Render(termui.Body)
}
func (ui *Ui) renderStatus() {
var status string
switch ui.state {
case Playing:
status = "[(Playing)](fg-black,bg-green)"
case Paused:
status = "[(Paused)](fg-black,bg-yellow)"
case Stopped:
status = "[(Stopped)](fg-black,bg-red)"
}
ui.scrollerGauge.BorderLabel = status
termui.Clear()
termui.Render(termui.Body)
}
//Song selection
func (ui *Ui) songDown() {
if ui.songSel < len(ui.songNames)-1 {
ui.setSong(ui.songSel+1, true)
}
}
func (ui *Ui) songUp() {
if ui.songSel > 0 {
ui.setSong(ui.songSel-1, true)
}
}
func (ui *Ui) volumeUp() {
if ui.volume < 100 {
ui.volume += 5
}
ui.volumeGauge.Percent = ui.volume
ui.OnVolume(ui.volume)
termui.Clear()
termui.Render(termui.Body)
}
func (ui *Ui) volumeDown() {
if ui.volume > 0 {
ui.volume -= 5
}
ui.volumeGauge.Percent = ui.volume
ui.OnVolume(ui.volume)
termui.Clear()
termui.Render(termui.Body)
}
func (ui *Ui) setSong(num int, unset bool) {
skip := 0
for num-skip >= ui.playList.Height-2 {
skip += ui.playList.Height - 2
}
if unset {
ui.songNames[ui.songSel] = ui.songNames[ui.songSel][1 : len(ui.songNames[ui.songSel])-20]
}
ui.songSel = num
ui.songNames[num] = fmt.Sprintf("[%s](fg-black,bg-green)", ui.songNames[num])
ui.playList.Items = ui.songNames[skip:]
}