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 1 commit
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
22 changes: 22 additions & 0 deletions src/segments/spotify_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build linux && !darwin && !windows

package segments

import (
"strings"

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

func (s *Spotify) Enable() bool {
command := "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata"
tlist := s.env.RunShellCommand(shell.BASH, command)
luisegarduno marked this conversation as resolved.
Show resolved Hide resolved

infos := strings.Split(tlist, " - ")
s.Status = playing
s.Artist = infos[0]
s.Track = infos[1]
s.resolveIcon()

return true
}
56 changes: 56 additions & 0 deletions src/segments/spotify_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//go:build !darwin && !windows

package segments

import (
"testing"

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

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

func TestSpotifyLinux(t *testing.T) {
cases := []struct {
Case string
ExpectedString string
ExpectedEnabled bool
Title string
Artist string
Track string
Error error
}{
{
Case: "Playing",
ExpectedString: "\ue602 Snow in Stockholm - Sarah, the Illstrumentalist",
ExpectedEnabled: true,
Title: "Snow in Stockholm - Sarah, the Illstrumentalist",
Artist: "Sarah, the Illstrumentalist",
Track: "Snow in Stockholm",
},
{
Case: "Stopped",
ExpectedEnabled: false,
Title: "Spotify - Web Player",
},
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)

env.On("RunShellCommand", "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.Title, tc.Artist)

s := &Spotify{
env: env,
props: properties.Map{
Command: "echo hi",
},
}
enable := s.Enable()
assert.True(t, enable)
if tc.ExpectedEnabled {
assert.True(t, s.Enable())
assert.Equal(t, tc.ExpectedString, renderTemplate(env, s.Template(), s))
}
}
}