Skip to content

Commit

Permalink
feat(spotify): add segment cache on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepSpace2 authored and JanDeDobbeleer committed Jul 25, 2024
1 parent e392890 commit 79c859d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 19 deletions.
43 changes: 43 additions & 0 deletions src/segments/spotify_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -23,12 +36,42 @@ 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
}

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)
}
41 changes: 22 additions & 19 deletions website/docs/segments/music/spotify.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,31 @@ fetching information from the native Spotify application and Edge PWA.

## Sample Configuration

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 "
}
}}/>
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 ",
},
}}
/>

## 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])

Expand Down

0 comments on commit 79c859d

Please sign in to comment.