Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spotify segment (linux) #4732

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/segments/spotify_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//go:build linux && !darwin && !windows

package segments

import (
"strings"

"github.com/jandedobbeleer/oh-my-posh/src/shell"
)

func (s *Spotify) Enabled() bool {
var err error
running := s.runLinuxScriptCommand("")

if strings.HasPrefix(running, "ERROR") {
return false
}
if strings.Contains(running, "Error.ServiceUnknown") || strings.HasSuffix(running, "-") {
s.Status = stopped
return false
}
s.Status = playing
if err != nil {
s.Status = stopped
return false
}
if s.Status == stopped {
return false
}

s.Artist = s.runLinuxScriptCommand(" | awk -F '\"' 'BEGIN {RS=\"entry\"}; /'xesam:artist'/ {a=$4} END {print a}'")
s.Track = s.runLinuxScriptCommand(" | awk -F '\"' 'BEGIN {RS=\"entry\"}; /'xesam:title'/ {t=$4} END {print t}'")
s.resolveIcon()
return true
}

func (s *Spotify) runLinuxScriptCommand(command string) string {
val := s.env.RunShellCommand(shell.BASH, "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata"+command)
return val
}
47 changes: 47 additions & 0 deletions src/segments/spotify_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:build linux && !darwin && !windows

package segments

import (
"errors"
"testing"

"github.com/jandedobbeleer/oh-my-posh/src/mock"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/shell"

"github.com/stretchr/testify/assert"
)

func TestSpotifyLinux(t *testing.T) {
cases := []struct {
Running bool
Expected string
Status string
Artist string
Track string
Error error
}{
{Running: false, Expected: "\uE602 -", Error: errors.New("oops")},
{Running: true, Expected: "\uE602 Candlemass - Spellbreaker", Status: "playing", Artist: "Candlemass", Track: "Spellbreaker"},
{Running: true, Expected: "\uE602 Candlemass - Spellbreaker", Status: "paused", Artist: "Candlemass", Track: "Spellbreaker"},
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("IsWsl").Return(false)
env.On("RunShellCommand", shell.BASH, "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata").Return(tc.Status, nil)
env.On("RunShellCommand", shell.BASH, "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata | awk -F '\"' 'BEGIN {RS=\"entry\"}; /'xesam:artist'/ {a=$4} END {print a}'").Return(tc.Artist, nil)
env.On("RunShellCommand", shell.BASH, "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata | awk -F '\"' 'BEGIN {RS=\"entry\"}; /'xesam:title'/ {t=$4} END {print t}'").Return(tc.Track, nil)

s := &Spotify{
env: env,
props: properties.Map{},
}
enable := s.Enabled()
assert.True(t, enable)
if tc.Running {
assert.True(t, s.Enabled())
assert.Equal(t, tc.Expected, renderTemplate(env, s.Template(), s))
}
}
}