Skip to content

Commit

Permalink
feat(spotify): add configurable throttling of api calls (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriankarlen authored Sep 11, 2024
1 parent 00715fd commit 7078f7f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ A tab bar configuration for wezterm, this configuration is heavily inspired by [

In order for the spotify integration to work you need to have [spotify-tui](https://github.com/Rigellute/spotify-tui) installed on you system. Follow their installation instructions on how to set it up.

> [!IMPORTANT]
> If you are using the spotify module, I recommend setting `status_update_interval` to quite a high number to avoid spotify request limits. I have it set to `10000`.
 

## 🚀 Installation
Expand Down Expand Up @@ -116,6 +113,7 @@ local config = {
icon = wez.nerdfonts.fa_spotify,
color = 3,
max_width = 64,
throttle = 15,
},
},
}
Expand Down
1 change: 1 addition & 0 deletions plugin/bar/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ M.options = {
icon = wez.nerdfonts.fa_spotify,
color = 3,
max_width = 64,
throttle = 15,
},
},
}
Expand Down
14 changes: 12 additions & 2 deletions plugin/bar/spotify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ local utilities = require "bar.utilities"

local M = {}

local last_update = 0
local stored_playback = ""

-- format spotify playback, to handle max_width nicely
local format_playback = function(pb, max_width)
if #pb <= max_width then
Expand All @@ -22,14 +25,21 @@ local format_playback = function(pb, max_width)
end

-- gets the currently playing song from spotify
M.get_currently_playing = function(max_width)
M.get_currently_playing = function(max_width, throttle)
if utilities._wait(throttle, last_update) then
return stored_playback
end
-- fetch playback using spotify-tui
local success, pb, stderr = wez.run_child_process { "spt", "pb", "--format", "%a - %t" }
if not success then
wez.log_error(stderr)
return ""
end
return format_playback(utilities._trim(pb), max_width)
local res = format_playback(utilities._trim(pb), max_width)
stored_playback = res
last_update = os.time()

return res
end

return M
4 changes: 4 additions & 0 deletions plugin/bar/utilities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ local wez = require "wezterm"
H.home = (os.getenv "USERPROFILE" or os.getenv "HOME" or wez.home_dir or ""):gsub("\\", "/")
H.is_windows = package.config:sub(1, 1) == "\\"

H._wait = function (throttle, last_update)
local current_time = os.time()
return current_time - last_update < throttle
end

-- get basename for dir/file, removing ft and path
H._basename = function(s)
Expand Down
2 changes: 1 addition & 1 deletion plugin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ wez.on("update-status", function(window, pane)
}

if options.modules.spotify.enabled then
local playback = spotify.get_currently_playing(options.modules.spotify.max_width)
local playback = spotify.get_currently_playing(options.modules.spotify.max_width, options.modules.spotify.throttle)
if #playback > 0 then
table.insert(right_cells, { Foreground = { Color = palette.ansi[options.modules.spotify.color] } })
table.insert(right_cells, { Text = playback })
Expand Down

0 comments on commit 7078f7f

Please sign in to comment.