|
| 1 | +//go:build linux && !darwin && !windows |
| 2 | + |
| 3 | +package segments |
| 4 | + |
| 5 | +import ( |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/jandedobbeleer/oh-my-posh/src/shell" |
| 9 | +) |
| 10 | + |
| 11 | +func (s *Spotify) Enabled() bool { |
| 12 | + // Check if we're in WSL and handle that separately |
| 13 | + if s.env.IsWsl() { |
| 14 | + return s.enabledWsl() |
| 15 | + } |
| 16 | + |
| 17 | + // Standard Linux implementation |
| 18 | + running := s.runLinuxScriptCommand(" string:PlaybackStatus | awk -F '\"' '/string/ {print tolower($2)}'") |
| 19 | + |
| 20 | + if strings.HasPrefix(running, "Error") { |
| 21 | + return false |
| 22 | + } |
| 23 | + |
| 24 | + if strings.Contains(running, "Error.ServiceUnknown") || strings.HasSuffix(running, "-") { |
| 25 | + s.Status = stopped |
| 26 | + return false |
| 27 | + } |
| 28 | + |
| 29 | + if running == stopped { |
| 30 | + s.Status = stopped |
| 31 | + return false |
| 32 | + } |
| 33 | + |
| 34 | + s.Status = running |
| 35 | + s.Artist = s.runLinuxScriptCommand(" string:Metadata | awk -F '\"' 'BEGIN {RS=\"entry\"}; /'xesam:artist'/ {a=$4} END {print a}'") |
| 36 | + s.Track = s.runLinuxScriptCommand(" string:Metadata | awk -F '\"' 'BEGIN {RS=\"entry\"}; /'xesam:title'/ {t=$4} END {print t}'") |
| 37 | + s.resolveIcon() |
| 38 | + |
| 39 | + return true |
| 40 | +} |
| 41 | + |
| 42 | +func (s *Spotify) runLinuxScriptCommand(command string) string { |
| 43 | + dbusCMD := "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player" |
| 44 | + val := s.env.RunShellCommand(shell.BASH, dbusCMD+command) |
| 45 | + return val |
| 46 | +} |
| 47 | + |
| 48 | +// Implementation for WSL - moved from spotify_wsl.go |
| 49 | +func (s *Spotify) enabledWsl() bool { |
| 50 | + tlist, err := s.env.RunCommand("tasklist.exe", "/V", "/FI", "Imagename eq Spotify.exe", "/FO", "CSV", "/NH") |
| 51 | + if err != nil || strings.HasPrefix(tlist, "INFO") { |
| 52 | + return false |
| 53 | + } |
| 54 | + |
| 55 | + records := strings.Split(tlist, "\n") |
| 56 | + if len(records) == 0 { |
| 57 | + return false |
| 58 | + } |
| 59 | + |
| 60 | + for _, record := range records { |
| 61 | + record = strings.TrimSpace(record) |
| 62 | + fields := strings.Split(record, ",") |
| 63 | + if len(fields) == 0 { |
| 64 | + continue |
| 65 | + } |
| 66 | + |
| 67 | + // last elemant is the title |
| 68 | + title := fields[len(fields)-1] |
| 69 | + // trim leading and trailing quotes from the field |
| 70 | + title = strings.TrimPrefix(title, `"`) |
| 71 | + title = strings.TrimSuffix(title, `"`) |
| 72 | + if !strings.Contains(title, " - ") { |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + infos := strings.Split(title, " - ") |
| 77 | + s.Artist = infos[0] |
| 78 | + s.Track = strings.Join(infos[1:], " - ") |
| 79 | + s.Status = playing |
| 80 | + s.resolveIcon() |
| 81 | + |
| 82 | + return true |
| 83 | + } |
| 84 | + |
| 85 | + return false |
| 86 | +} |
0 commit comments