Skip to content

Commit

Permalink
Better stringification of play times longer than 1 hr
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Jun 14, 2024
1 parent 768c385 commit 169b914
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 10 deletions.
4 changes: 2 additions & 2 deletions ui/browsing/nowplayingpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ func (a *NowPlayingPage) formatStatusLine() {
if state != "Stopped" {
trackNum = a.pm.NowPlayingIndex() + 1
statusSuffix = fmt.Sprintf(" %s/%s",
util.SecondsToTimeString(playerStats.TimePos),
util.SecondsToTimeString(dur))
util.SecondsToMMSS(playerStats.TimePos),
util.SecondsToMMSS(dur))
}
status := fmt.Sprintf("%s (%d/%d)%s", state, trackNum,
len(a.queue), statusSuffix)
Expand Down
38 changes: 37 additions & 1 deletion ui/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func MakeOpaque(c color.Color) color.Color {
return c
}

func SecondsToTimeString(s float64) string {
func SecondsToMMSS(s float64) string {
if s < 0 {
s = 0
}
Expand All @@ -43,6 +43,42 @@ func SecondsToTimeString(s float64) string {
return fmt.Sprintf("%d:%02d", min, sec)
}

func SecondsToTimeString(s float64) string {
if s < 3600 /*1 hour*/ {
return SecondsToMMSS(s)
}
sec := int64(s)
days := sec / 86400
sec -= days * 86400
hr := sec / 3600
sec -= hr * 3600
min := sec / 60
sec -= min * 60

var str string
if days > 0 {
daysStr := "days"
if days == 1 {
daysStr = "day"
}
str = fmt.Sprintf("%d %s ", days, daysStr)
}
if hr > 0 {
hrStr := "hrs"
if hr == 1 {
hrStr = "hr"
}
str += fmt.Sprintf("%d %s ", hr, hrStr)
}
if min > 0 {
str += fmt.Sprintf("%d min ", min)
}
if sec > 0 {
str += fmt.Sprintf("%d sec ", sec)
}
return str[:len(str)-1]
}

func BytesToSizeString(bytes int64) string {
var num float64
var suffix string
Expand Down
25 changes: 25 additions & 0 deletions ui/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package util

import "testing"

func TestSecondsToTimeString(t *testing.T) {
inputs := []float64{
57.2,
3360,
4800,
4812,
86401,
}
outputs := []string{
"0:57",
"56:00",
"1 hr 20 min",
"1 hr 20 min 12 sec",
"1 day 1 sec",
}
for i, input := range inputs {
if s := SecondsToTimeString(input); s != outputs[i] {
t.Errorf("got %s, want %s", s, outputs[i])
}
}
}
10 changes: 5 additions & 5 deletions ui/widgets/playercontrols.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ func NewPlayerControls() *PlayerControls {

pc.slider = NewTrackPosSlider()
pc.slider.Disable()
pc.curTimeLabel = NewLabelMinSize(util.SecondsToTimeString(0), 55)
pc.curTimeLabel = NewLabelMinSize(util.SecondsToMMSS(0), 55)
pc.curTimeLabel.Alignment = fyne.TextAlignTrailing
pc.totalTimeLabel = NewLabelMinSize(util.SecondsToTimeString(0), 55)
pc.totalTimeLabel = NewLabelMinSize(util.SecondsToMMSS(0), 55)
pc.totalTimeLabel.Alignment = fyne.TextAlignTrailing

pc.slider.OnChanged = func(f float64) {
if pc.slider.IsDragging() {
time := f * pc.totalTime
pc.curTimeLabel.SetText(util.SecondsToTimeString(time))
pc.curTimeLabel.SetText(util.SecondsToMMSS(time))
}
}

Expand Down Expand Up @@ -178,7 +178,7 @@ func (pc *PlayerControls) UpdatePlayTime(curTime, totalTime float64) {
}

updated := false
tt := util.SecondsToTimeString(totalTime)
tt := util.SecondsToMMSS(totalTime)
if tt != pc.totalTimeLabel.Text {
pc.totalTimeLabel.SetText(tt)
updated = true
Expand All @@ -189,7 +189,7 @@ func (pc *PlayerControls) UpdatePlayTime(curTime, totalTime float64) {
pc.slider.Disable()
}
if !pc.slider.IsDragging() {
ct := util.SecondsToTimeString(curTime)
ct := util.SecondsToMMSS(curTime)
if ct != pc.curTimeLabel.Text {
pc.curTimeLabel.SetText(ct)
updated = true
Expand Down
2 changes: 1 addition & 1 deletion ui/widgets/playqueuelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ func (p *PlayQueueListRow) Update(tm *util.TrackListModel, rowNum int) {
p.trackID = meta.ID
p.title.Text = meta.Name
p.artist.BuildSegments(meta.Artists, meta.ArtistIDs)
p.time.Text = util.SecondsToTimeString(float64(meta.Duration))
p.time.Text = util.SecondsToMMSS(float64(meta.Duration))
changed = true
}

Expand Down
2 changes: 1 addition & 1 deletion ui/widgets/tracklistrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (t *tracklistRowBase) Update(tm *util.TrackListModel, rowNum int) {
t.name.Segments[0].(*widget.TextSegment).Text = tr.Title
t.artist.BuildSegments(tr.ArtistNames, tr.ArtistIDs)
t.album.BuildSegments([]string{tr.Album}, []string{tr.AlbumID})
t.dur.Text = util.SecondsToTimeString(float64(tr.Duration))
t.dur.Text = util.SecondsToMMSS(float64(tr.Duration))
t.year.Text = strconv.Itoa(tr.Year)
t.plays.Text = strconv.Itoa(int(tr.PlayCount))
t.comment.Text = tr.Comment
Expand Down

0 comments on commit 169b914

Please sign in to comment.