Skip to content

Commit

Permalink
fix(spotify): parse correctly when in WSL
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Mar 14, 2024
1 parent 33148ef commit d05737a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/segments/spotify_wsl.go
Expand Up @@ -3,32 +3,46 @@
package segments

import (
"encoding/csv"
"strings"
)

func (s *Spotify) Enabled() bool {
if !s.env.IsWsl() {
return false
}

tlist, err := s.env.RunCommand("tasklist.exe", "/V", "/FI", "Imagename eq Spotify.exe", "/FO", "CSV", "/NH")
if err != nil || strings.HasPrefix(tlist, "INFO") {
return false
}
records, err := csv.NewReader(strings.NewReader(tlist)).ReadAll()
if err != nil || len(records) == 0 {

records := strings.Split(tlist, "\n")
if len(records) == 0 {
return false
}

for _, record := range records {
title := record[len(record)-1]
if strings.Contains(title, " - ") {
infos := strings.Split(title, " - ")
s.Artist = infos[0]
s.Track = strings.Join(infos[1:], " - ")
s.Status = playing
s.resolveIcon()
return true
fields := strings.Split(record, ",")
if len(fields) == 0 {
continue
}

// last elemant is the title
title := fields[len(fields)-1]
// trim leading and trailing quotes from the field
title = strings.TrimPrefix(title, `"`)
title = strings.TrimSuffix(title, `"`)
if !strings.Contains(title, " - ") {
continue
}

infos := strings.Split(title, " - ")
s.Artist = infos[0]
s.Track = strings.Join(infos[1:], " - ")
s.Status = playing
s.resolveIcon()
return true
}

return false
}
11 changes: 11 additions & 0 deletions src/segments/spotify_wsl_test.go
Expand Up @@ -46,6 +46,17 @@ func TestSpotifyWsl(t *testing.T) {
"Spotify.exe","22052","Console","1","29,040 K","Unknown","PC\user","0:00:00","N/A"
"Spotify.exe","22072","Console","1","43,960 K","Unknown","PC\user","0:01:50","N/A"
"Spotify.exe","10404","Console","1","256,924 K","Unknown","PC\user","0:10:49","N/A"`,
},
{
Case: "Spotify playing",
ExpectedString: "\ue602 Grabbitz - Another Form Of \"Goodbye\"",
ExpectedEnabled: true,
ExecOutput: `"Spotify.exe","13748","Console","1","303.744 K","Running","GARMIN\elderbroekowe","0:03:58","Grabbitz - Another Form Of "Goodbye""
"Spotify.exe","4208","Console","1","31.544 K","Running","GARMIN\elderbroekowe","0:00:00","N/A"
"Spotify.exe","14528","Console","1","184.020 K","Running","GARMIN\elderbroekowe","0:02:54","N/A"
"Spotify.exe","14488","Console","1","53.828 K","Unknown","GARMIN\elderbroekowe","0:00:08","N/A"
"Spotify.exe","14800","Console","1","29.576 K","Unknown","GARMIN\elderbroekowe","0:00:00","N/A"
"Spotify.exe","19836","Console","1","237.360 K","Unknown","GARMIN\elderbroekowe","0:07:46","N/A"`,
},
{
Case: "tasklist.exe not in path",
Expand Down

0 comments on commit d05737a

Please sign in to comment.