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-22 21:18:36 +08:00
|
|
|
func PlayingBar(app *tview.Application, player *Player) *Progress {
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-06-22 21:18:36 +08:00
|
|
|
textView := tview.NewTextView()
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-06-23 18:42:18 +08:00
|
|
|
progress := InitProgressBar(textView, player)
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-06-22 21:18:36 +08:00
|
|
|
textView.SetChangedFunc(func() {
|
|
|
|
app.Draw()
|
|
|
|
|
|
|
|
if !player.IsRunning {
|
|
|
|
progress.SetDefault()
|
|
|
|
}
|
|
|
|
})
|
2020-06-21 23:47:02 +08:00
|
|
|
|
|
|
|
return progress
|
|
|
|
}
|
|
|
|
|
|
|
|
type Progress struct {
|
2020-06-22 21:18:36 +08:00
|
|
|
textView *tview.TextView
|
|
|
|
full int
|
|
|
|
limit int
|
|
|
|
progress chan int
|
|
|
|
frame *tview.Frame
|
2020-06-21 23:47:02 +08:00
|
|
|
_progress int
|
2020-06-23 18:42:18 +08:00
|
|
|
player *Player
|
2020-06-24 12:05:30 +08:00
|
|
|
skip bool
|
2020-06-21 23:47:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// full is the maximum amount of value can be sent to channel
|
|
|
|
// limit is the progress bar size
|
2020-06-23 18:42:18 +08:00
|
|
|
func InitProgressBar(txt *tview.TextView, player *Player) *Progress {
|
|
|
|
p := &Progress{textView: txt, player: player}
|
2020-06-21 23:47:02 +08:00
|
|
|
p.progress = make(chan int)
|
|
|
|
p.textView.SetTextAlign(tview.AlignCenter)
|
|
|
|
|
|
|
|
p.frame = tview.NewFrame(p.textView).SetBorders(1, 1, 1, 1, 1, 1)
|
2020-06-23 18:42:18 +08:00
|
|
|
p.frame.SetBorder(true).SetTitle(" Now Playing ")
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-06-22 21:18:36 +08:00
|
|
|
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
|
|
|
|
|
|
|
func (p *Progress) Run() {
|
|
|
|
|
2020-06-23 20:15:29 +08:00
|
|
|
go func() {
|
2020-06-21 23:47:02 +08:00
|
|
|
for {
|
|
|
|
|
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
|
|
|
|
|
|
|
|
p.textView.Clear()
|
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
start, err := time.ParseDuration(strconv.Itoa(p._progress) + "s")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
end, err := time.ParseDuration(strconv.Itoa(p.full) + "s")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-25 20:18:12 +08:00
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
x := p._progress * p.limit / p.full
|
2020-06-25 20:18:12 +08:00
|
|
|
p.textView.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),
|
|
|
|
strings.Repeat("-", p.limit-x),
|
2020-06-24 11:34:20 +08:00
|
|
|
fmtDuration(end),
|
2020-06-21 23:47:02 +08:00
|
|
|
))
|
|
|
|
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Progress) SetSongTitle(title string) {
|
|
|
|
p.frame.Clear()
|
|
|
|
p.frame.AddText(title, true, tview.AlignCenter, tcell.ColorGreen)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Progress) NewProgress(songTitle string, full, limit int) {
|
|
|
|
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
|
|
|
|
func (p *Progress) SetDefault() {
|
2020-06-25 20:18:12 +08:00
|
|
|
p.SetSongTitle("---------:---------")
|
|
|
|
p.textView.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
|
|
|
|
|
|
|
func (p *Progress) Stop() {
|
|
|
|
p.skip = true
|
|
|
|
}
|