diff --git a/README.md b/README.md index 3d878fb..5b405c2 100644 --- a/README.md +++ b/README.md @@ -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 @@ -116,6 +113,7 @@ local config = { icon = wez.nerdfonts.fa_spotify, color = 3, max_width = 64, + throttle = 15, }, }, } diff --git a/plugin/bar/config.lua b/plugin/bar/config.lua index e95ef32..c74fbf4 100644 --- a/plugin/bar/config.lua +++ b/plugin/bar/config.lua @@ -57,6 +57,7 @@ M.options = { icon = wez.nerdfonts.fa_spotify, color = 3, max_width = 64, + throttle = 15, }, }, } diff --git a/plugin/bar/spotify.lua b/plugin/bar/spotify.lua index 31ecd4f..2ec65b4 100644 --- a/plugin/bar/spotify.lua +++ b/plugin/bar/spotify.lua @@ -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 @@ -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 diff --git a/plugin/bar/utilities.lua b/plugin/bar/utilities.lua index 76f768f..d710fdc 100644 --- a/plugin/bar/utilities.lua +++ b/plugin/bar/utilities.lua @@ -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) diff --git a/plugin/init.lua b/plugin/init.lua index c322b36..3648fa9 100644 --- a/plugin/init.lua +++ b/plugin/init.lua @@ -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 })