gomu/playingbar.go

166 lines
3.3 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 (
2021-02-21 15:18:19 +08:00
// "bytes"
2020-06-21 23:47:02 +08:00
"fmt"
2021-02-21 15:18:19 +08:00
"log"
2020-06-21 23:47:02 +08:00
"strconv"
"strings"
"time"
2020-06-19 16:22:20 +08:00
2021-02-21 15:18:19 +08:00
// "github.com/asticode/go-astisub"
"github.com/bogem/id3v2"
"github.com/martinlindhe/subtitles"
2020-06-21 23:47:02 +08:00
"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
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
2021-02-21 15:18:19 +08:00
hasTag bool
tag *id3v2.Tag
2020-06-21 23:47:02 +08:00
}
2021-02-21 15:18:19 +08:00
type lyricParsed struct {
timestart time.Duration
timeend time.Duration
lyricText string
}
var lyricsParsed []lyricParsed
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)
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-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
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
}
p._progress += <-p.progress
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-08-22 14:51:16 +08:00
_, _, width, _ := p.GetInnerRect()
progressBar := progresStr(p._progress, p.full, width/2, "█", "━")
// our progress bar
2021-02-21 15:18:19 +08:00
if p.hasTag {
p.text.SetText(fmt.Sprintf("%s ┃%s┫ %s\n%s",
fmtDuration(start),
progressBar,
fmtDuration(end),
p.tag.Title(),
))
} else {
p.text.SetText(fmt.Sprintf("%s ┃%s┫ %s",
fmtDuration(start),
progressBar,
fmtDuration(end),
))
}
2021-02-19 15:07:44 +08:00
gomu.app.Draw()
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()
2020-08-22 14:41:03 +08:00
titleColor := gomu.colors.title
p.AddText(title, true, tview.AlignCenter, titleColor)
2020-06-21 23:47:02 +08:00
}
// Resets progress bar, ready for execution
2020-08-22 14:51:16 +08:00
func (p *PlayingBar) newProgress(songTitle string, full int) {
2020-06-21 23:47:02 +08:00
p.full = full
p._progress = 0
2020-07-23 15:15:39 +08:00
p.setSongTitle(songTitle)
2021-02-21 15:18:19 +08:00
var tag *id3v2.Tag
var err error
tag, err = id3v2.Open(gomu.player.currentSong.path, id3v2.Options{Parse: true})
if tag == nil || err != nil {
logError(err)
} else {
p.hasTag = true
p.tag = tag
usltFrames := tag.GetFrames(tag.CommonID("Unsynchronised lyrics/text transcription"))
for _, f := range usltFrames {
uslf, ok := f.(id3v2.UnsynchronisedLyricsFrame)
if !ok {
log.Fatal("USLT error!")
}
/* subtitleLyric, err := astisub.ReadFromWebVTT(bytes.NewBufferString(uslf.Lyrics))
if err != nil {
logError(err)
}
_ = subtitleLyric */
res, err := subtitles.NewFromSRT(uslf.Lyrics)
if err != nil {
logError(err)
}
fmt.Println(res.Captions[3])
}
}
defer tag.Close()
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-08-22 14:51:16 +08:00
_, _, width, _ := p.GetInnerRect()
2020-07-20 21:48:13 +08:00
text := fmt.Sprintf(
2020-08-22 14:51:16 +08:00
"%s ┣%s┫ %s", "00:00", strings.Repeat("━", width/2), "00:00",
2020-07-20 21:48:13 +08: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
}