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
import (
"encoding/json"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
)
const spotifyCacheKey = "spotify_music_player"
func (s *Spotify) Enabled() bool {
cacheTimeout := s.props.GetInt(properties.CacheTimeout, 0)
if cacheTimeout > 0 && s.getFromCache() {
return true
}
// Check if running
running := s.runAppleScriptCommand("application \"Spotify\" is 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.Track = s.runAppleScriptCommand("tell application \"Spotify\" to name of current track as string")
s.resolveIcon()
if cacheTimeout > 0 {
s.setCache(cacheTimeout)
}
return true
}
@ -32,3 +50,28 @@ func (s *Spotify) runAppleScriptCommand(command string) string {
val, _ := s.env.RunCommand("osascript", "-e", command)
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
import Config from '@site/src/components/Config.js';
import Config from "@site/src/components/Config.js";
<Config data={{
"type": "spotify",
"style": "powerline",
"powerline_symbol": "\uE0B0",
"foreground": "#ffffff",
"background": "#1BD760",
"properties": {
"playing_icon": "\uE602 ",
"paused_icon": "\uF8E3 ",
"stopped_icon": "\uF04D "
}
}}/>
<Config
data={{
type: "spotify",
style: "powerline",
powerline_symbol: "\uE0B0",
foreground: "#ffffff",
background: "#1BD760",
properties: {
playing_icon: "\uE602 ",
paused_icon: "\uF8E3 ",
stopped_icon: "\uF04D ",
},
}}
/>
## Properties
| Name | Type | Default | Description |
| -------------- | :------: | :-------: | ------------------------------ |
| `playing_icon` | `string` | `\uE602 ` | text/icon to show when playing |
| `paused_icon` | `string` | `\uF8E3 ` | text/icon to show when paused |
| `stopped_icon` | `string` | `\uF04D` | text/icon to show when stopped |
| Name | Type | Default | Description |
| --------------- | :------: | :-------: | ---------------------------------------------------------------------------- |
| `playing_icon` | `string` | `\uE602 ` | text/icon to show when playing |
| `paused_icon` | `string` | `\uF8E3 ` | text/icon to show when paused |
| `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])