format duration string

This commit is contained in:
raziman 2020-06-24 11:34:20 +08:00
parent e17c1b45f0
commit 0d00c4fc73
3 changed files with 36 additions and 10 deletions

View File

@ -59,7 +59,7 @@ func (p *Progress) Run() {
go func() {
for {
if p._progress > p.full {
if p._progress > p.full || p.player.isSkipped {
p._progress = 0
break
@ -83,10 +83,10 @@ func (p *Progress) Run() {
x := p._progress * p.limit / p.full
p.textView.SetText(fmt.Sprintf("%s %s%s %s",
start.String(),
fmtDuration(start),
strings.Repeat("■", x),
strings.Repeat("□", p.limit-x),
end.String(),
fmtDuration(end),
))
}
@ -108,5 +108,5 @@ func (p *Progress) NewProgress(songTitle string, full, limit int) {
// sets default title and progress bar
func (p *Progress) SetDefault() {
p.SetSongTitle("-")
p.textView.SetText(fmt.Sprintf("%s %s %s", "0s", strings.Repeat("□", 100), "0s"))
p.textView.SetText(fmt.Sprintf("%s %s %s", "00:00", strings.Repeat("□", 100), "00:00"))
}

View File

@ -7,7 +7,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
@ -89,9 +88,7 @@ func Playlist(list *tview.List, playBar *Progress, player *Player) *tview.TreeVi
go func () {
player.Run()
list.AddItem(
fmt.Sprintf("%s | %s", player.length.String(), audioFile.Name),
"", 0, nil)
list.AddItem("", "", 0, nil)
} ()
} else {
@ -102,7 +99,7 @@ func Playlist(list *tview.List, playBar *Progress, player *Player) *tview.TreeVi
log(err.Error())
}
list.AddItem(
fmt.Sprintf("[ %s ] %s", songLength.Round(time.Second).String(), audioFile.Name),
fmt.Sprintf("[ %s ] %s", fmtDuration(songLength), audioFile.Name),
"", 0, nil)
}
}

View File

@ -2,7 +2,11 @@
package main
import "os"
import (
"os"
"strings"
"time"
)
func log(text string) {
@ -21,3 +25,28 @@ func log(text string) {
}
}
func fmtDuration(input time.Duration) string {
val := input.Round(time.Second).String()
if !strings.Contains(val, "m") {
val = "0m" + val
}
val = strings.ReplaceAll(val, "h", ":")
val = strings.ReplaceAll(val, "m", ":")
val = strings.ReplaceAll(val, "s", "")
var result []string
for _, v := range strings.Split(val, ":") {
if len(v) < 2 {
result = append(result, "0" + v)
} else {
result = append(result, v)
}
}
return strings.Join(result, ":")
}