Skip to content

Instantly share code, notes, and snippets.

@whit
Created October 27, 2011 18:55
Show Gist options
  • Save whit/1320472 to your computer and use it in GitHub Desktop.
Save whit/1320472 to your computer and use it in GitHub Desktop.
Simple awesome battery widget for notebooks with SMAPI (IBM/Lenovo).
-- register widget
batterywidget = widget({type = "textbox", name = "batterywidget", align = "right" })
-- timer
awful.hooks.timer.register(10, function()
battery('BAT0')
end)
-- widget function
function battery(adapter)
spacer = " "
local fcur = io.open("/sys/devices/platform/smapi/"..adapter.."/remaining_percent")
local fsta = io.open("/sys/devices/platform/smapi/"..adapter.."/state")
local cur_percent = tonumber(fcur:read())
local sta = fsta:read()
fcur:close()
fsta:close()
if sta:match("discharging") then
dir = "↘"
if cur_percent < 20 then
color = "orange"
elseif cur_percent < 7 then
color = "red"
naughty.notify({ title = "Battery Warning",
text = "Battery low!"..spacer..cur_percent.."%"..spacer.."left!",
timeout = 5,
position = "top_right",
fg = beautiful.fg_focus,
bg = beautiful.bg_focus
})
end
elseif sta:match("charging") then
dir = "↗"
color = "green"
else
dir = ""
color = nil
end
widget_text = spacer..cur_percent.."%"..dir..spacer
if color then
batterywidget.text = '<span color="'..color..'">'..widget_text.."</span>"
else
batterywidget.text = widget_text
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment