Skip to content

Instantly share code, notes, and snippets.

@lucasb-eyer
Created February 7, 2015 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasb-eyer/ace20ce1487063947614 to your computer and use it in GitHub Desktop.
Save lucasb-eyer/ace20ce1487063947614 to your computer and use it in GitHub Desktop.
awesomewm battery widget
-- Create a battery widget on laptops
mybattery = nil
mybattery_timer = nil
if lucasb.file_exists("/sys/class/power_supply/BAT0/status") then
mybattery = awful.widget.progressbar()
mybattery:set_width(13)
mybattery:set_vertical(true)
mybattery_tip = awful.tooltip({objects = {mybattery}})
lastalarm = 0
if not mybattery_timer then
battery_timer = timer({timeout = 1})
battery_timer:connect_signal("timeout", function()
local fcap = io.open("/sys/class/power_supply/BAT0/capacity", "r")
local fstat = io.open("/sys/class/power_supply/BAT0/status", "r")
if fcap and fstat then
local cap = tonumber(fcap:read())
local stat = fstat:read()
io.close(fcap)
io.close(fstat)
mybattery:set_value(cap/100.0)
mybattery_tip:set_text(cap .. "%, " .. stat)
if stat == "Charging" or stat == "Full" then
mybattery:set_color(beautiful.bg_focus)
else
if cap >= 4 then
mybattery:set_color(beautiful.colors.yellow)
else
mybattery:set_color(beautiful.bg_urgent)
-- Don't spam that thing, that'd be annoying
if lastalarm == 0 then
naughty.notify({
preset = naughty.config.presets.critical,
title = "Low Battery",
text = "Battery's almost empty! (" .. cap .. "% left)",
font = "Liberation 20",
timeout = 30
})
end
lastalarm = lastalarm + 1
if lastalarm > 30 then lastalarm = 0 end
end
end
end
end)
battery_timer:start()
battery_timer:emit_signal("timeout")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment