added LoopState datatype for more precise control
This commit is contained in:
		
							
								
								
									
										66
									
								
								loopstate.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								loopstate.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,66 @@
 | 
			
		||||
package mpvipc
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"reflect"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
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))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										18
									
								
								props_get.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								props_get.go
									
									
									
									
									
								
							@ -157,10 +157,20 @@ func GetPropertyList(socket string) ([]string, error) {
 | 
			
		||||
	return GetProperty[[]string](socket, "property-list")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetLoopFile(socket string) (bool, error) {
 | 
			
		||||
	return GetProperty[bool](socket, "loop-file")
 | 
			
		||||
func GetLoopFile(socket string) (LoopState, error) {
 | 
			
		||||
	value, err := GetProperty[any](socket, "loop-file")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return LoopInvalid, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return ParseLoopState(value)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetLoopPlaylist(socket string) (bool, error) {
 | 
			
		||||
	return GetProperty[bool](socket, "loop-playlist")
 | 
			
		||||
func GetLoopPlaylist(socket string) (LoopState, error) {
 | 
			
		||||
	value, err := GetProperty[any](socket, "loop-playlist")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return LoopInvalid, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return ParseLoopState(value)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -26,10 +26,10 @@ func SetPlaylistPosition(socket string, index int) error {
 | 
			
		||||
	return SetProperty(socket, "playlist-pos", index)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func SetLoopFile(socket string, loopFile bool) error {
 | 
			
		||||
	return SetProperty(socket, "loop-file", loopFile)
 | 
			
		||||
func SetLoopFile(socket string, loopFile LoopState) error {
 | 
			
		||||
	return SetProperty(socket, "loop-file", loopFile.String())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func SetLoopPlaylist(socket string, loopPlaylist bool) error {
 | 
			
		||||
	return SetProperty(socket, "loop-playlist", loopPlaylist)
 | 
			
		||||
func SetLoopPlaylist(socket string, loopPlaylist LoopState) error {
 | 
			
		||||
	return SetProperty(socket, "loop-playlist", loopPlaylist.String())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -47,7 +47,7 @@ func SendCommand[T any](socket string, cmd *Command) (*Response[T], error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetProperty[T any](socket string, propertyName string) (T, error) {
 | 
			
		||||
	cmd := &Command{[]interface{}{"get_property", propertyName}}
 | 
			
		||||
	cmd := &Command{[]any{"get_property", propertyName}}
 | 
			
		||||
	resp, err := SendCommand[T](socket, cmd)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return *new(T), err
 | 
			
		||||
@ -61,7 +61,7 @@ func GetProperty[T any](socket string, propertyName string) (T, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func SetProperty[T any](socket string, propertyName string, propertyValue T) error {
 | 
			
		||||
	cmd := &Command{[]interface{}{"set_property", propertyName, propertyValue}}
 | 
			
		||||
	cmd := &Command{[]any{"set_property", propertyName, propertyValue}}
 | 
			
		||||
	resp, err := SendCommand[T](socket, cmd)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user