gomp/ui/progressBar.go

157 lines
3.5 KiB
Go
Raw Permalink Normal View History

package ui
import (
"fmt"
"strconv"
"strings"
2022-09-08 19:16:38 +00:00
"github.com/fhs/gompd/v2/mpd"
"github.com/gdamore/tcell/v2"
"github.com/aditya-K2/gomp/utils"
"github.com/aditya-K2/tview"
)
var (
CurrentSong string = ""
CONN *mpd.Client
RENDERER interface{ Send(string) }
)
func SetConnection(c *mpd.Client) {
CONN = c
}
func ConnectRenderer(r interface{ Send(string) }) {
RENDERER = r
}
2021-10-24 07:56:10 +00:00
2022-08-29 12:34:34 +00:00
// ProgressBar is a two-lined Box. First line is the BarTitle
// Second being the actual progress done.
2022-10-14 06:30:44 +00:00
// 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
type ProgressBar struct {
*tview.Box
BarTitle string
BarText string
BarTopTitle string
2022-08-29 12:34:34 +00:00
progressFunc func() (BarTitle string,
BarTopTitle string,
BarText string,
percentage float64)
}
2022-10-14 06:30:44 +00:00
func (self *ProgressBar) SetProgressFunc(
pfunc func() (string, string, string, float64),
) (
*ProgressBar,
) {
self.progressFunc = pfunc
return self
}
func NewProgressBar() *ProgressBar {
return &ProgressBar{
Box: tview.NewBox(),
}
}
func GetProgressGlyph(width, percentage float64, btext string) string {
2022-10-14 06:30:44 +00:00
q := "[default:gray: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
}
func (self *ProgressBar) Draw(screen tcell.Screen) {
var (
OFFSET int = 1
)
2022-10-14 06:30:44 +00:00
self.Box.SetBorder(false)
self.Box.SetBorderPadding(1, 1, 1, 1)
self.Box.SetBackgroundColor(tcell.ColorBlack)
var percentage float64
2022-10-14 06:30:44 +00:00
self.BarTitle, _, 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()
2022-10-14 06:30:44 +00:00
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)
}
2022-10-14 06:30:44 +00:00
func ProgressFunction() (
song string,
top string,
text string,
percentage float64,
) {
// song info
currentAttributes, err := CONN.CurrentSong()
if err == nil {
2022-10-14 06:30:44 +00:00
song += "[green::bi]" +
currentAttributes["Title"] + "[-:-:-] - " + "[blue::b]" +
currentAttributes["Artist"] + "\n"
if len(currentAttributes) == 0 && CurrentSong != "" {
2021-10-24 07:56:10 +00:00
CurrentSong = ""
2021-12-22 12:24:14 +00:00
RENDERER.Send("stop")
2022-10-14 06:30:44 +00:00
} else if song != CurrentSong && len(currentAttributes) != 0 {
RENDERER.Send(currentAttributes["file"])
2021-10-24 07:56:10 +00:00
CurrentSong = song
}
} else {
utils.Print("RED", "Error Retrieving Current Song\n")
panic(err)
}
2022-10-14 06:30:44 +00:00
// status
status, err := CONN.Status()
el, err1 := strconv.ParseFloat(status["elapsed"], 8)
du, err := strconv.ParseFloat(status["duration"], 8)
song += fmt.Sprintf(
"[gray::-] | " +
"[[-:]%s[gray::-]] " +
"[[-:]shuf: %s[gray::-]] " +
"[[-:]rep: %s[gray::-]] " +
"[[-:]vol: %s[gray::-]] ",
utils.FormatString(status["state"]),
utils.FormatString(status["random"]),
utils.FormatString(status["repeat"]),
status["volume"])
// bar text
if du != 0 {
percentage = el / du * 100
if err == nil && err1 == nil {
2022-10-14 06:30:44 +00:00
text = utils.StrTime(el) + " / " + utils.StrTime(du) +
" (" + strconv.FormatFloat(
percentage, 'f', 2, 32) + "%" + ")"
} else {
text = ""
}
} else {
text = " ---:---"
percentage = 0
}
2022-10-14 06:30:44 +00:00
// percentage
2022-09-08 20:01:51 +00:00
if percentage > 100 {
percentage = 0
}
2022-10-14 06:30:44 +00:00
song = top + song
return
}