Skip to content

Instantly share code, notes, and snippets.

@yfyf
Created September 25, 2013 19:32
Show Gist options
  • Save yfyf/6704830 to your computer and use it in GitHub Desktop.
Save yfyf/6704830 to your computer and use it in GitHub Desktop.
awesomewm battery widget with colour reflecting the remaining battery life and a notification for low battery
-- Tooltip comes from: https://github.com/liliff/web/blob/master/_posts/2012-04-05-first-dive-into-lua-battery-widget.md
function rgbToHex(r, g, b)
return string.format("#%0.2X%0.2X%0.2X", r, g, b)
end
function chargeToColour(charge)
local unit = 255 / 100
local amount = math.floor(unit * charge)
return rgbToHex(255 - amount, amount, 25)
end
-- {{{ Battery status
batwidget = awful.widget.progressbar()
batwidget:set_width(10)
batwidget:set_height(10)
batwidget:set_vertical(true)
batwidget:set_background_color('#494B4F')
batwidget_t = awful.tooltip({ objects = { batwidget },})
vicious.register(batwidget, vicious.widgets.bat, function (widget, args)
batwidget_t:set_text(
"State: " .. args[1] .. " | " ..
"Charge: " .. args[2] .. "% | " ..
"Remaining: " .. args[3]
)
local charge = args[2]
local colour = chargeToColour(charge)
batwidget:set_color(colour)
if charge <= 5 then
naughty.notify({ text="Battery is low! "
.. charge .." percent remaining." })
end
return charge
end , 60, "BAT0")
-- }}}
@motiejus
Copy link

This looks like an awesome widget.

However, in my rc.lua I cannot see vicious defined. Can you show how that variable is defined in your setup?

@motiejus
Copy link

OK, found the docs (doh google). Nevermind.

@X-Raym
Copy link

X-Raym commented Jun 15, 2015

Thanks for the rgb.hex :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment