diff --git a/src/segments/spotify_windows.go b/src/segments/spotify_windows.go index 443bcaad..bdf7f458 100644 --- a/src/segments/spotify_windows.go +++ b/src/segments/spotify_windows.go @@ -11,30 +11,43 @@ func (s *Spotify) Enabled() bool { // Can be either "Spotify xxx" or the song name "Candlemass - Spellbreaker" windowTitle, err := s.env.QueryWindowTitles("spotify.exe", `^(Spotify.*)|(.*\s-\s.*)$`) if err == nil { - return s.parseSpotifyTitle(windowTitle, " - ") + return s.parseNativeTitle(windowTitle) } windowTitle, err = s.env.QueryWindowTitles("msedge.exe", `^(Spotify.*)`) if err != nil { return false } - return s.parseWebSpotifyTitle(windowTitle) + return s.parseWebTitle(windowTitle) } -func (s *Spotify) parseWebSpotifyTitle(windowTitle string) bool { - windowTitle = strings.TrimPrefix(windowTitle, "Spotify - ") - return s.parseSpotifyTitle(windowTitle, " • ") -} +func (s *Spotify) parseNativeTitle(windowTitle string) bool { + separator := " - " -func (s *Spotify) parseSpotifyTitle(windowTitle, separator string) bool { if !strings.Contains(windowTitle, separator) { s.Status = stopped return false } - infos := strings.Split(windowTitle, separator) - s.Artist = infos[0] - // remove first element and concat others(a song can contains also a " - ") - s.Track = strings.Join(infos[1:], separator) + index := strings.Index(windowTitle, separator) + s.Artist = windowTitle[0:index] + s.Track = windowTitle[index+len(separator):] + s.Status = playing + s.resolveIcon() + return true +} + +func (s *Spotify) parseWebTitle(windowTitle string) bool { + windowTitle = strings.TrimPrefix(windowTitle, "Spotify - ") + separator := " • " + + if !strings.Contains(windowTitle, separator) { + s.Status = stopped + return false + } + + index := strings.Index(windowTitle, separator) + s.Track = windowTitle[0:index] + s.Artist = windowTitle[index+len(separator):] s.Status = playing s.resolveIcon() return true diff --git a/src/segments/spotify_windows_test.go b/src/segments/spotify_windows_test.go index f2f9d2ba..51d75c6a 100644 --- a/src/segments/spotify_windows_test.go +++ b/src/segments/spotify_windows_test.go @@ -63,10 +63,16 @@ func TestSpotifyWindowsPWA(t *testing.T) { }{ { Case: "Playing", - ExpectedString: "\ue602 Snow in Stockholm - Sarah, the Illstrumentalist", + ExpectedString: "\ue602 Sarah, the Illstrumentalist - Snow in Stockholm", ExpectedEnabled: true, Title: "Spotify - Snow in Stockholm • Sarah, the Illstrumentalist", }, + { + Case: "Playing", + ExpectedString: "\ue602 Main one - Bring the drama", + ExpectedEnabled: true, + Title: "Spotify - Bring the drama • Main one", + }, { Case: "Stopped", ExpectedEnabled: false,