gomu/playingbar.go

145 lines
2.7 KiB
Go
Raw Normal View History

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-07-21 12:22:00 +08:00
"github.com/ztrue/tracerr"
2020-06-21 23:47:02 +08:00
)
2020-06-26 12:54:48 +08:00
type PlayingBar struct {
*tview.Frame
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-17 15:34:50 +08:00
func (p *PlayingBar) Help() []string {
return []string{}
}
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-17 15:34:50 +08:00
p := &PlayingBar{
Frame: frame,
text: textView,
progress: make(chan int),
}
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
// Start processing progress bar
2020-07-21 12:22:00 +08:00
func (p *PlayingBar) Run() error {
2020-06-21 23:47:02 +08:00
// When app is suspending, we want the progress bar to stop progressing
// because it causes screen to hang-up when app starts to stop suspending
// accumulate when app is suspending
acc := 0
wasSuspended := false
2020-07-21 12:22:00 +08:00
for {
2020-06-21 23:47:02 +08:00
2020-07-21 12:22:00 +08:00
// stop progressing if song ends or skipped
if p._progress > p.full || p.skip {
p.skip = false
p._progress = 0
break
}
if gomu.IsSuspend {
// channel the progress to acc
acc += <-p.progress
wasSuspended = true
continue
} else {
// normal progressing
p._progress += <-p.progress
}
2020-06-21 23:47:02 +08:00
if wasSuspended {
// add back so that we dont lose track in progress bar
p._progress += acc
wasSuspended = false
acc = 0
}
2020-06-23 18:42:18 +08:00
p.text.Clear()
2020-07-21 12:22:00 +08:00
start, err := time.ParseDuration(strconv.Itoa(p._progress) + "s")
2020-06-23 18:42:18 +08:00
2020-07-21 12:22:00 +08:00
if err != nil {
return tracerr.Wrap(err)
}
2020-06-21 23:47:02 +08:00
2020-07-21 12:22:00 +08:00
end, err := time.ParseDuration(strconv.Itoa(p.full) + "s")
2020-06-21 23:47:02 +08:00
2020-07-21 12:22:00 +08:00
if err != nil {
return tracerr.Wrap(err)
}
2020-06-21 23:47:02 +08:00
2020-07-21 12:22:00 +08:00
x := p._progress * p.limit / p.full
// our progress bar
2020-07-21 12:22:00 +08:00
p.text.SetText(fmt.Sprintf("%s ┃%s%s┫ %s",
fmtDuration(start),
strings.Repeat("█", x),
strings.Repeat("━", p.limit-x),
fmtDuration(end),
))
2020-06-26 12:54:48 +08:00
2020-07-21 12:22:00 +08:00
}
2020-06-21 23:47:02 +08:00
2020-07-21 12:22:00 +08:00
return nil
2020-06-21 23:47:02 +08:00
}
// Updates song title
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
}
// Resets progress bar, ready for execution
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)
}
// 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-20 21:48:13 +08:00
text := fmt.Sprintf(
"%s ┣%s┫ %s", "00:00", strings.Repeat("━", 100), "00:00",
)
p.text.SetText(text)
}
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
}