diff --git a/ui/browsing/nowplayingpage.go b/ui/browsing/nowplayingpage.go index a0f81e8..1f12ec5 100644 --- a/ui/browsing/nowplayingpage.go +++ b/ui/browsing/nowplayingpage.go @@ -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) diff --git a/ui/util/util.go b/ui/util/util.go index 2fd5613..d237ac5 100644 --- a/ui/util/util.go +++ b/ui/util/util.go @@ -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 } @@ -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 diff --git a/ui/util/util_test.go b/ui/util/util_test.go new file mode 100644 index 0000000..f8ba1c2 --- /dev/null +++ b/ui/util/util_test.go @@ -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]) + } + } +} diff --git a/ui/widgets/playercontrols.go b/ui/widgets/playercontrols.go index b3dd72e..83076b3 100644 --- a/ui/widgets/playercontrols.go +++ b/ui/widgets/playercontrols.go @@ -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)) } } @@ -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 @@ -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 diff --git a/ui/widgets/playqueuelist.go b/ui/widgets/playqueuelist.go index ed9f60c..57b6cbd 100644 --- a/ui/widgets/playqueuelist.go +++ b/ui/widgets/playqueuelist.go @@ -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 } diff --git a/ui/widgets/tracklistrow.go b/ui/widgets/tracklistrow.go index 1794919..b941894 100644 --- a/ui/widgets/tracklistrow.go +++ b/ui/widgets/tracklistrow.go @@ -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