2020-06-22 00:05:56 +08:00
|
|
|
// Copyright (C) 2020 Raziman
|
|
|
|
|
2020-06-19 16:22:20 +08:00
|
|
|
package main
|
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2020-06-19 16:22:20 +08:00
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
"github.com/rivo/tview"
|
|
|
|
)
|
|
|
|
|
2020-06-26 12:54:48 +08:00
|
|
|
type PlayingBar struct {
|
|
|
|
*tview.Frame
|
2020-06-22 21:18:36 +08:00
|
|
|
full int
|
|
|
|
limit int
|
|
|
|
progress chan int
|
2020-06-21 23:47:02 +08:00
|
|
|
_progress int
|
2020-06-24 12:05:30 +08:00
|
|
|
skip bool
|
2020-06-26 12:54:48 +08:00
|
|
|
text *tview.TextView
|
2020-06-21 23:47:02 +08:00
|
|
|
}
|
|
|
|
|
2020-07-15 21:38:34 +08:00
|
|
|
// Playing bar shows progress of the song and the title of the song
|
|
|
|
func NewPlayingBar() *PlayingBar {
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-07-15 21:38:34 +08:00
|
|
|
textView := tview.NewTextView().SetTextAlign(tview.AlignCenter)
|
2020-06-26 12:54:48 +08:00
|
|
|
|
2020-07-15 21:38:34 +08:00
|
|
|
frame := tview.NewFrame(textView).SetBorders(1, 1, 1, 1, 1, 1)
|
|
|
|
frame.SetBorder(true).SetTitle(" Now Playing ")
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-07-15 21:38:34 +08:00
|
|
|
p := &PlayingBar{frame, 0, 0, make(chan int), 0, false, textView}
|
2020-06-22 21:18:36 +08:00
|
|
|
p.SetDefault()
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-07-15 21:38:34 +08:00
|
|
|
textView.SetChangedFunc(func() {
|
|
|
|
gomu.App.Draw()
|
|
|
|
|
|
|
|
if !gomu.Player.IsRunning {
|
|
|
|
p.SetDefault()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
return p
|
2020-06-19 16:22:20 +08:00
|
|
|
}
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-07-10 22:05:36 +08:00
|
|
|
// start processing progress bar
|
2020-07-06 17:02:59 +08:00
|
|
|
// runs asynchronusly
|
2020-06-26 12:54:48 +08:00
|
|
|
func (p *PlayingBar) Run() {
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-06-23 20:15:29 +08:00
|
|
|
go func() {
|
2020-06-21 23:47:02 +08:00
|
|
|
for {
|
|
|
|
|
2020-07-04 17:12:49 +08:00
|
|
|
// stop progressing if song ends or skipped
|
2020-06-24 12:05:30 +08:00
|
|
|
if p._progress > p.full || p.skip {
|
|
|
|
p.skip = false
|
2020-06-21 23:47:02 +08:00
|
|
|
p._progress = 0
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-06-23 18:42:18 +08:00
|
|
|
p._progress += <-p.progress
|
|
|
|
|
2020-06-26 12:54:48 +08:00
|
|
|
p.text.Clear()
|
2020-06-23 18:42:18 +08:00
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
start, err := time.ParseDuration(strconv.Itoa(p._progress) + "s")
|
|
|
|
|
|
|
|
if err != nil {
|
2020-07-06 17:02:59 +08:00
|
|
|
appLog(err)
|
2020-06-21 23:47:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
end, err := time.ParseDuration(strconv.Itoa(p.full) + "s")
|
|
|
|
|
|
|
|
if err != nil {
|
2020-07-06 17:02:59 +08:00
|
|
|
appLog(err)
|
2020-06-21 23:47:02 +08:00
|
|
|
}
|
2020-06-26 12:54:48 +08:00
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
x := p._progress * p.limit / p.full
|
2020-07-03 13:01:05 +08:00
|
|
|
p.text.SetText(fmt.Sprintf("%s ┃%s%s┫ %s",
|
2020-06-24 11:34:20 +08:00
|
|
|
fmtDuration(start),
|
2020-06-25 20:18:12 +08:00
|
|
|
strings.Repeat("█", x),
|
2020-07-03 13:01:05 +08:00
|
|
|
strings.Repeat("━", p.limit-x),
|
2020-06-24 11:34:20 +08:00
|
|
|
fmtDuration(end),
|
2020-06-21 23:47:02 +08:00
|
|
|
))
|
|
|
|
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-06-26 12:54:48 +08:00
|
|
|
func (p *PlayingBar) SetSongTitle(title string) {
|
|
|
|
p.Clear()
|
|
|
|
p.AddText(title, true, tview.AlignCenter, tcell.ColorGreen)
|
2020-06-21 23:47:02 +08:00
|
|
|
}
|
|
|
|
|
2020-06-26 12:54:48 +08:00
|
|
|
func (p *PlayingBar) NewProgress(songTitle string, full, limit int) {
|
2020-06-21 23:47:02 +08:00
|
|
|
p.full = full
|
|
|
|
p.limit = limit
|
|
|
|
p._progress = 0
|
|
|
|
p.SetSongTitle(songTitle)
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:18:36 +08:00
|
|
|
// sets default title and progress bar
|
2020-06-26 12:54:48 +08:00
|
|
|
func (p *PlayingBar) SetDefault() {
|
2020-06-25 20:18:12 +08:00
|
|
|
p.SetSongTitle("---------:---------")
|
2020-07-03 13:01:05 +08:00
|
|
|
p.text.SetText(fmt.Sprintf("%s ┣%s┫ %s", "00:00", strings.Repeat("━", 100), "00:00"))
|
2020-06-22 21:18:36 +08:00
|
|
|
}
|
2020-06-24 12:05:30 +08:00
|
|
|
|
2020-06-26 12:54:48 +08:00
|
|
|
func (p *PlayingBar) Stop() {
|
2020-06-24 12:05:30 +08:00
|
|
|
p.skip = true
|
|
|
|
}
|