2021-12-22 08:09:01 -07:00
|
|
|
package ui
|
2021-10-16 23:40:38 -06:00
|
|
|
|
|
|
|
import (
|
2021-10-17 01:45:52 -06:00
|
|
|
"fmt"
|
2021-10-17 10:59:56 -06:00
|
|
|
"strconv"
|
2022-08-29 06:06:36 -06:00
|
|
|
"strings"
|
2021-10-17 10:59:56 -06:00
|
|
|
|
2022-09-08 13:16:38 -06:00
|
|
|
"github.com/fhs/gompd/v2/mpd"
|
2022-08-29 06:06:36 -06:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2021-12-23 07:50:27 -07:00
|
|
|
|
2021-12-16 12:36:18 -07:00
|
|
|
"github.com/aditya-K2/gomp/utils"
|
2021-11-15 09:18:17 -07:00
|
|
|
"github.com/aditya-K2/tview"
|
2021-10-16 23:40:38 -06:00
|
|
|
)
|
|
|
|
|
2021-12-22 08:09:01 -07:00
|
|
|
var (
|
2022-08-29 06:06:36 -06:00
|
|
|
CurrentSong string = ""
|
2021-12-22 08:09:01 -07:00
|
|
|
CONN *mpd.Client
|
|
|
|
RENDERER interface{ Send(string) }
|
|
|
|
)
|
|
|
|
|
|
|
|
func SetConnection(c *mpd.Client) {
|
|
|
|
CONN = c
|
|
|
|
}
|
|
|
|
|
2021-12-23 07:50:27 -07:00
|
|
|
func ConnectRenderer(r interface{ Send(string) }) {
|
2021-12-22 08:09:01 -07:00
|
|
|
RENDERER = r
|
|
|
|
}
|
2021-10-24 01:56:10 -06:00
|
|
|
|
2022-08-29 06:34:34 -06:00
|
|
|
// ProgressBar is a two-lined Box. First line is the BarTitle
|
|
|
|
// Second being the actual progress done.
|
|
|
|
// Use SetProgressFunc to provide the callback which provides the Fields each time the ProgressBar will be Drawn.
|
|
|
|
// The progressFunc must return (BarTitle, BarTopTitle, BarText, percentage) respectively
|
2022-08-29 06:06:36 -06:00
|
|
|
type ProgressBar struct {
|
|
|
|
*tview.Box
|
|
|
|
BarTitle string
|
|
|
|
BarText string
|
|
|
|
BarTopTitle string
|
2022-08-29 06:34:34 -06:00
|
|
|
progressFunc func() (BarTitle string,
|
|
|
|
BarTopTitle string,
|
|
|
|
BarText string,
|
|
|
|
percentage float64)
|
2021-10-16 23:40:38 -06:00
|
|
|
}
|
|
|
|
|
2022-08-29 06:06:36 -06:00
|
|
|
func (self *ProgressBar) SetProgressFunc(pfunc func() (string, string, string, float64)) *ProgressBar {
|
|
|
|
self.progressFunc = pfunc
|
|
|
|
return self
|
|
|
|
}
|
2021-10-16 23:40:38 -06:00
|
|
|
|
2022-08-29 06:06:36 -06:00
|
|
|
func NewProgressBar() *ProgressBar {
|
|
|
|
return &ProgressBar{
|
|
|
|
Box: tview.NewBox(),
|
|
|
|
}
|
|
|
|
}
|
2021-10-16 23:40:38 -06:00
|
|
|
|
2022-08-29 06:06:36 -06:00
|
|
|
func GetProgressGlyph(width, percentage float64, btext string) string {
|
|
|
|
q := "[black:white:b]"
|
|
|
|
var a string
|
|
|
|
a += strings.Repeat(" ", int(width)-len(btext))
|
|
|
|
a = utils.InsertAt(a, btext, int(width/2)-10)
|
|
|
|
a = utils.InsertAt(a, "[-:-:-]", int(width*percentage/100))
|
|
|
|
q += a
|
|
|
|
return q
|
|
|
|
}
|
2021-10-16 23:40:38 -06:00
|
|
|
|
2022-08-29 06:06:36 -06:00
|
|
|
func (self *ProgressBar) Draw(screen tcell.Screen) {
|
|
|
|
var (
|
|
|
|
OFFSET int = 1
|
|
|
|
)
|
|
|
|
self.Box.SetBorder(true)
|
|
|
|
self.Box.SetBackgroundColor(tcell.ColorDefault)
|
|
|
|
var percentage float64
|
|
|
|
self.BarTitle, self.BarTopTitle, self.BarText, percentage = self.progressFunc()
|
|
|
|
self.DrawForSubclass(screen, self.Box)
|
|
|
|
self.Box.SetTitle(self.BarTopTitle)
|
|
|
|
self.Box.SetTitleAlign(tview.AlignRight)
|
|
|
|
x, y, _width, _ := self.Box.GetInnerRect()
|
|
|
|
tview.Print(screen, self.BarTitle, x+OFFSET, y, _width, tview.AlignLeft, tcell.ColorWhite)
|
|
|
|
tview.Print(screen,
|
|
|
|
GetProgressGlyph(float64(_width-OFFSET-1),
|
|
|
|
percentage,
|
|
|
|
self.BarText),
|
|
|
|
x, y+2, _width-OFFSET, tview.AlignRight, tcell.ColorWhite)
|
2021-10-16 23:40:38 -06:00
|
|
|
}
|
|
|
|
|
2022-10-03 04:07:26 -06:00
|
|
|
func ProgressFunction() (string, string, string, float64) {
|
2021-11-12 23:02:00 -07:00
|
|
|
_currentAttributes, err := CONN.CurrentSong()
|
2022-08-29 06:06:36 -06:00
|
|
|
var song, top, text string
|
|
|
|
var percentage float64
|
2021-10-16 23:40:38 -06:00
|
|
|
if err == nil {
|
2022-08-29 06:06:36 -06:00
|
|
|
song = "[green::bi]" +
|
|
|
|
_currentAttributes["Title"] + "[-:-:-] - " + "[blue::b]" +
|
|
|
|
_currentAttributes["Artist"] + "\n"
|
2021-10-24 01:56:10 -06:00
|
|
|
if len(_currentAttributes) == 0 && CurrentSong != "" {
|
|
|
|
CurrentSong = ""
|
2021-12-22 05:24:14 -07:00
|
|
|
RENDERER.Send("stop")
|
2021-10-24 01:56:10 -06:00
|
|
|
} else if song != CurrentSong && len(_currentAttributes) != 0 {
|
2021-12-22 05:24:14 -07:00
|
|
|
RENDERER.Send(_currentAttributes["file"])
|
2021-10-24 01:56:10 -06:00
|
|
|
CurrentSong = song
|
|
|
|
}
|
2022-08-29 06:06:36 -06:00
|
|
|
} else {
|
|
|
|
utils.Print("RED", "Error Retrieving Current Song\n")
|
|
|
|
panic(err)
|
2021-10-16 23:40:38 -06:00
|
|
|
}
|
2021-11-12 23:02:00 -07:00
|
|
|
_status, err := CONN.Status()
|
2021-10-16 23:40:38 -06:00
|
|
|
el, err1 := strconv.ParseFloat(_status["elapsed"], 8)
|
2021-10-17 10:59:56 -06:00
|
|
|
du, err := strconv.ParseFloat(_status["duration"], 8)
|
2022-08-29 06:06:36 -06:00
|
|
|
top = fmt.Sprintf("[[::i] %s [-:-:-]Shuffle: %s Repeat: %s Volume: %s ]",
|
|
|
|
utils.FormatString(_status["state"]),
|
|
|
|
utils.FormatString(_status["random"]),
|
|
|
|
utils.FormatString(_status["repeat"]),
|
|
|
|
_status["volume"])
|
2021-11-29 02:49:33 -07:00
|
|
|
if du != 0 {
|
2022-08-29 06:06:36 -06:00
|
|
|
percentage = el / du * 100
|
2021-11-29 02:49:33 -07:00
|
|
|
if err == nil && err1 == nil {
|
2022-08-29 06:06:36 -06:00
|
|
|
text = utils.StrTime(el) + "/" + utils.StrTime(du) +
|
|
|
|
"(" + strconv.FormatFloat(percentage, 'f', 2, 32) + "%" + ")"
|
2021-11-29 02:49:33 -07:00
|
|
|
} else {
|
2022-08-29 06:06:36 -06:00
|
|
|
text = ""
|
2021-11-29 02:49:33 -07:00
|
|
|
}
|
2021-10-17 01:45:52 -06:00
|
|
|
} else {
|
2022-08-29 06:06:36 -06:00
|
|
|
text = " ---:---"
|
|
|
|
percentage = 0
|
2021-10-16 23:40:38 -06:00
|
|
|
}
|
2022-09-08 14:01:51 -06:00
|
|
|
if percentage > 100 {
|
|
|
|
percentage = 0
|
|
|
|
}
|
2022-08-29 06:06:36 -06:00
|
|
|
return song, top, text, percentage
|
2021-10-16 23:40:38 -06:00
|
|
|
}
|