5
5
-- requires - playerctl
6
6
-- @copyright 2020
7
7
---- ---------------------------------------------
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' )
13
13
14
14
local playerctl = {
15
15
player_name = nil ,
@@ -24,14 +24,25 @@ function playerctl:set_player(name)
24
24
end
25
25
end
26
26
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 , ' }};{{' ))
30
41
31
42
function 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 )
33
44
34
- self .watch_params = {timeout = timeout , callback = callback , widget = widget }
45
+ self .watch_params = { timeout = timeout , callback = callback , widget = widget }
35
46
36
47
local cb = function (widget , stdout , _ , _ , _ )
37
48
local words = gears .string .split (stdout , ' ;' )
@@ -63,21 +74,14 @@ function playerctl:watch(timeout, callback, widget)
63
74
_ , self .timer = awful .widget .watch (cmd , timeout , cb , widget )
64
75
end
65
76
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
69
78
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
77
80
81
+ function playerctl :prev () awful .spawn (self :cmd (' previous' ), false ) end
78
82
79
83
local player_selector_popup = {
80
- popup = awful .popup {
84
+ popup = awful .popup {
81
85
bg = beautiful .bg_normal ,
82
86
fg = beautiful .fg_normal ,
83
87
ontop = true ,
@@ -87,63 +91,67 @@ local player_selector_popup = {
87
91
border_color = beautiful .bg_focus ,
88
92
maximum_width = 400 ,
89
93
offset = { y = 5 },
90
- widget = {}
94
+ widget = {},
91
95
},
92
96
93
- rows = { layout = wibox .layout .fixed .vertical },
97
+ rows = { layout = wibox .layout .fixed .vertical },
94
98
}
95
99
96
100
function player_selector_popup :add_radio_button (player_name )
97
101
local checkbox = wibox .widget {
102
+ layout = wibox .container .place ,
103
+ valign = ' center' ,
98
104
{
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 ,
104
110
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 ,
107
113
},
108
- valign = ' center' ,
109
- layout = wibox .container .place ,
110
114
}
111
115
112
- checkbox :connect_signal (" button::press" , function ()
116
+ checkbox :connect_signal (' button::press' , function ()
113
117
playerctl :set_player (player_name )
114
118
self :toggle ()
115
119
end )
116
120
117
- table.insert ( self . rows , wibox .widget {
121
+ local row = wibox .widget {
118
122
{
119
123
{
120
124
checkbox ,
121
125
{
122
126
{
123
127
text = player_name ,
124
128
align = ' left' ,
125
- widget = wibox .widget .textbox
129
+ widget = wibox .widget .textbox ,
126
130
},
127
131
left = 10 ,
128
- layout = wibox .container .margin
132
+ layout = wibox .container .margin ,
129
133
},
130
134
spacing = 8 ,
131
- layout = wibox .layout .align .horizontal
135
+ layout = wibox .layout .align .horizontal ,
132
136
},
133
137
margins = 4 ,
134
- layout = wibox .container .margin
138
+ layout = wibox .container .margin ,
135
139
},
136
140
bg = beautiful .bg_normal ,
137
141
fg = beautiful .fg_normal ,
138
- widget = wibox .container .background
139
- })
142
+ widget = wibox .container .background ,
143
+ }
144
+
145
+ table.insert (self .rows , row )
140
146
end
141
147
142
148
function 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
145
153
146
- for name in stdout :gmatch (" [^\r\n ]+" ) do
154
+ for name in stdout :gmatch (' [^\r\n ]+' ) do
147
155
if name ~= ' ' and name ~= nil then
148
156
self :add_radio_button (name )
149
157
end
165
173
166
174
local function duration (microseconds )
167
175
if microseconds == nil then
168
- return " --:--"
176
+ return ' --:--'
169
177
end
170
178
171
179
local seconds = math.floor (microseconds / 1000000 )
@@ -174,9 +182,9 @@ local function duration(microseconds)
174
182
local hours = math.floor (minutes / 60 )
175
183
minutes = minutes - hours * 60
176
184
if hours >= 1 then
177
- return string.format (" %d:%02d:%02d" , hours , minutes , seconds )
185
+ return string.format (' %d:%02d:%02d' , hours , minutes , seconds )
178
186
end
179
- return string.format (" %d:%02d" , minutes , seconds )
187
+ return string.format (' %d:%02d' , minutes , seconds )
180
188
end
181
189
182
190
local mpris_widget = {}
@@ -186,19 +194,19 @@ local function worker(user_args)
186
194
187
195
local font = args .font or ' Roboto Condensed 16px'
188
196
189
- local path_to_icons = " /usr/share/icons/Adwaita"
197
+ local path_to_icons = ' /usr/share/icons/Adwaita'
190
198
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
196
204
197
205
playerctl .player_name = args .default_player or ' mpv'
198
206
199
207
local icon = wibox .widget {
200
208
widget = wibox .widget .imagebox ,
201
- image = play_icon
209
+ image = play_icon ,
202
210
}
203
211
204
212
local progress_widget = wibox .widget {
@@ -212,18 +220,18 @@ local function worker(user_args)
212
220
forced_height = 24 ,
213
221
forced_width = 24 ,
214
222
rounded_edge = true ,
215
- colors = {" #ffffff11" , " black" },
223
+ colors = { ' #ffffff11' , ' black' },
216
224
paddings = 2 ,
217
225
}
218
226
219
227
local artist_widget = wibox .widget {
220
228
font = font ,
221
- widget = wibox .widget .textbox
229
+ widget = wibox .widget .textbox ,
222
230
}
223
231
224
232
local title_widget = wibox .widget {
225
233
font = font ,
226
- widget = wibox .widget .textbox
234
+ widget = wibox .widget .textbox ,
227
235
}
228
236
229
237
mpris_widget = wibox .widget {
@@ -242,30 +250,30 @@ local function worker(user_args)
242
250
}
243
251
244
252
local metadata_widget = wibox .widget {
245
- widget = wibox .widget .textbox ,
246
- font = font ,
253
+ widget = wibox .widget .textbox ,
254
+ font = font ,
247
255
forced_height = 100 ,
248
- forced_width = popup_width ,
256
+ forced_width = popup_width ,
249
257
}
250
258
251
259
local update_metadata = function (meta )
252
260
artist_widget :set_text (meta .artist )
253
261
title_widget :set_text (meta .current_song )
254
262
255
- local s = meta .album ;
263
+ local s = meta .album
256
264
if meta .year ~= nil and # meta .year == 4 then
257
- s = s .. " ( " .. meta .year .. " ) "
265
+ s = s .. ' ( ' .. meta .year .. ' ) '
258
266
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 ) .. ' ) '
260
268
metadata_widget :set_text (s )
261
269
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 }
263
271
264
272
-- 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 )
267
275
268
- if art_url ~= nil and art_url ~= " " then
276
+ if art_url ~= nil and art_url ~= ' ' then
269
277
cover_art_widget .image = art_url
270
278
cover_art_widget .forced_height = popup_width
271
279
else
@@ -277,19 +285,19 @@ local function worker(user_args)
277
285
local update_graphic = function (widget , metadata )
278
286
if metadata .current_song ~= nil then
279
287
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 ) .. ' … '
281
289
end
282
290
end
283
291
284
- if metadata .status == " Playing" then
292
+ if metadata .status == ' Playing' then
285
293
icon .image = pause_icon
286
294
widget .colors = { beautiful .widget_main_color }
287
295
update_metadata (metadata )
288
- elseif metadata .status == " Paused" then
296
+ elseif metadata .status == ' Paused' then
289
297
icon .image = play_icon
290
298
widget .colors = { beautiful .widget_main_color }
291
299
update_metadata (metadata )
292
- elseif metadata .status == " Stopped" then
300
+ elseif metadata .status == ' Stopped' then
293
301
icon .image = stop_icon
294
302
else -- no player is running
295
303
icon .image = library_icon
@@ -310,27 +318,25 @@ local function worker(user_args)
310
318
311
319
local mpris_popup = awful .popup {
312
320
border_color = beautiful .border_color ,
313
- ontop = true ,
314
- visible = false ,
321
+ ontop = true ,
322
+ visible = false ,
315
323
widget = wibox .widget {
316
324
cover_art_widget ,
317
325
metadata_widget ,
318
326
layout = wibox .layout .fixed .vertical ,
319
- }
327
+ },
320
328
}
321
329
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 )
331
335
-- }}
332
336
333
337
return mpris_widget
334
338
end
335
339
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