55-- requires - playerctl
66-- @copyright 2020
77---- ---------------------------------------------
8- local awful = require (" awful" )
9- local beautiful = require (" beautiful" )
10- local watch = require (" awful.widget.watch" )
11- local wibox = require (" wibox" )
12- local gears = require (" gears" )
8+ local awful = require (' awful' )
9+ local beautiful = require (' beautiful' )
10+ local watch = require (' awful.widget.watch' )
11+ local wibox = require (' wibox' )
12+ local gears = require (' gears' )
1313
1414local playerctl = {
1515 player_name = nil ,
@@ -24,14 +24,25 @@ function playerctl:set_player(name)
2424 end
2525end
2626
27- function playerctl :cmd (cmd )
28- return " playerctl -p '" .. self .player_name .. " ' " .. cmd
29- end
27+ function playerctl :cmd (cmd ) return " playerctl -p '" .. self .player_name .. " ' " .. cmd end
28+
29+ local watch_fields = {
30+ [1 ] = ' status' ,
31+ [2 ] = ' xesam:artist' ,
32+ [3 ] = ' xesam:title' ,
33+ [4 ] = ' mpris:artUrl' ,
34+ [5 ] = ' position' ,
35+ [6 ] = ' mpris:length' ,
36+ [7 ] = ' album' ,
37+ [8 ] = ' xesam:contentCreated' ,
38+ }
39+
40+ local watch_cmd = string.format (" -f '{{%s}}' metadata" , table.concat (watch_fields , ' }};{{' ))
3041
3142function playerctl :watch (timeout , callback , widget )
32- local cmd = self :cmd (" -f '{{status}};{{xesam:artist}};{{xesam:title}};{{mpris:artUrl}};{{position}};{{mpris:length}};{{album}};{{xesam:contentCreated}}' metadata " )
43+ local cmd = self :cmd (watch_cmd )
3344
34- self .watch_params = {timeout = timeout , callback = callback , widget = widget }
45+ self .watch_params = { timeout = timeout , callback = callback , widget = widget }
3546
3647 local cb = function (widget , stdout , _ , _ , _ )
3748 local words = gears .string .split (stdout , ' ;' )
@@ -63,21 +74,14 @@ function playerctl:watch(timeout, callback, widget)
6374 _ , self .timer = awful .widget .watch (cmd , timeout , cb , widget )
6475end
6576
66- function playerctl :toggle ()
67- awful .spawn (self :cmd (" play-pause" ), false )
68- end
77+ function playerctl :toggle () awful .spawn (self :cmd (' play-pause' ), false ) end
6978
70- function playerctl :next ()
71- awful .spawn (self :cmd (" next" ), false )
72- end
73-
74- function playerctl :prev ()
75- awful .spawn (self :cmd (" previous" ), false )
76- end
79+ function playerctl :next () awful .spawn (self :cmd (' next' ), false ) end
7780
81+ function playerctl :prev () awful .spawn (self :cmd (' previous' ), false ) end
7882
7983local player_selector_popup = {
80- popup = awful .popup {
84+ popup = awful .popup {
8185 bg = beautiful .bg_normal ,
8286 fg = beautiful .fg_normal ,
8387 ontop = true ,
@@ -87,63 +91,67 @@ local player_selector_popup = {
8791 border_color = beautiful .bg_focus ,
8892 maximum_width = 400 ,
8993 offset = { y = 5 },
90- widget = {}
94+ widget = {},
9195 },
9296
93- rows = { layout = wibox .layout .fixed .vertical },
97+ rows = { layout = wibox .layout .fixed .vertical },
9498}
9599
96100function player_selector_popup :add_radio_button (player_name )
97101 local checkbox = wibox .widget {
102+ layout = wibox .container .place ,
103+ valign = ' center' ,
98104 {
99- checked = player_name == playerctl .player_name ,
100- color = beautiful .bg_normal ,
101- paddings = 2 ,
102- shape = gears .shape .circle ,
103- forced_width = 20 ,
105+ checked = player_name == playerctl .player_name ,
106+ color = beautiful .bg_normal ,
107+ paddings = 2 ,
108+ shape = gears .shape .circle ,
109+ forced_width = 20 ,
104110 forced_height = 20 ,
105- check_color = beautiful .fg_normal ,
106- widget = wibox .widget .checkbox
111+ check_color = beautiful .fg_normal ,
112+ widget = wibox .widget .checkbox ,
107113 },
108- valign = ' center' ,
109- layout = wibox .container .place ,
110114 }
111115
112- checkbox :connect_signal (" button::press" , function ()
116+ checkbox :connect_signal (' button::press' , function ()
113117 playerctl :set_player (player_name )
114118 self :toggle ()
115119 end )
116120
117- table.insert ( self . rows , wibox .widget {
121+ local row = wibox .widget {
118122 {
119123 {
120124 checkbox ,
121125 {
122126 {
123127 text = player_name ,
124128 align = ' left' ,
125- widget = wibox .widget .textbox
129+ widget = wibox .widget .textbox ,
126130 },
127131 left = 10 ,
128- layout = wibox .container .margin
132+ layout = wibox .container .margin ,
129133 },
130134 spacing = 8 ,
131- layout = wibox .layout .align .horizontal
135+ layout = wibox .layout .align .horizontal ,
132136 },
133137 margins = 4 ,
134- layout = wibox .container .margin
138+ layout = wibox .container .margin ,
135139 },
136140 bg = beautiful .bg_normal ,
137141 fg = beautiful .fg_normal ,
138- widget = wibox .container .background
139- })
142+ widget = wibox .container .background ,
143+ }
144+
145+ table.insert (self .rows , row )
140146end
141147
142148function player_selector_popup :rebuild ()
143- awful .spawn .easy_async (" playerctl -l" , function (stdout , _ , _ , _ )
144- for i = 0 , # self .rows do self .rows [i ] = nil end
149+ awful .spawn .easy_async (' playerctl -l' , function (stdout , _ , _ , _ )
150+ for i = 0 , # self .rows do
151+ self .rows [i ] = nil
152+ end
145153
146- for name in stdout :gmatch (" [^\r\n ]+" ) do
154+ for name in stdout :gmatch (' [^\r\n ]+' ) do
147155 if name ~= ' ' and name ~= nil then
148156 self :add_radio_button (name )
149157 end
165173
166174local function duration (microseconds )
167175 if microseconds == nil then
168- return " --:--"
176+ return ' --:--'
169177 end
170178
171179 local seconds = math.floor (microseconds / 1000000 )
@@ -174,9 +182,9 @@ local function duration(microseconds)
174182 local hours = math.floor (minutes / 60 )
175183 minutes = minutes - hours * 60
176184 if hours >= 1 then
177- return string.format (" %d:%02d:%02d" , hours , minutes , seconds )
185+ return string.format (' %d:%02d:%02d' , hours , minutes , seconds )
178186 end
179- return string.format (" %d:%02d" , minutes , seconds )
187+ return string.format (' %d:%02d' , minutes , seconds )
180188end
181189
182190local mpris_widget = {}
@@ -186,19 +194,19 @@ local function worker(user_args)
186194
187195 local font = args .font or ' Roboto Condensed 16px'
188196
189- local path_to_icons = " /usr/share/icons/Adwaita"
197+ local path_to_icons = ' /usr/share/icons/Adwaita'
190198
191- local pause_icon = args .pause_icon or path_to_icons .. " /symbolic/actions/media-playback-pause-symbolic.svg"
192- local play_icon = args .play_icon or path_to_icons .. " /symbolic/actions/media-playback-start-symbolic.svg"
193- local stop_icon = args .stop_icon or path_to_icons .. " /symbolic/actions/media-playback-stop-symbolic.svg"
194- local library_icon = args .library_icon or path_to_icons .. " /symbolic/places/folder-music-symbolic.svg"
195- local popup_width = args .popup_width or 300
199+ local pause_icon = args .pause_icon or path_to_icons .. ' /symbolic/actions/media-playback-pause-symbolic.svg'
200+ local play_icon = args .play_icon or path_to_icons .. ' /symbolic/actions/media-playback-start-symbolic.svg'
201+ local stop_icon = args .stop_icon or path_to_icons .. ' /symbolic/actions/media-playback-stop-symbolic.svg'
202+ local library_icon = args .library_icon or path_to_icons .. ' /symbolic/places/folder-music-symbolic.svg'
203+ local popup_width = args .popup_width or 300
196204
197205 playerctl .player_name = args .default_player or ' mpv'
198206
199207 local icon = wibox .widget {
200208 widget = wibox .widget .imagebox ,
201- image = play_icon
209+ image = play_icon ,
202210 }
203211
204212 local progress_widget = wibox .widget {
@@ -212,18 +220,18 @@ local function worker(user_args)
212220 forced_height = 24 ,
213221 forced_width = 24 ,
214222 rounded_edge = true ,
215- colors = {" #ffffff11" , " black" },
223+ colors = { ' #ffffff11' , ' black' },
216224 paddings = 2 ,
217225 }
218226
219227 local artist_widget = wibox .widget {
220228 font = font ,
221- widget = wibox .widget .textbox
229+ widget = wibox .widget .textbox ,
222230 }
223231
224232 local title_widget = wibox .widget {
225233 font = font ,
226- widget = wibox .widget .textbox
234+ widget = wibox .widget .textbox ,
227235 }
228236
229237 mpris_widget = wibox .widget {
@@ -242,30 +250,30 @@ local function worker(user_args)
242250 }
243251
244252 local metadata_widget = wibox .widget {
245- widget = wibox .widget .textbox ,
246- font = font ,
253+ widget = wibox .widget .textbox ,
254+ font = font ,
247255 forced_height = 100 ,
248- forced_width = popup_width ,
256+ forced_width = popup_width ,
249257 }
250258
251259 local update_metadata = function (meta )
252260 artist_widget :set_text (meta .artist )
253261 title_widget :set_text (meta .current_song )
254262
255- local s = meta .album ;
263+ local s = meta .album
256264 if meta .year ~= nil and # meta .year == 4 then
257- s = s .. " ( " .. meta .year .. " ) "
265+ s = s .. ' ( ' .. meta .year .. ' ) '
258266 end
259- s = s .. " \n " .. meta .current_song .. " ( " .. duration (meta .position ) .. " / " .. duration (meta .length ) .. " ) "
267+ s = s .. ' \n ' .. meta .current_song .. ' ( ' .. duration (meta .position ) .. ' / ' .. duration (meta .length ) .. ' ) '
260268 metadata_widget :set_text (s )
261269
262- progress_widget .values = {1.0 - (meta .progress or 0.0 ), meta .progress or 0.0 }
270+ progress_widget .values = { 1.0 - (meta .progress or 0.0 ), meta .progress or 0.0 }
263271
264272 -- poor man's urldecode
265- local art_url = meta .art_url :gsub (" file://" , " / " )
266- art_url = art_url :gsub (" %%(%x%x)" , function (x ) return string.char (tonumber (x , 16 )) end )
273+ local art_url = meta .art_url :gsub (' file://' , ' / ' )
274+ art_url = art_url :gsub (' %%(%x%x)' , function (x ) return string.char (tonumber (x , 16 )) end )
267275
268- if art_url ~= nil and art_url ~= " " then
276+ if art_url ~= nil and art_url ~= ' ' then
269277 cover_art_widget .image = art_url
270278 cover_art_widget .forced_height = popup_width
271279 else
@@ -277,19 +285,19 @@ local function worker(user_args)
277285 local update_graphic = function (widget , metadata )
278286 if metadata .current_song ~= nil then
279287 if string.len (metadata .current_song ) > 40 then
280- metadata .current_song = string.sub (metadata .current_song , 0 , 38 ) .. " … "
288+ metadata .current_song = string.sub (metadata .current_song , 0 , 38 ) .. ' … '
281289 end
282290 end
283291
284- if metadata .status == " Playing" then
292+ if metadata .status == ' Playing' then
285293 icon .image = pause_icon
286294 widget .colors = { beautiful .widget_main_color }
287295 update_metadata (metadata )
288- elseif metadata .status == " Paused" then
296+ elseif metadata .status == ' Paused' then
289297 icon .image = play_icon
290298 widget .colors = { beautiful .widget_main_color }
291299 update_metadata (metadata )
292- elseif metadata .status == " Stopped" then
300+ elseif metadata .status == ' Stopped' then
293301 icon .image = stop_icon
294302 else -- no player is running
295303 icon .image = library_icon
@@ -310,27 +318,25 @@ local function worker(user_args)
310318
311319 local mpris_popup = awful .popup {
312320 border_color = beautiful .border_color ,
313- ontop = true ,
314- visible = false ,
321+ ontop = true ,
322+ visible = false ,
315323 widget = wibox .widget {
316324 cover_art_widget ,
317325 metadata_widget ,
318326 layout = wibox .layout .fixed .vertical ,
319- }
327+ },
320328 }
321329
322- mpris_widget :connect_signal (' mouse::enter' ,
323- function ()
324- mpris_popup .visible = true
325- mpris_popup :move_next_to (mouse .current_widget_geometry )
326- end )
327- mpris_widget :connect_signal (' mouse::leave' ,
328- function ()
329- mpris_popup .visible = false
330- end )
330+ mpris_widget :connect_signal (' mouse::enter' , function ()
331+ mpris_popup .visible = true
332+ mpris_popup :move_next_to (mouse .current_widget_geometry )
333+ end )
334+ mpris_widget :connect_signal (' mouse::leave' , function () mpris_popup .visible = false end )
331335 -- }}
332336
333337 return mpris_widget
334338end
335339
336- return setmetatable (mpris_widget , { __call = function (_ , ...) return worker (... ) end })
340+ return setmetatable (mpris_widget , {
341+ __call = function (_ , ...) return worker (... ) end ,
342+ })
0 commit comments