fix #155 plus some improvements

- trim long artist/song names
- dim widget if spotify is paused
This commit is contained in:
streetturtle 2020-06-12 15:52:05 -04:00
parent c55c60a7d0
commit e785d9cf85
5 changed files with 74 additions and 12 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@ -5,15 +5,22 @@
-- https://github.com/streetturtle/awesome-wm-widgets/tree/master/spotify-widget -- https://github.com/streetturtle/awesome-wm-widgets/tree/master/spotify-widget
-- @author Pavel Makhov -- @author Pavel Makhov
-- @copyright 2018 Pavel Makhov -- @copyright 2020 Pavel Makhov
------------------------------------------------- -------------------------------------------------
local awful = require("awful") local awful = require("awful")
local wibox = require("wibox") local wibox = require("wibox")
local watch = require("awful.widget.watch") local watch = require("awful.widget.watch")
local naughty = require("naughty")
local GET_SPOTIFY_STATUS_CMD = 'sp status' local GET_SPOTIFY_STATUS_CMD = 'sp status'
local GET_CURRENT_SONG_CMD = 'sp current-oneline' local GET_CURRENT_SONG_CMD = 'sp current'
local function ellipsize(text, length)
return (text:len() > length and length > 0)
and text:sub(0, length - 3) .. '...'
or text
end
local spotify_widget = {} local spotify_widget = {}
@ -24,24 +31,53 @@ local function worker(args)
local play_icon = args.play_icon or '/usr/share/icons/Arc/actions/24/player_play.png' local play_icon = args.play_icon or '/usr/share/icons/Arc/actions/24/player_play.png'
local pause_icon = args.pause_icon or '/usr/share/icons/Arc/actions/24/player_pause.png' local pause_icon = args.pause_icon or '/usr/share/icons/Arc/actions/24/player_pause.png'
local font = args.font or 'Play 9' local font = args.font or 'Play 9'
local dim_when_paused = args.dim_when_paused == nil and false or args.dim_when_paused
local dim_opacity = args.dim_opacity or 0.2
local max_length = args.max_length or 15
local show_tooltip = args.show_tooltip == nil and false or args.show_tooltip
local cur_artist = ''
local cur_title = ''
local cur_album = ''
spotify_widget = wibox.widget { spotify_widget = wibox.widget {
{
id = 'artistw',
font = font,
widget = wibox.widget.textbox,
},
{ {
id = "icon", id = "icon",
widget = wibox.widget.imagebox, widget = wibox.widget.imagebox,
}, },
{ {
id = 'current_song', id = 'titlew',
widget = wibox.widget.textbox, font = font,
font = font widget = wibox.widget.textbox
}, },
layout = wibox.layout.align.horizontal, layout = wibox.layout.align.horizontal,
set_status = function(self, is_playing) set_status = function(self, is_playing)
self.icon.image = (is_playing and play_icon or pause_icon) self.icon.image = (is_playing and play_icon or pause_icon)
if dim_when_paused then
self.icon.opacity = (is_playing and 1 or dim_opacity)
self.titlew:set_opacity(is_playing and 1 or dim_opacity)
self.titlew:emit_signal('widget::redraw_needed')
self.artistw:set_opacity(is_playing and 1 or dim_opacity)
self.artistw:emit_signal('widget::redraw_needed')
end
end, end,
set_text = function(self, path) set_text = function(self, artist, song)
self.current_song.markup = path local artist_to_display = ellipsize(artist, max_length)
end, if self.artistw.text ~= artist_to_display then
self.artistw.text = artist_to_display
end
local title_to_display = ellipsize(song, max_length)
if self.titlew.text ~= title_to_display then
self.titlew.text = title_to_display
end
end
} }
local update_widget_icon = function(widget, stdout, _, _, _) local update_widget_icon = function(widget, stdout, _, _, _)
@ -50,12 +86,22 @@ local function worker(args)
end end
local update_widget_text = function(widget, stdout, _, _, _) local update_widget_text = function(widget, stdout, _, _, _)
local escaped = string.gsub(stdout, "&", '&')
if string.find(stdout, 'Error: Spotify is not running.') ~= nil then if string.find(stdout, 'Error: Spotify is not running.') ~= nil then
widget:set_text('') widget:set_text('','')
widget:set_visible(false) widget:set_visible(false)
else return
widget:set_text(escaped) end
local escaped = string.gsub(stdout, "&", '&')
local album, album_artist, artist, title =
string.match(escaped, 'Album%s*(.*)\nAlbumArtist%s*(.*)\nArtist%s*(.*)\nTitle%s*(.*)\n')
if album ~= nil and title ~=nil and artist ~= nil then
cur_artist = artist
cur_title = title
cur_album = album
widget:set_text(artist, title)
widget:set_visible(true) widget:set_visible(true)
end end
end end
@ -80,6 +126,22 @@ local function worker(args)
end) end)
end) end)
if show_tooltip then
local spotify_tooltip = awful.tooltip {
mode = 'outside',
preferred_positions = {'bottom'},
}
spotify_tooltip:add_to_object(spotify_widget)
spotify_widget:connect_signal('mouse::enter', function()
spotify_tooltip.markup = '<b>Album</b>: ' .. cur_album
.. '\n<b>Artist</b>: ' .. cur_artist
.. '\n<b>Song</b>: ' .. cur_title
end)
end
return spotify_widget return spotify_widget
end end