From 3f300b4f1da70e3e56346ce4ed7ca7567ec80280 Mon Sep 17 00:00:00 2001 From: luisegarduno Date: Sun, 3 Mar 2024 02:23:47 -0500 Subject: [PATCH 1/3] - Added spotify segment for linux --- src/segments/spotify_linux.go | 22 ++++++++++++ src/segments/spotify_linux_test.go | 56 ++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/segments/spotify_linux.go create mode 100644 src/segments/spotify_linux_test.go diff --git a/src/segments/spotify_linux.go b/src/segments/spotify_linux.go new file mode 100644 index 000000000000..d0feb7fa3c46 --- /dev/null +++ b/src/segments/spotify_linux.go @@ -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) + + infos := strings.Split(tlist, " - ") + s.Status = playing + s.Artist = infos[0] + s.Track = infos[1] + s.resolveIcon() + + return true +} diff --git a/src/segments/spotify_linux_test.go b/src/segments/spotify_linux_test.go new file mode 100644 index 000000000000..b81aa74475b6 --- /dev/null +++ b/src/segments/spotify_linux_test.go @@ -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)) + } + } +} From 9e43d85312f32272bd4569e3793374c157176ba3 Mon Sep 17 00:00:00 2001 From: luisegarduno Date: Wed, 20 Mar 2024 13:46:39 -0400 Subject: [PATCH 2/3] - Changed: RunShellCommand() --> RunCommand() --- src/segments/spotify_linux.go | 45 +++++++++++++++++++++++------- src/segments/spotify_linux_test.go | 19 ++++++++----- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/segments/spotify_linux.go b/src/segments/spotify_linux.go index d0feb7fa3c46..e6ee10233936 100644 --- a/src/segments/spotify_linux.go +++ b/src/segments/spotify_linux.go @@ -3,20 +3,45 @@ package segments import ( + "encoding/json" "strings" - - "github.com/jandedobbeleer/oh-my-posh/src/shell" ) +type Metadata struct { + TrackID string `json:"mpris:trackid"` + Length uint64 `json:"mpris:length"` + ArtURL string `json:"mpris:artUrl"` + Album string `json:"xesam:album"` + AlbumArtist []string `json:"xesam:albumArtist"` + Artist []string `json:"xesam:artist"` + AutoRating float64 `json:"xesam:autoRating"` + DiscNumber int32 `json:"xesam:discNumber"` + Title string `json:"xesam:title"` + TrackNumber int32 `json:"xesam:trackNumber"` + URL string `json:"xesam:url"` +} + 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) - infos := strings.Split(tlist, " - ") - s.Status = playing - s.Artist = infos[0] - s.Track = infos[1] - s.resolveIcon() + tlist, _ := s.env.RunCommand("dbus-send", "--print-reply", "--dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata") + + // Split the output into lines + lines := strings.Split(tlist, "\n") + + // Extract the JSON-formatted data + var jsonData string + for _, line := range lines { + if strings.Contains(line, "variant") { + jsonData = strings.TrimSpace(line[len(" variant array ["):]) + break + } + } + + var metadata []Metadata + // Parsing JSON data from the output string + if err := json.Unmarshal([]byte(jsonData), &metadata); err != nil { + return true + } - return true + return false } diff --git a/src/segments/spotify_linux_test.go b/src/segments/spotify_linux_test.go index b81aa74475b6..6ddc2be588ea 100644 --- a/src/segments/spotify_linux_test.go +++ b/src/segments/spotify_linux_test.go @@ -23,7 +23,7 @@ func TestSpotifyLinux(t *testing.T) { }{ { Case: "Playing", - ExpectedString: "\ue602 Snow in Stockholm - Sarah, the Illstrumentalist", + ExpectedString: "Sarah, the Illstrumentalist - Snow in Stockholm", ExpectedEnabled: true, Title: "Snow in Stockholm - Sarah, the Illstrumentalist", Artist: "Sarah, the Illstrumentalist", @@ -31,19 +31,24 @@ func TestSpotifyLinux(t *testing.T) { }, { Case: "Stopped", - ExpectedEnabled: false, - Title: "Spotify - Web Player", + ExpectedEnabled: true, + ExpectedString: "-", }, } 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) + env.On("RunCommand", "dbus-send", []string{ + "--print-reply", + "--dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata", + }).Return(tc.Track+" "+tc.Artist, nil) s := &Spotify{ - env: env, - props: properties.Map{ - Command: "echo hi", + env: env, + props: properties.Map{}, + MusicPlayer: MusicPlayer{ + Artist: tc.Artist, + Track: tc.Track, }, } enable := s.Enable() From 694254828279e437a3f508a556686d7d7cd6ec5a Mon Sep 17 00:00:00 2001 From: luisegarduno Date: Sat, 6 Apr 2024 19:05:41 -0400 Subject: [PATCH 3/3] + Temporary: Artist & Track work (if spotify_wsl is removed) --- src/segments/spotify_linux.go | 61 +++++++++++++----------------- src/segments/spotify_linux_test.go | 54 ++++++++++---------------- 2 files changed, 47 insertions(+), 68 deletions(-) diff --git a/src/segments/spotify_linux.go b/src/segments/spotify_linux.go index e6ee10233936..3eee76787a77 100644 --- a/src/segments/spotify_linux.go +++ b/src/segments/spotify_linux.go @@ -3,45 +3,38 @@ package segments import ( - "encoding/json" "strings" -) - -type Metadata struct { - TrackID string `json:"mpris:trackid"` - Length uint64 `json:"mpris:length"` - ArtURL string `json:"mpris:artUrl"` - Album string `json:"xesam:album"` - AlbumArtist []string `json:"xesam:albumArtist"` - Artist []string `json:"xesam:artist"` - AutoRating float64 `json:"xesam:autoRating"` - DiscNumber int32 `json:"xesam:discNumber"` - Title string `json:"xesam:title"` - TrackNumber int32 `json:"xesam:trackNumber"` - URL string `json:"xesam:url"` -} -func (s *Spotify) Enable() bool { - - tlist, _ := s.env.RunCommand("dbus-send", "--print-reply", "--dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata") + "github.com/jandedobbeleer/oh-my-posh/src/shell" +) - // Split the output into lines - lines := strings.Split(tlist, "\n") +func (s *Spotify) Enabled() bool { + var err error + running := s.runLinuxScriptCommand("") - // Extract the JSON-formatted data - var jsonData string - for _, line := range lines { - if strings.Contains(line, "variant") { - jsonData = strings.TrimSpace(line[len(" variant array ["):]) - break - } + if strings.HasPrefix(running, "ERROR") { + return false } - - var metadata []Metadata - // Parsing JSON data from the output string - if err := json.Unmarshal([]byte(jsonData), &metadata); err != nil { - return true + 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 } - 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 } diff --git a/src/segments/spotify_linux_test.go b/src/segments/spotify_linux_test.go index 6ddc2be588ea..f20c652fe95e 100644 --- a/src/segments/spotify_linux_test.go +++ b/src/segments/spotify_linux_test.go @@ -1,61 +1,47 @@ -//go:build !darwin && !windows +//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 { - Case string - ExpectedString string - ExpectedEnabled bool - Title string - Artist string - Track string - Error error + Running bool + Expected string + Status string + Artist string + Track string + Error error }{ - { - Case: "Playing", - ExpectedString: "Sarah, the Illstrumentalist - Snow in Stockholm", - ExpectedEnabled: true, - Title: "Snow in Stockholm - Sarah, the Illstrumentalist", - Artist: "Sarah, the Illstrumentalist", - Track: "Snow in Stockholm", - }, - { - Case: "Stopped", - ExpectedEnabled: true, - ExpectedString: "-", - }, + {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("RunCommand", "dbus-send", []string{ - "--print-reply", - "--dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata", - }).Return(tc.Track+" "+tc.Artist, nil) + 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{}, - MusicPlayer: MusicPlayer{ - Artist: tc.Artist, - Track: tc.Track, - }, } - enable := s.Enable() + enable := s.Enabled() assert.True(t, enable) - if tc.ExpectedEnabled { - assert.True(t, s.Enable()) - assert.Equal(t, tc.ExpectedString, renderTemplate(env, s.Template(), s)) + if tc.Running { + assert.True(t, s.Enabled()) + assert.Equal(t, tc.Expected, renderTemplate(env, s.Template(), s)) } } }