2022-07-16 16:11:20 -07:00
|
|
|
local battery = req_rel(..., "util")
|
|
|
|
local widget = {}
|
2023-08-26 18:05:21 -07:00
|
|
|
local dotgrid = require("widgets.dotgrid")
|
2022-07-16 16:11:20 -07:00
|
|
|
|
|
|
|
-- Percentages to warn at
|
|
|
|
-- (must be in order least -> greatest)
|
|
|
|
widget.warnings = {
|
|
|
|
5, 10, 25, 50
|
|
|
|
}
|
|
|
|
|
|
|
|
widget.warninglog = {}
|
|
|
|
|
|
|
|
for i=1, #widget.warnings do
|
|
|
|
widget.warninglog[i] = false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2023-08-26 18:05:21 -07:00
|
|
|
widget.dots = dotgrid:new();
|
|
|
|
widget.dots.on_color = {1, 0, 0}
|
|
|
|
widget.dots.off_color = {0.5, 0, 0}
|
|
|
|
widget.dots.spacing = beautiful.dpi(2)
|
2022-07-16 16:11:20 -07:00
|
|
|
|
|
|
|
widget.widget = wibox.widget {
|
|
|
|
{
|
2023-08-26 18:05:21 -07:00
|
|
|
widget.dots,
|
|
|
|
top = beautiful.dpi(2),
|
|
|
|
bottom = beautiful.dpi(2),
|
|
|
|
left = beautiful.dpi(2),
|
|
|
|
right = beautiful.dpi(2),
|
|
|
|
layout = wibox.container.margin,
|
2022-07-16 16:11:20 -07:00
|
|
|
},
|
|
|
|
layout = wibox.container.background,
|
|
|
|
}
|
|
|
|
|
2023-08-26 18:05:21 -07:00
|
|
|
|
2022-07-16 16:11:20 -07:00
|
|
|
widget.widget:connect_signal("mouse::enter", function(result)
|
|
|
|
widget.widget.bg = beautiful.color.bar.hover_bg
|
|
|
|
end)
|
|
|
|
|
|
|
|
widget.widget:connect_signal("mouse::leave", function(result)
|
|
|
|
widget.widget.bg = beautiful.color.transparent
|
|
|
|
end)
|
|
|
|
|
|
|
|
widget.update = function(stdout)
|
|
|
|
local batpec = string.match(stdout, "(%d?%d?%d)%%")
|
|
|
|
batpec = tonumber(string.format("% 3d", batpec))
|
|
|
|
|
2023-08-21 19:53:40 -07:00
|
|
|
local discharging = string.match(stdout, "Discharging") or false
|
2022-07-16 16:11:20 -07:00
|
|
|
|
|
|
|
-- Handle low power notifications
|
|
|
|
if discharging then
|
|
|
|
for i=1, #widget.warnings do
|
|
|
|
v = widget.warnings[i]
|
|
|
|
if (batpec <= v) and (not widget.warninglog[i]) then
|
|
|
|
widget.warninglog[i] = true
|
|
|
|
|
|
|
|
naughty.notify({
|
|
|
|
title = "Low power",
|
|
|
|
text = "Battery is at " .. tostring(batpec) .. "%",
|
2022-07-16 21:37:04 -07:00
|
|
|
icon = beautiful.icons.battery.caution,
|
2022-07-16 16:11:20 -07:00
|
|
|
|
|
|
|
timeout = 5,
|
|
|
|
ignore_suspend = true,
|
|
|
|
|
2022-09-10 21:52:40 -07:00
|
|
|
border_color = beautiful.color.battery.danger,
|
2022-07-16 16:11:20 -07:00
|
|
|
preset = beautiful.notification_templates.bottom_right
|
|
|
|
})
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
for i=1, #widget.warnings do
|
|
|
|
if (batpec >= widget.warnings[i]) then
|
|
|
|
widget.warninglog[i] = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-26 18:05:21 -07:00
|
|
|
widget.dots.value = batpec
|
2022-07-16 16:11:20 -07:00
|
|
|
|
|
|
|
if discharging then
|
2023-08-26 18:05:21 -07:00
|
|
|
widget.dots.on_color = {1, 0, 0}
|
|
|
|
widget.dots.off_color = {0.5, 0, 0}
|
2022-07-16 16:11:20 -07:00
|
|
|
else
|
2023-08-26 18:05:21 -07:00
|
|
|
widget.dots.on_color = {1, 1, 0}
|
|
|
|
widget.dots.off_color = {0.5, 0.5, 0}
|
2022-07-16 16:11:20 -07:00
|
|
|
end
|
2023-08-26 18:05:21 -07:00
|
|
|
|
|
|
|
widget.dots:emit_signal("widget::redraw_needed")
|
2022-07-16 16:11:20 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
widget.readupdate = function()
|
|
|
|
battery.status(
|
|
|
|
function(stdout, stderr, exitreason, exitcode)
|
|
|
|
widget.update(stdout)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
widget.widget:connect_signal("button::press",
|
|
|
|
function(_, _, _, button, mods)
|
|
|
|
if (button == 1) then
|
|
|
|
battery.status(
|
|
|
|
function(stdout, stderr, exitreason, exitcode)
|
2023-08-26 18:05:21 -07:00
|
|
|
local batpec = string.match(stdout, "(%d?%d?%d)%%")
|
|
|
|
batpec = tonumber(string.format("% 3d", batpec))
|
|
|
|
|
|
|
|
local batstat = string.match(stdout, "Discharging") or false
|
|
|
|
|
|
|
|
debug_message(batpec)
|
2022-07-16 16:11:20 -07:00
|
|
|
|
|
|
|
if batstat then
|
|
|
|
batstat = "Discharging, "
|
|
|
|
else
|
|
|
|
batstat = "Charging, "
|
|
|
|
end
|
|
|
|
|
|
|
|
local out = naughty.notify({
|
|
|
|
title = "Battery:",
|
|
|
|
text = batstat .. batpec .. "%",
|
2022-07-16 21:37:04 -07:00
|
|
|
icon = widget.icon.image,
|
2022-07-16 16:11:20 -07:00
|
|
|
replaces_id = widget.notid,
|
|
|
|
ignore_suspend = true,
|
|
|
|
|
|
|
|
preset = beautiful.notification_templates.bottom_right
|
|
|
|
})
|
|
|
|
widget.notid = out.id
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
battery.watch(
|
2023-08-26 18:05:21 -07:00
|
|
|
2,
|
2022-07-16 16:11:20 -07:00
|
|
|
function(_, stdout)
|
|
|
|
widget.update(stdout)
|
|
|
|
end,
|
|
|
|
widget.widget
|
|
|
|
)
|
|
|
|
|
|
|
|
return widget
|