2021-10-17 05:40:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-10-17 07:45:52 +00:00
|
|
|
"fmt"
|
2021-10-17 16:59:56 +00:00
|
|
|
"strconv"
|
|
|
|
|
2021-12-16 19:36:18 +00:00
|
|
|
"github.com/aditya-K2/gomp/utils"
|
2021-12-12 20:05:40 +00:00
|
|
|
|
2021-11-15 16:18:17 +00:00
|
|
|
"github.com/aditya-K2/tview"
|
2021-10-17 16:59:56 +00:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2021-10-17 05:40:38 +00:00
|
|
|
)
|
|
|
|
|
2021-10-24 07:56:10 +00:00
|
|
|
var CurrentSong string
|
|
|
|
|
2021-10-17 05:40:38 +00:00
|
|
|
// The progressBar is just a string which is separated by the color formatting String
|
|
|
|
// for e.g
|
|
|
|
// "[:#fbff00:]******************`innerText`[-:-:-] "
|
|
|
|
// the above string shows represents the progress until [-:-:-]
|
|
|
|
// [-:-:-] this string represents resetting colors so the substring before it would be with a
|
|
|
|
// colored background. this is done by calculating the innerRect of the table and taking that length as
|
|
|
|
// 100% and then representing the rest of the information in relation to it
|
2021-10-17 16:59:56 +00:00
|
|
|
type progressBar struct {
|
2021-10-17 05:40:38 +00:00
|
|
|
t *tview.Table
|
|
|
|
}
|
|
|
|
|
|
|
|
// This Function returns a progressBar with a table of two rows
|
|
|
|
// the First row will contain information about the current Song
|
|
|
|
// and the Second one will contain the progressBar
|
2021-12-22 12:24:14 +00:00
|
|
|
func newProgressBar() *progressBar {
|
2021-10-17 05:40:38 +00:00
|
|
|
p := progressBar{}
|
|
|
|
|
|
|
|
a := tview.NewTable().
|
2021-10-17 16:59:56 +00:00
|
|
|
SetCell(0, 0, tview.NewTableCell("")).
|
2021-10-19 06:29:20 +00:00
|
|
|
SetCell(1, 0, tview.NewTableCell("")).
|
|
|
|
SetCell(2, 0, tview.NewTableCell(""))
|
2021-10-17 05:40:38 +00:00
|
|
|
|
2021-12-16 19:36:18 +00:00
|
|
|
a.SetBackgroundColor(tcell.ColorDefault)
|
2021-10-17 05:40:38 +00:00
|
|
|
a.SetBorder(true)
|
|
|
|
|
2021-10-17 16:59:56 +00:00
|
|
|
a.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) {
|
2021-12-22 12:24:14 +00:00
|
|
|
p.updateTitle()
|
2021-11-13 06:02:00 +00:00
|
|
|
p.updateProgress()
|
2021-10-17 05:40:38 +00:00
|
|
|
return p.t.GetInnerRect()
|
|
|
|
})
|
|
|
|
|
2021-10-24 07:56:10 +00:00
|
|
|
CurrentSong = ""
|
2021-10-17 05:40:38 +00:00
|
|
|
|
2021-10-24 07:56:10 +00:00
|
|
|
p = progressBar{a}
|
2021-10-17 05:40:38 +00:00
|
|
|
return &p
|
|
|
|
}
|
|
|
|
|
2021-12-22 12:24:14 +00:00
|
|
|
func (s *progressBar) updateTitle() {
|
2021-11-13 06:02:00 +00:00
|
|
|
_currentAttributes, err := CONN.CurrentSong()
|
2021-10-17 05:40:38 +00:00
|
|
|
if err == nil {
|
2021-10-24 07:56:10 +00:00
|
|
|
song := "[green::bi]" + _currentAttributes["Title"] + "[-:-:-] - " + "[blue::b]" + _currentAttributes["Artist"] + "\n"
|
|
|
|
s.t.GetCell(0, 0).Text = song
|
|
|
|
if len(_currentAttributes) == 0 && CurrentSong != "" {
|
|
|
|
CurrentSong = ""
|
2021-12-22 12:24:14 +00:00
|
|
|
RENDERER.Send("stop")
|
2021-10-24 07:56:10 +00:00
|
|
|
} else if song != CurrentSong && len(_currentAttributes) != 0 {
|
2021-12-22 12:24:14 +00:00
|
|
|
RENDERER.Send(_currentAttributes["file"])
|
2021-10-24 07:56:10 +00:00
|
|
|
CurrentSong = song
|
|
|
|
}
|
2021-10-17 05:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 06:02:00 +00:00
|
|
|
func (s *progressBar) updateProgress() {
|
|
|
|
_status, err := CONN.Status()
|
2021-10-17 05:40:38 +00:00
|
|
|
_, _, _width, _ := s.t.GetInnerRect()
|
|
|
|
el, err1 := strconv.ParseFloat(_status["elapsed"], 8)
|
2021-10-17 16:59:56 +00:00
|
|
|
du, err := strconv.ParseFloat(_status["duration"], 8)
|
2021-11-29 09:49:33 +00:00
|
|
|
if du != 0 {
|
|
|
|
percentage := el / du * 100
|
|
|
|
if err == nil && err1 == nil {
|
2021-12-12 20:05:40 +00:00
|
|
|
s.t.SetTitle(fmt.Sprintf("[[::i] %s [-:-:-]Shuffle: %s Repeat: %s Volume: %s ]", utils.FormatString(_status["state"]), utils.FormatString(_status["random"]), utils.FormatString(_status["repeat"]), _status["volume"])).SetTitleAlign(tview.AlignRight)
|
|
|
|
s.t.GetCell(2, 0).Text = utils.GetText(float64(_width), percentage, utils.StrTime(el)+"/"+utils.StrTime(du)+"("+strconv.FormatFloat(percentage, 'f', 2, 32)+"%"+")")
|
2021-11-29 09:49:33 +00:00
|
|
|
} else {
|
2021-12-12 20:05:40 +00:00
|
|
|
s.t.SetTitle(fmt.Sprintf("[[::i] %s [-:-:-]Shuffle: %s Repeat: %s]", utils.FormatString(_status["state"]), utils.FormatString(_status["random"]), utils.FormatString(_status["repeat"]))).SetTitleAlign(tview.AlignRight)
|
2021-11-29 09:49:33 +00:00
|
|
|
s.t.GetCell(2, 0).Text = ""
|
|
|
|
}
|
2021-10-17 07:45:52 +00:00
|
|
|
} else {
|
2021-12-12 20:05:40 +00:00
|
|
|
s.t.SetTitle(fmt.Sprintf("[[::i] %s [-:-:-]Shuffle: %s Repeat: %s Volume: %s ]", utils.FormatString(_status["state"]), utils.FormatString(_status["random"]), utils.FormatString(_status["repeat"]), _status["volume"])).SetTitleAlign(tview.AlignRight)
|
|
|
|
s.t.GetCell(2, 0).Text = utils.GetText(float64(_width), 0, " ---:---")
|
2021-10-17 05:40:38 +00:00
|
|
|
}
|
|
|
|
}
|