From 79c859d4348060fd58748903c1124f26980f44c5 Mon Sep 17 00:00:00 2001 From: Adi Vaknin <6841988+DeepSpace2@users.noreply.github.com> Date: Thu, 25 Jul 2024 17:01:50 +0300 Subject: [PATCH] feat(spotify): add segment cache on macOS --- src/segments/spotify_darwin.go | 43 +++++++++++++++++++++++++ website/docs/segments/music/spotify.mdx | 39 +++++++++++----------- 2 files changed, 64 insertions(+), 18 deletions(-) diff --git a/src/segments/spotify_darwin.go b/src/segments/spotify_darwin.go index d468ceef..35976fbc 100644 --- a/src/segments/spotify_darwin.go +++ b/src/segments/spotify_darwin.go @@ -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) +} diff --git a/website/docs/segments/music/spotify.mdx b/website/docs/segments/music/spotify.mdx index 102e2aa0..7344f0f5 100644 --- a/website/docs/segments/music/spotify.mdx +++ b/website/docs/segments/music/spotify.mdx @@ -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"; - + ## 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])