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-23 15:15:39 +08:00
func (p *PlayingBar) help() []string {
2020-07-17 15:34:50 +08:00
return []string{}
}
2020-07-15 21:38:34 +08:00
// Playing bar shows progress of the song and the title of the song
2020-07-23 15:15:39 +08:00
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),
}
2020-07-23 15:15:39 +08:00
p.setDefault()
2020-06-21 23:47:02 +08:00
2020-07-15 21:38:34 +08:00
textView.SetChangedFunc(func() {
2020-07-23 15:15:39 +08:00
gomu.app.Draw()
2020-07-15 21:38:34 +08:00
2020-07-23 15:15:39 +08:00
if !gomu.player.isRunning {
p.setDefault()
2020-07-15 21:38:34 +08:00
}
})
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-23 15:15:39 +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
}
2020-07-23 15:15:39 +08:00
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-24 17:08:34 +08:00
progressBar := progresStr(p._progress, p.full, p.limit, "█", "━")
// our progress bar
2020-07-24 17:08:34 +08:00
p.text.SetText(fmt.Sprintf("%s ┃%s┫ %s",
2020-07-21 12:22:00 +08:00
fmtDuration(start),
2020-07-24 17:08:34 +08:00
progressBar,
2020-07-21 12:22:00 +08:00
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-07-23 15:15:39 +08:00
func (p *PlayingBar) setSongTitle(title string) {
2020-06-26 12:54:48 +08:00
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-07-23 15:15:39 +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
2020-07-23 15:15:39 +08:00
p.setSongTitle(songTitle)
2020-06-21 23:47:02 +08:00
}
// Sets default title and progress bar
2020-07-23 15:15:39 +08:00
func (p *PlayingBar) setDefault() {
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-07-23 15:34:56 +08:00
// Skips the current playing song
2020-07-23 15:15:39 +08:00
func (p *PlayingBar) stop() {
2020-06-24 12:05:30 +08:00
p.skip = true
}