local awful = require("awful")
local spawn = require("awful.spawn")
local watch = require("awful.widget.watch")
local beautiful = require("beautiful")
local wibox = require("wibox")
local gears = require("gears")
local naughty = require("naughty")
local mpd_widget = {}
local MUSIC_ICON = ' '
local OFF_ICON = ' '
local PLAY_ICON = ' '
local PAUSE_ICON = ' '
local PREV_ICON = ' '
local NEXT_ICON = ' '
local function worker(user_args)
local mpd_bar = wibox.widget {
{
id = "progress",
widget = wibox.widget.progressbar,
max_value = 1,
forced_height = 12,
forced_width = 150,
border_width = 0,
color = beautiful.bg_focus,
background_color = beautiful.bg_normal,
},
{
id = "title",
widget = wibox.widget.textbox,
text = "NO SONG",
valign = "center",
halign = "center",
},
layout = wibox.layout.stack
}
local status_icon = wibox.widget {
id = "status",
widget = wibox.widget.textbox,
text = PLAY_ICON
}
local update = function(widget, stdout, _, _, _)
local current_song = string.gmatch(stdout, "[^\r\n]+")()
stdout = string.gsub(stdout, "\n", "")
local mpd_percent = string.match(stdout, "(%d%d)%%")
local mpd_status = string.match(stdout, "%[(%a+)%]")
widget:get_children_by_id("title")[1].text = current_song
widget:get_children_by_id("progress")[1].value = tonumber(mpd_percent)/100
if mpd_status == "playing" then
status_icon.text = PLAY_ICON
elseif mpd_status == "paused" then
status_icon.text = PAUSE_ICON
end
end
mpd_bar:connect_signal("button::press", function(_, _, _, button)
if button == 1 then awful.spawn("mpc toggle", false)
elseif button == 3 then awful.spawn("kitty -e ncmpcpp", false)
elseif button == 4 then awful.spawn("mpc next", false)
elseif button == 5 then awful.spawn("mpc prev", false)
end
end)
watch("mpc status", 1, update, mpd_bar)
mpd_widget = wibox.widget {
status_icon,
mpd_bar,
layout = wibox.layout.align.horizontal,
spacing = 8,
}
return mpd_widget
end
return setmetatable(mpd_widget, { __call = function(_, ...)
return worker(...)
end})