From ce65aeaab44fc38109eb2d6128e9552f12f643b9 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Mon, 29 Nov 2021 15:19:33 +0530 Subject: [PATCH] Fixes #12 This fixes The Issue when a song with unknown duration was played then the percentage was calculated wrong and the slice was getting out of bound. fix : I have added a check for the duration. --- progressBar.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/progressBar.go b/progressBar.go index 1dd4fb3..69f5575 100644 --- a/progressBar.go +++ b/progressBar.go @@ -66,12 +66,17 @@ func (s *progressBar) updateProgress() { _, _, _width, _ := s.t.GetInnerRect() el, err1 := strconv.ParseFloat(_status["elapsed"], 8) du, err := strconv.ParseFloat(_status["duration"], 8) - percentage := el / du * 100 - if err == nil && err1 == nil { - s.t.SetTitle(fmt.Sprintf("[[::i] %s [-:-:-]Shuffle: %s Repeat: %s Volume: %s ]", formatString(_status["state"]), formatString(_status["random"]), formatString(_status["repeat"]), _status["volume"])).SetTitleAlign(tview.AlignRight) - s.t.GetCell(2, 0).Text = getText(float64(_width), percentage, strTime(el)+"/"+strTime(du)+"("+strconv.FormatFloat(percentage, 'f', 2, 32)+"%"+")") + if du != 0 { + percentage := el / du * 100 + if err == nil && err1 == nil { + s.t.SetTitle(fmt.Sprintf("[[::i] %s [-:-:-]Shuffle: %s Repeat: %s Volume: %s ]", formatString(_status["state"]), formatString(_status["random"]), formatString(_status["repeat"]), _status["volume"])).SetTitleAlign(tview.AlignRight) + s.t.GetCell(2, 0).Text = getText(float64(_width), percentage, strTime(el)+"/"+strTime(du)+"("+strconv.FormatFloat(percentage, 'f', 2, 32)+"%"+")") + } else { + s.t.SetTitle(fmt.Sprintf("[[::i] %s [-:-:-]Shuffle: %s Repeat: %s]", formatString(_status["state"]), formatString(_status["random"]), formatString(_status["repeat"]))).SetTitleAlign(tview.AlignRight) + s.t.GetCell(2, 0).Text = "" + } } else { - s.t.SetTitle(fmt.Sprintf("[[::i] %s [-:-:-]Shuffle: %s Repeat: %s]", formatString(_status["state"]), formatString(_status["random"]), formatString(_status["repeat"]))).SetTitleAlign(tview.AlignRight) - s.t.GetCell(2, 0).Text = "" + s.t.SetTitle(fmt.Sprintf("[[::i] %s [-:-:-]Shuffle: %s Repeat: %s Volume: %s ]", formatString(_status["state"]), formatString(_status["random"]), formatString(_status["repeat"]), _status["volume"])).SetTitleAlign(tview.AlignRight) + s.t.GetCell(2, 0).Text = getText(float64(_width), 0, " ---:---") } }