feat(spotify): add segment cache on macOS

This commit is contained in:
Adi Vaknin 2024-07-25 17:01:50 +03:00 committed by Jan De Dobbeleer
parent e3928901fc
commit 79c859d434
2 changed files with 64 additions and 18 deletions

View file

@ -2,7 +2,20 @@
package segments package segments
import (
"encoding/json"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
)
const spotifyCacheKey = "spotify_music_player"
func (s *Spotify) Enabled() bool { func (s *Spotify) Enabled() bool {
cacheTimeout := s.props.GetInt(properties.CacheTimeout, 0)
if cacheTimeout > 0 && s.getFromCache() {
return true
}
// Check if running // Check if running
running := s.runAppleScriptCommand("application \"Spotify\" is running") running := s.runAppleScriptCommand("application \"Spotify\" is running")
if running == "false" || running == "" { if running == "false" || running == "" {
@ -23,8 +36,13 @@ func (s *Spotify) Enabled() bool {
s.Artist = s.runAppleScriptCommand("tell application \"Spotify\" to artist of current track as string") s.Artist = s.runAppleScriptCommand("tell application \"Spotify\" to artist of current track as string")
s.Track = s.runAppleScriptCommand("tell application \"Spotify\" to name of current track as string") s.Track = s.runAppleScriptCommand("tell application \"Spotify\" to name of current track as string")
s.resolveIcon() s.resolveIcon()
if cacheTimeout > 0 {
s.setCache(cacheTimeout)
}
return true return true
} }
@ -32,3 +50,28 @@ func (s *Spotify) runAppleScriptCommand(command string) string {
val, _ := s.env.RunCommand("osascript", "-e", command) val, _ := s.env.RunCommand("osascript", "-e", command)
return val return val
} }
func (s *Spotify) getFromCache() bool {
str, found := s.env.Cache().Get(spotifyCacheKey)
if !found {
return false
}
var cachedMusicPlayer MusicPlayer
err := json.Unmarshal([]byte(str), &cachedMusicPlayer)
if err != nil {
return false
}
s.MusicPlayer = cachedMusicPlayer
return true
}
func (s *Spotify) setCache(cacheTimeout int) {
cache, err := json.Marshal(s.MusicPlayer)
if err != nil {
return
}
s.env.Cache().Set(spotifyCacheKey, string(cache), cacheTimeout)
}

View file

@ -19,28 +19,31 @@ fetching information from the native Spotify application and Edge PWA.
## Sample Configuration ## Sample Configuration
import Config from '@site/src/components/Config.js'; import Config from "@site/src/components/Config.js";
<Config data={{ <Config
"type": "spotify", data={{
"style": "powerline", type: "spotify",
"powerline_symbol": "\uE0B0", style: "powerline",
"foreground": "#ffffff", powerline_symbol: "\uE0B0",
"background": "#1BD760", foreground: "#ffffff",
"properties": { background: "#1BD760",
"playing_icon": "\uE602 ", properties: {
"paused_icon": "\uF8E3 ", playing_icon: "\uE602 ",
"stopped_icon": "\uF04D " paused_icon: "\uF8E3 ",
} stopped_icon: "\uF04D ",
}}/> },
}}
/>
## Properties ## Properties
| Name | Type | Default | Description | | Name | Type | Default | Description |
| -------------- | :------: | :-------: | ------------------------------ | | --------------- | :------: | :-------: | ---------------------------------------------------------------------------- |
| `playing_icon` | `string` | `\uE602 ` | text/icon to show when playing | | `playing_icon` | `string` | `\uE602 ` | text/icon to show when playing |
| `paused_icon` | `string` | `\uF8E3 ` | text/icon to show when paused | | `paused_icon` | `string` | `\uF8E3 ` | text/icon to show when paused |
| `stopped_icon` | `string` | `\uF04D` | text/icon to show when stopped | | `stopped_icon` | `string` | `\uF04D` | text/icon to show when stopped |
| `cache_timeout` | `int` | `0` | **macOS only** in minutes - How long to wait before fetching new information |
## Template ([info][templates]) ## Template ([info][templates])