♻️ Use HoursMinSecondsToS to parse time for fading/trimming
This commit is contained in:
parent
d175360362
commit
4057d00c46
3 changed files with 40 additions and 18 deletions
12
app/fade.go
12
app/fade.go
|
|
@ -2,14 +2,14 @@ package app
|
|||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"codeberg.org/danjones000/strip-beats/media"
|
||||
"codeberg.org/danjones000/strip-beats/utils"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
func validateNumber(input string, lastChar rune) bool {
|
||||
num, err := strconv.ParseFloat(input, 64)
|
||||
num, err := utils.HourMinSecToSeconds(input)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
|
@ -42,16 +42,16 @@ func fadeFile() error {
|
|||
cont := tview.NewApplication()
|
||||
form := tview.NewForm().
|
||||
AddInputField("Beginning time?", "0", 0, validateNumber, func(input string) {
|
||||
start, _ = strconv.ParseFloat(input, 64)
|
||||
start, _ = utils.HourMinSecToSeconds(input)
|
||||
}).
|
||||
AddInputField("Ending time? (leave 0 for full time)", "0", 0, validateNumber, func(input string) {
|
||||
stop, _ = strconv.ParseFloat(input, 64)
|
||||
stop, _ = utils.HourMinSecToSeconds(input)
|
||||
}).
|
||||
AddInputField("Fade in duration?", "0", 0, validateNumber, func(input string) {
|
||||
up, _ = strconv.ParseFloat(input, 64)
|
||||
up, _ = utils.HourMinSecToSeconds(input)
|
||||
}).
|
||||
AddInputField("Fade out duration?", "0", 0, validateNumber, func(input string) {
|
||||
down, _ = strconv.ParseFloat(input, 64)
|
||||
down, _ = utils.HourMinSecToSeconds(input)
|
||||
}).
|
||||
AddButton("Start", func() {
|
||||
cont.Stop()
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
p "path"
|
||||
"strings"
|
||||
|
|
@ -14,8 +15,11 @@ func GetShortPath(path string) string {
|
|||
return fmt.Sprintf("%s/%s", dir, base)
|
||||
}
|
||||
|
||||
func HourMinSecToSeconds(time string) float64 {
|
||||
func HourMinSecToSeconds(time string) (float64, error) {
|
||||
parts := strings.Split(time, ":")
|
||||
if len(parts) > 3 {
|
||||
return 0, errors.New(fmt.Sprintf("Can't parse %s. Must be in HH:MM:SS format", time))
|
||||
}
|
||||
units := []string{"h", "m", "s"}
|
||||
units = units[len(units)-len(parts):]
|
||||
f := ""
|
||||
|
|
@ -23,5 +27,5 @@ func HourMinSecToSeconds(time string) float64 {
|
|||
f = f + part + units[idx]
|
||||
}
|
||||
dur, _ := t.ParseDuration(f)
|
||||
return dur.Seconds()
|
||||
return dur.Seconds(), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,30 +1,48 @@
|
|||
package utils
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func hmsTest(t *testing.T, time string, expected float64) {
|
||||
found := HourMinSecToSeconds(time)
|
||||
if found != expected {
|
||||
t.Fatalf(`HourMinSecToSeconds("%s") = %v, want %v`, time, found, expected)
|
||||
func hmsTest(t *testing.T, time string, expected float64, expecterr error) {
|
||||
found, err := HourMinSecToSeconds(time)
|
||||
var expecterrmsg, errmsg string
|
||||
if err != nil {
|
||||
errmsg = err.Error()
|
||||
}
|
||||
if expecterr != nil {
|
||||
expecterrmsg = expecterr.Error()
|
||||
}
|
||||
|
||||
if found != expected || errmsg != expecterrmsg {
|
||||
t.Fatalf(`HourMinSecToSeconds("%s") = %v, %v, want %v, %v`, time, found, err, expected, expecterr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTwoManyParts(t *testing.T) {
|
||||
val := "2:3:4:5:6"
|
||||
err := fmt.Sprintf("Can't parse %s. Must be in HH:MM:SS format", val)
|
||||
hmsTest(t, val, 0, errors.New(err))
|
||||
}
|
||||
|
||||
func TestJustSeconds(t *testing.T) {
|
||||
hmsTest(t, "4.23", 4.23)
|
||||
hmsTest(t, "4.23", 4.23, nil)
|
||||
}
|
||||
|
||||
func TestMinuteSeconds(t *testing.T) {
|
||||
hmsTest(t, "2:3.45", 123.45)
|
||||
hmsTest(t, "2:3.45", 123.45, nil)
|
||||
}
|
||||
|
||||
func TestHourMinuteSeconds(t *testing.T) {
|
||||
hmsTest(t, "3:5:12.43", 11112.43)
|
||||
hmsTest(t, "3:5:12.43", 11112.43, nil)
|
||||
}
|
||||
|
||||
func TestLeadingZeros(t *testing.T) {
|
||||
hmsTest(t, "03:05:02.43", 11102.43)
|
||||
hmsTest(t, "03:05:02.43", 11102.43, nil)
|
||||
}
|
||||
|
||||
func TestWholeSecond(t *testing.T) {
|
||||
hmsTest(t, "4:2", 242)
|
||||
hmsTest(t, "4:2", 242, nil)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue