Skip to content

Commit

Permalink
feat(spotify): add spotify playback module (#10)
Browse files Browse the repository at this point in the history
* feat(spotify): adds currently playing song from spotify

* fix(spotify): only load playback if playing

* chore: update preview

* docs: add spotify instructions
  • Loading branch information
adriankarlen authored Sep 10, 2024
1 parent 732384d commit 12b1ce7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 🍺 bar.wezterm

A tab bar configuration for wezterm, this configuration is heavily inspired by [rose-pine/tmux](https://github.com/rose-pine/tmux)
A tab bar configuration for wezterm, this configuration is heavily inspired by [rose-pine/tmux](https://github.com/rose-pine/tmux).

## 📷

Expand All @@ -15,6 +15,14 @@ A tab bar configuration for wezterm, this configuration is heavily inspired by [
![image](https://raw.githubusercontent.com/adriankarlen/bar.wezterm/main/misc/catppuccin-mocha.png)
 

## 📋 Prerequisites

### 🎵Spotify

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.

 

## 🚀 Installation

This is a wezterm [plugin](https://github.com/wez/wezterm/commit/e4ae8a844d8feaa43e1de34c5cc8b4f07ce525dd). It can be installed by importing the repo and calling the apply_to_config-function. It is important that the `apply_to_config`-function is called after `color_scheme` has been set.
Expand All @@ -33,11 +41,12 @@ bar.apply_to_config(config)
The `apply_to_config`-function takes a second param `opts`. To override any options simply pass a table of the desired changes.

```lua
-- example disable spotify module
bar.apply_to_config(
config,
{
modules = {
username = {
spotify = {
enabled = false,
},
},
Expand Down Expand Up @@ -99,6 +108,12 @@ local config = {
icon = wez.nerdfonts.oct_file_directory,
color = 7,
},
spotify = {
enabled = true,
icon = wez.nerdfonts.fa_spotify,
color = 3,
max_width = 64,
},
},
}
```
Expand Down
Binary file modified misc/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions plugin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ local config = {
icon = wez.nerdfonts.oct_file_directory,
color = 7,
},
spotify = {
enabled = true,
icon = wez.nerdfonts.fa_spotify,
color = 3,
max_width = 64,
},
},
}

Expand Down Expand Up @@ -191,6 +197,35 @@ local get_leader = function(prev)
return _space(leader, first_half, second_half)
end

-- format spotify playback, to handle max_width nicely
local format_playback = function(pb)
if #pb <= config.modules.spotify.max_width then
return pb
end

-- split on " - "
local artist, track = pb:match "^(.-) %- (.+)$"
-- get artist before first ","
local pb_main_artist = artist:match "([^,]+)" .. " - " .. track
if #pb_main_artist <= config.modules.spotify.max_width then
return pb_main_artist
end

-- fallback, return track name (trimmed to max width)
return track:sub(1, config.modules.spotify.max_width)
end

-- gets the currently playing song from spotify
local get_currently_playing = function()
-- 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(_trim(pb))
end

-- conforming to https://github.com/wez/wezterm/commit/e4ae8a844d8feaa43e1de34c5cc8b4f07ce525dd
M.apply_to_config = function(c, opts)
-- make the opts arg optional
Expand Down Expand Up @@ -298,6 +333,20 @@ wez.on("update-status", function(window, pane)
{ Background = { Color = palette.tab_bar.background } },
}

if config.modules.spotify.enabled then
local playback = get_currently_playing()
if #playback > 0 then
table.insert(right_cells, { Foreground = { Color = palette.ansi[config.modules.spotify.color] } })
table.insert(right_cells, { Text = get_currently_playing() })
table.insert(right_cells, { Foreground = { Color = palette.brights[1] } })
table.insert(right_cells, {
Text = _space(config.separator.right_icon, config.separator.space, nil)
.. config.modules.spotify.icon
.. _space(config.separator.field_icon, config.separator.space, nil),
})
end
end

if config.modules.username.enabled then
table.insert(right_cells, { Foreground = { Color = palette.ansi[config.modules.username.color] } })
table.insert(right_cells, { Text = username })
Expand Down

0 comments on commit 12b1ce7

Please sign in to comment.