Skip to content

Commit

Permalink
make getDecode more useful
Browse files Browse the repository at this point in the history
  • Loading branch information
sentriz committed Nov 24, 2023
1 parent 3d73a9f commit 5bc29f4
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions jukebox/jukebox.go
Expand Up @@ -77,7 +77,7 @@ func (j *Jukebox) Start(sockPath string, mpvExtraArgs []string) error {
}

var mpvVersionStr string
if err := j.getDecode(&mpvVersionStr, "mpv-version"); err != nil {
if err := getDecode(j.conn, &mpvVersionStr, "mpv-version"); err != nil {
return fmt.Errorf("get mpv version: %w", err)
}
if major, minor, patch := parseMPVVersion(mpvVersionStr); major == 0 && minor < 34 {
Expand All @@ -104,7 +104,7 @@ func (j *Jukebox) GetPlaylist() ([]string, error) {
defer lockr(&j.mu)()

var playlist mpvPlaylist
if err := j.getDecode(&playlist, "playlist"); err != nil {
if err := getDecode(j.conn, &playlist, "playlist"); err != nil {
return nil, fmt.Errorf("get playlist: %w", err)
}
var items []string
Expand All @@ -118,7 +118,7 @@ func (j *Jukebox) SetPlaylist(items []string) error {
defer lock(&j.mu)()

var playlist mpvPlaylist
if err := j.getDecode(&playlist, "playlist"); err != nil {
if err := getDecode(j.conn, &playlist, "playlist"); err != nil {
return fmt.Errorf("get playlist: %w", err)
}
current, currentIndex := find(playlist, func(item mpvPlaylistItem) bool {
Expand Down Expand Up @@ -260,7 +260,7 @@ func (j *Jukebox) GetVolumePct() (float64, error) {
defer lockr(&j.mu)()

var volume float64
if err := j.getDecode(&volume, "volume"); err != nil {
if err := getDecode(j.conn, &volume, "volume"); err != nil {
return 0, fmt.Errorf("get volume: %w", err)
}
return volume, nil
Expand All @@ -279,11 +279,11 @@ func (j *Jukebox) GetStatus() (*Status, error) {
defer lockr(&j.mu)()

var status Status
_ = j.getDecode(&status.Position, "time-pos") // property may not always be there
_ = j.getDecode(&status.GainPct, "volume") // property may not always be there
_ = getDecode(j.conn, &status.Position, "time-pos") // property may not always be there
_ = getDecode(j.conn, &status.GainPct, "volume") // property may not always be there

var playlist mpvPlaylist
_ = j.getDecode(&playlist, "playlist")
_ = getDecode(j.conn, &playlist, "playlist")

status.CurrentIndex = slices.IndexFunc(playlist, func(pl mpvPlaylistItem) bool {
return pl.Current
Expand All @@ -298,7 +298,7 @@ func (j *Jukebox) GetStatus() (*Status, error) {
status.CurrentFilename = playlist[status.CurrentIndex].Filename

var paused bool
_ = j.getDecode(&paused, "pause") // property may not always be there
_ = getDecode(j.conn, &paused, "pause") // property may not always be there
status.Playing = !paused

return &status, nil
Expand All @@ -324,8 +324,8 @@ func (j *Jukebox) Quit() error {
return nil
}

func (j *Jukebox) getDecode(dest any, property string) error {
raw, err := j.conn.Get(property)
func getDecode(conn *mpvipc.Connection, dest any, property string) error {
raw, err := conn.Get(property)
if err != nil {
return fmt.Errorf("get property: %w", err)
}
Expand Down

0 comments on commit 5bc29f4

Please sign in to comment.