Skip to content

Commit 2e42269

Browse files
committed
fix duration function, allow to override icons and font
1 parent f944270 commit 2e42269

File tree

1 file changed

+89
-81
lines changed

1 file changed

+89
-81
lines changed

mpris-widget/init.lua

Lines changed: 89 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ local watch = require("awful.widget.watch")
1111
local wibox = require("wibox")
1212
local gears = require("gears")
1313

14-
local PATH_TO_ICONS = "/usr/share/icons/Adwaita"
15-
local PAUSE_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-pause-symbolic.svg"
16-
local PLAY_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-start-symbolic.svg"
17-
local STOP_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-stop-symbolic.svg"
18-
local LIBRARY_ICON_NAME = PATH_TO_ICONS .. "/symbolic/places/folder-music-symbolic.svg"
19-
20-
local FONT = 'Roboto Condensed 16px'
21-
2214
local playerctl = {
2315
player_name = 'mpv',
2416
}
@@ -44,18 +36,19 @@ function playerctl:watch(timeout, callback, widget)
4436
local cb = function(widget, stdout, _, _, _)
4537
local words = gears.string.split(stdout, ';')
4638

47-
local progress
48-
if words[5] ~= nil and words[6] ~= nil then
49-
progress = tonumber(words[5]) / tonumber(words[6])
39+
local position, length, progress = tonumber(words[5]), tonumber(words[6])
40+
41+
if position ~= nil and length ~= nil then
42+
progress = position / length
5043
end
5144

5245
local metadata = {
5346
status = words[1],
5447
artist = words[2],
5548
current_song = words[3],
5649
art_url = words[4],
57-
position = words[5],
58-
length = words[6],
50+
position = position,
51+
length = length,
5952
album = words[7],
6053
progress = progress,
6154
}
@@ -78,61 +71,6 @@ function playerctl:prev()
7871
awful.spawn(self:cmd("previous"), false)
7972
end
8073

81-
local icon = wibox.widget {
82-
id = "icon",
83-
widget = wibox.widget.imagebox,
84-
image = PLAY_ICON_NAME
85-
}
86-
87-
local progress_widget = wibox.widget {
88-
id = 'progress',
89-
widget = wibox.container.arcchart,
90-
icon,
91-
min_value = 0,
92-
max_value = 1,
93-
value = 0,
94-
thickness = 2,
95-
start_angle = 4.71238898, -- 2pi*3/4
96-
forced_height = 24,
97-
forced_width = 24,
98-
rounded_edge = true,
99-
bg = "#ffffff11",
100-
paddings = 2,
101-
}
102-
103-
local artist_widget = wibox.widget {
104-
id = 'artist',
105-
font = FONT,
106-
widget = wibox.widget.textbox
107-
}
108-
109-
local title_widget = wibox.widget {
110-
id = 'title',
111-
font = FONT,
112-
widget = wibox.widget.textbox
113-
}
114-
115-
local mpris_widget = wibox.widget {
116-
artist_widget,
117-
progress_widget,
118-
title_widget,
119-
spacing = 4,
120-
layout = wibox.layout.fixed.horizontal,
121-
}
122-
123-
local cover_art_widget = wibox.widget {
124-
widget = wibox.widget.imagebox,
125-
forced_height = 0,
126-
forced_width = 300,
127-
resize_allowed = true,
128-
}
129-
130-
local metadata_widget = wibox.widget {
131-
widget = wibox.widget.textbox,
132-
font = FONT,
133-
forced_height = 100,
134-
forced_width = 300,
135-
}
13674

13775
local player_selector_popup = {
13876
popup = awful.popup {
@@ -198,8 +136,9 @@ function player_selector_popup:add_radio_button(player_name)
198136
end
199137

200138
function player_selector_popup:rebuild()
201-
self.rows = { layout = wibox.layout.fixed.vertical }
202139
awful.spawn.easy_async("playerctl -l", function(stdout, _, _, _)
140+
for i = 0, #self.rows do self.rows[i] = nil end
141+
203142
for name in stdout:gmatch("[^\r\n]+") do
204143
if name ~= '' and name ~= nil then
205144
self:add_radio_button(name)
@@ -221,18 +160,87 @@ function player_selector_popup:toggle()
221160
end
222161

223162
local function duration(microseconds)
224-
local seconds = microseconds / 1000000
225-
local minutes = seconds / 60
226-
seconds = seconds % 60
227-
local hours = minutes / 60
228-
minutes = minutes % 60
163+
if microseconds == nil then
164+
return "--:--"
165+
end
166+
167+
local seconds = math.floor(microseconds / 1000000)
168+
local minutes = math.floor(seconds / 60)
169+
seconds = seconds - minutes * 60
170+
local hours = math.floor(minutes / 60)
171+
minutes = minutes - hours * 60
229172
if hours >= 1 then
230-
return string.format("%.f:%02.f:%02.f", hours, minutes, seconds)
173+
return string.format("%d:%02d:%02d", hours, minutes, seconds)
231174
end
232-
return string.format("%.f:%02.f", minutes, seconds)
175+
return string.format("%d:%02d", minutes, seconds)
233176
end
234177

235-
local function worker()
178+
local mpris_widget = {}
179+
180+
local function worker(user_args)
181+
local args = user_args or {}
182+
183+
local font = args.font or 'Roboto Condensed 16px'
184+
185+
local path_to_icons = "/usr/share/icons/Adwaita"
186+
187+
local pause_icon = args.pause_icon or path_to_icons .. "/symbolic/actions/media-playback-pause-symbolic.svg"
188+
local play_icon = args.play_icon or path_to_icons .. "/symbolic/actions/media-playback-start-symbolic.svg"
189+
local stop_icon = args.stop_icon or path_to_icons .. "/symbolic/actions/media-playback-stop-symbolic.svg"
190+
local library_icon = args.library_icon or path_to_icons .. "/symbolic/places/folder-music-symbolic.svg"
191+
192+
local icon = wibox.widget {
193+
widget = wibox.widget.imagebox,
194+
image = play_icon
195+
}
196+
197+
local progress_widget = wibox.widget {
198+
widget = wibox.container.arcchart,
199+
icon,
200+
min_value = 0,
201+
max_value = 1,
202+
value = 0,
203+
thickness = 2,
204+
start_angle = 4.71238898, -- 2pi*3/4
205+
forced_height = 24,
206+
forced_width = 24,
207+
rounded_edge = true,
208+
bg = "#ffffff11",
209+
paddings = 2,
210+
}
211+
212+
local artist_widget = wibox.widget {
213+
font = font,
214+
widget = wibox.widget.textbox
215+
}
216+
217+
local title_widget = wibox.widget {
218+
font = font,
219+
widget = wibox.widget.textbox
220+
}
221+
222+
mpris_widget = wibox.widget {
223+
artist_widget,
224+
progress_widget,
225+
title_widget,
226+
spacing = 4,
227+
layout = wibox.layout.fixed.horizontal,
228+
}
229+
230+
local cover_art_widget = wibox.widget {
231+
widget = wibox.widget.imagebox,
232+
forced_height = 0,
233+
forced_width = 300,
234+
resize_allowed = true,
235+
}
236+
237+
local metadata_widget = wibox.widget {
238+
widget = wibox.widget.textbox,
239+
font = font,
240+
forced_height = 100,
241+
forced_width = 300,
242+
}
243+
236244
local update_metadata = function(meta)
237245
artist_widget:set_text(meta.artist)
238246
title_widget:set_text(meta.current_song)
@@ -260,17 +268,17 @@ local function worker()
260268
end
261269

262270
if metadata.status == "Playing" then
263-
icon.image = PLAY_ICON_NAME
271+
icon.image = play_icon
264272
widget.colors = { beautiful.widget_main_color }
265273
update_metadata(metadata)
266274
elseif metadata.status == "Paused" then
267-
icon.image = PAUSE_ICON_NAME
275+
icon.image = pause_icon
268276
widget.colors = { beautiful.widget_main_color }
269277
update_metadata(metadata)
270278
elseif metadata.status == "Stopped" then
271-
icon.image = STOP_ICON_NAME
279+
icon.image = stop_icon
272280
else -- no player is running
273-
icon.image = LIBRARY_ICON_NAME
281+
icon.image = library_icon
274282
widget.colors = { beautiful.widget_red }
275283
end
276284
end

0 commit comments

Comments
 (0)