fix(spotify): parse correctly when in WSL

This commit is contained in:
Jan De Dobbeleer 2024-03-13 15:20:41 +01:00 committed by Jan De Dobbeleer
parent 33148ef8f9
commit d05737a8e8
2 changed files with 36 additions and 11 deletions

View file

@ -3,7 +3,6 @@
package segments
import (
"encoding/csv"
"strings"
)
@ -11,24 +10,39 @@ 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
}

View file

@ -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",