Added battery module

master
Mark 2022-07-16 16:11:20 -07:00
parent 3014105bbf
commit a4f80add9e
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
7 changed files with 240 additions and 225 deletions

View File

@ -17,16 +17,6 @@ return {
end
},
battery = {
watch = function(timeout, callback, widget)
awful.widget.watch(script_dir .. "battery", timeout, callback, widget)
end,
status = function(callback)
awful.spawn.easy_async(script_dir .. "battery", callback)
end,
},
rofi = {
launcher = function()
awful.spawn("rofi -show drun -theme \"" .. dir .. "/rofi/launcher.rasi\"")

View File

@ -47,7 +47,7 @@ if conf.backlight_enabled then
desktop.widgets.backlight = modules.backlight.widgets.backlight
end
if conf.battery_enabled then
desktop.widgets.battery = require("desktop.widgets.battery")
desktop.widgets.battery = modules.battery.widgets.battery
end
if conf.mpc_enabled then
desktop.widgets.mpc = modules.mpc.widgets.mpc

View File

@ -1,214 +0,0 @@
local battery = {}
-- Percentages to warn at
-- (must be in order least -> greatest)
battery.warnings = {
5, 10, 25, 50
}
battery.warninglog = {}
for i=1, #battery.warnings do
battery.warninglog[i] = false
end
battery.image_path = beautiful.icons.battery.missing
battery.icon = wibox.widget {
image = beautiful.icons.battery.missing,
resize = true,
widget = wibox.widget.imagebox,
}
battery.progressbar = wibox.widget {
max_value = 100,
widget = wibox.widget.progressbar,
paddings = beautiful.dpi(2),
color = beautiful.color.bar.active,
background_color = beautiful.color.transparent,
border_color = beautiful.color.bar.active,
border_width = beautiful.dpi(1),
margins = beautiful.dpi(3)
}
battery.arc = wibox.widget {
{
battery.icon,
top = beautiful.dpi(1),
bottom = beautiful.dpi(1),
layout = wibox.container.margin,
},
max_value = 100,
thickness = beautiful.dpi(4),
start_angle = 4.71238898, -- 2pi*3/4
--forced_height = beautiful.dpi(16),
--forced_width = beautiful.dpi(16),
colors = {"#27D4CC", "#00446B"},
bg = "#FFFFFF30",
paddings = beautiful.dpi(2),
widget = wibox.container.arcchart
}
battery.rotator = wibox.widget {
battery.progressbar,
forced_width = beautiful.dpi(15),
direction = "east",
layout = wibox.container.rotate,
}
battery.widget = wibox.widget {
{
{ -- Right space
widget = wibox.widget.separator,
color = beautiful.color.transparent,
forced_width = beautiful.dpi(3)
},
{
battery.arc,
top = beautiful.dpi(2),
bottom = beautiful.dpi(2),
layout = wibox.container.margin,
},
{ -- Left space
widget = wibox.widget.separator,
color = beautiful.color.transparent,
forced_width = beautiful.dpi(3)
},
layout = wibox.layout.align.horizontal,
},
layout = wibox.container.background,
}
battery.widget:connect_signal("mouse::enter", function(result)
battery.widget.bg = beautiful.color.bar.hover_bg
end)
battery.widget:connect_signal("mouse::leave", function(result)
battery.widget.bg = beautiful.color.transparent
end)
battery.update = function(stdout)
local batpec = string.match(stdout, "(%d?%d?%d)%%")
batpec = tonumber(string.format("% 3d", batpec))
local discharging = string.match(stdout, "discharging") or false
-- Handle low power notifications
if discharging then
for i=1, #battery.warnings do
v = battery.warnings[i]
if (batpec <= v) and (not battery.warninglog[i]) then
battery.warninglog[i] = true
naughty.notify({
title = "Low power",
text = "Battery is at " .. tostring(batpec) .. "%",
icon = beautiful.icons.battery.caution,
timeout = 5,
ignore_suspend = true,
border_color = beautiful.color.battery.danger,
preset = beautiful.notification_templates.bottom_right
})
break
end
end
else
for i=1, #battery.warnings do
if (batpec >= battery.warnings[i]) then
battery.warninglog[i] = false
end
end
end
battery.progressbar.value = batpec
battery.arc.value = batpec
if batpec > 60 then
battery.progressbar.color = beautiful.color.battery.good
elseif batpec > 40 then
battery.progressbar.color = beautiful.color.battery.low
elseif batpec <= 40 then
battery.progressbar.color = beautiful.color.battery.danger
end
battery.image_path = beautiful.icons.battery.missing
-- Set current battery icon
if (not discharging) then
if batpec > 80 then battery.image_path = beautiful.icons.battery.charging.full
elseif batpec > 60 then battery.image_path = beautiful.icons.battery.charging.good
elseif batpec > 40 then battery.image_path = beautiful.icons.battery.charging.low
elseif batpec > 20 then battery.image_path = beautiful.icons.battery.charging.caution
elseif batpec <= 20 then battery.image_path = beautiful.icons.battery.charging.empty end
else
if batpec > 80 then battery.image_path = beautiful.icons.battery.full
elseif batpec > 60 then battery.image_path = beautiful.icons.battery.good
elseif batpec > 40 then battery.image_path = beautiful.icons.battery.low
elseif batpec > 20 then battery.image_path = beautiful.icons.battery.caution
elseif batpec <= 20 then battery.image_path = beautiful.icons.battery.empty end
end
battery.icon.image = battery.image_path
if (not discharging) and (batpec > 90) then
battery.progressbar.border_color = beautiful.color.battery.good
elseif (discharging) and (batpec <= 25) then
battery.progressbar.border_color = beautiful.color.battery.danger
else
battery.progressbar.border_color = beautiful.color.bar.active
end
if discharging then
battery.rotator.direction = "east"
else
battery.rotator.direction = "west"
end
end
battery.readupdate = function()
bin.battery.status(
function(stdout, stderr, exitreason, exitcode)
battery.update(stdout)
end
)
end
battery.widget:connect_signal("button::press",
function(_, _, _, button, mods)
if (button == 1) then
bin.battery.status(
function(stdout, stderr, exitreason, exitcode)
local batpec = string.match(stdout, "(%d?%d?%d)%%") -- (\d?\d?\d)\%)
local batstat = string.match(stdout, "discharging") or false
if batstat then
batstat = "Discharging, "
else
batstat = "Charging, "
end
local out = naughty.notify({
title = "Battery:",
text = batstat .. batpec .. "%",
icon = battery.image_path,
replaces_id = battery.notid,
ignore_suspend = true,
preset = beautiful.notification_templates.bottom_right
})
battery.notid = out.id
end
)
end
end
)
bin.battery.watch(10, function(_, stdout) battery.update(stdout) end, battery.widget)
return battery.widget

5
modules/battery/init.lua Normal file
View File

@ -0,0 +1,5 @@
return {
widgets = {
battery = require("modules.battery.widget").widget
}
}

13
modules/battery/util.lua Normal file
View File

@ -0,0 +1,13 @@
local battery = {}
local script = configuration_dir .. "modules/battery/battery.fish"
battery.watch = function(timeout, callback, widget)
awful.widget.watch(script, timeout, callback, widget)
end
battery.status = function(callback)
awful.spawn.easy_async(script, callback)
end
return battery

221
modules/battery/widget.lua Executable file
View File

@ -0,0 +1,221 @@
local battery = req_rel(..., "util")
local widget = {}
-- 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
widget.image_path = beautiful.icons.widget.missing
widget.icon = wibox.widget {
image = beautiful.icons.widget.missing,
resize = true,
widget = wibox.widget.imagebox,
}
widget.progressbar = wibox.widget {
max_value = 100,
widget = wibox.widget.progressbar,
paddings = beautiful.dpi(2),
color = beautiful.color.bar.active,
background_color = beautiful.color.transparent,
border_color = beautiful.color.bar.active,
border_width = beautiful.dpi(1),
margins = beautiful.dpi(3)
}
widget.arc = wibox.widget {
{
widget.icon,
top = beautiful.dpi(1),
bottom = beautiful.dpi(1),
layout = wibox.container.margin,
},
max_value = 100,
thickness = beautiful.dpi(4),
start_angle = 4.71238898, -- 2pi*3/4
--forced_height = beautiful.dpi(16),
--forced_width = beautiful.dpi(16),
colors = {"#27D4CC", "#00446B"},
bg = "#FFFFFF30",
paddings = beautiful.dpi(2),
widget = wibox.container.arcchart
}
widget.rotator = wibox.widget {
widget.progressbar,
forced_width = beautiful.dpi(15),
direction = "east",
layout = wibox.container.rotate,
}
widget.widget = wibox.widget {
{
{ -- Right space
widget = wibox.widget.separator,
color = beautiful.color.transparent,
forced_width = beautiful.dpi(3)
},
{
widget.arc,
top = beautiful.dpi(2),
bottom = beautiful.dpi(2),
layout = wibox.container.margin,
},
{ -- Left space
widget = wibox.widget.separator,
color = beautiful.color.transparent,
forced_width = beautiful.dpi(3)
},
layout = wibox.layout.align.horizontal,
},
layout = wibox.container.background,
}
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))
local discharging = string.match(stdout, "discharging") or false
-- 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) .. "%",
icon = beautiful.icons.widget.caution,
timeout = 5,
ignore_suspend = true,
border_color = beautiful.color.widget.danger,
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
widget.progressbar.value = batpec
widget.arc.value = batpec
if batpec > 60 then
widget.progressbar.color = beautiful.color.widget.good
elseif batpec > 40 then
widget.progressbar.color = beautiful.color.widget.low
elseif batpec <= 40 then
widget.progressbar.color = beautiful.color.widget.danger
end
widget.image_path = beautiful.icons.widget.missing
-- Set current battery icon
if (not discharging) then
if batpec > 80 then widget.image_path = beautiful.icons.widget.charging.full
elseif batpec > 60 then widget.image_path = beautiful.icons.widget.charging.good
elseif batpec > 40 then widget.image_path = beautiful.icons.widget.charging.low
elseif batpec > 20 then widget.image_path = beautiful.icons.widget.charging.caution
elseif batpec <= 20 then widget.image_path = beautiful.icons.widget.charging.empty end
else
if batpec > 80 then widget.image_path = beautiful.icons.widget.full
elseif batpec > 60 then widget.image_path = beautiful.icons.widget.good
elseif batpec > 40 then widget.image_path = beautiful.icons.widget.low
elseif batpec > 20 then widget.image_path = beautiful.icons.widget.caution
elseif batpec <= 20 then widget.image_path = beautiful.icons.widget.empty end
end
widget.icon.image = widget.image_path
if (not discharging) and (batpec > 90) then
widget.progressbar.border_color = beautiful.color.widget.good
elseif (discharging) and (batpec <= 25) then
widget.progressbar.border_color = beautiful.color.widget.danger
else
widget.progressbar.border_color = beautiful.color.bar.active
end
if discharging then
widget.rotator.direction = "east"
else
widget.rotator.direction = "west"
end
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)
local batpec = string.match(stdout, "(%d?%d?%d)%%") -- (\d?\d?\d)\%)
local batstat = string.match(stdout, "discharging") or false
if batstat then
batstat = "Discharging, "
else
batstat = "Charging, "
end
local out = naughty.notify({
title = "Battery:",
text = batstat .. batpec .. "%",
icon = widget.image_path,
replaces_id = widget.notid,
ignore_suspend = true,
preset = beautiful.notification_templates.bottom_right
})
widget.notid = out.id
end
)
end
end
)
battery.watch(
10,
function(_, stdout)
widget.update(stdout)
end,
widget.widget
)
return widget