Files
mpvipc/loopstate.go

75 lines
1.5 KiB
Go

package mpvipc
import (
"fmt"
"reflect"
"strconv"
"strings"
)
// LoopState is an integer representing the amount of loops.
// Specially handled values are LoopInfinite, LoopForced and LoopInvalid.
// There is also the alias value LoopNo which is equal to 1
type LoopState int
const (
LoopInfinite LoopState = -2
LoopForced LoopState = -1
LoopInvalid LoopState = 0
LoopNo LoopState = 1
)
func Loop(amount int) LoopState {
if amount < int(LoopInfinite) {
amount = int(LoopInfinite)
} else if amount == 0 {
amount = int(LoopNo)
}
return LoopState(amount)
}
func ParseLoopState(value any) (LoopState, error) {
switch v := value.(type) {
case string:
switch strings.ToLower(v) {
case "no":
return LoopNo, nil
case "inf":
return LoopInfinite, nil
case "force":
return LoopForced, nil
}
case int:
return Loop(v), nil
case float64:
return Loop(int(v)), nil
case bool:
if v {
return LoopInfinite, nil
} else {
return LoopNo, nil
}
}
return LoopInvalid, fmt.Errorf("could not parse value as valid loop state: %#v (type: %#v)", value, reflect.TypeOf(value).Name())
}
func (s LoopState) String() string {
switch s {
case LoopInfinite:
return "inf"
case LoopNo:
return "no"
case LoopForced:
return "force"
default:
return strconv.Itoa(int(s))
}
}
// RepeatAgain returns true if the song will be repeated again after the end of the song is reached
func (s LoopState) RepeatAgain() bool {
return s == LoopInfinite || s == LoopForced || s > LoopNo
}