2016-08-22 13:58:25 +02:00
|
|
|
-- This function returns a formatted string with the current battery status. It
|
|
|
|
-- can be used to populate a text widget in the awesome window manager. Based
|
|
|
|
-- on the "Gigamo Battery Widget" found in the wiki at awesome.naquadah.org
|
|
|
|
|
|
|
|
local wibox = require("wibox")
|
|
|
|
local naughty = require("naughty")
|
|
|
|
local beautiful = require("beautiful")
|
|
|
|
|
|
|
|
local previous_percent = 0
|
|
|
|
|
2016-09-10 14:03:11 +02:00
|
|
|
function batteryInfo()
|
2017-01-20 09:50:08 +01:00
|
|
|
local fd = io.popen('acpi | head -n 1 | cut -d ":" -f 2 | cut -d "," -f 2 | tr -d "%"')
|
2016-09-10 14:03:11 +02:00
|
|
|
local percentage = fd:read('*all')
|
|
|
|
fd:close()
|
|
|
|
return tonumber(percentage)
|
2016-08-22 13:58:25 +02:00
|
|
|
end
|
|
|
|
|
2016-09-10 14:03:11 +02:00
|
|
|
function isCharging()
|
2017-01-20 09:50:08 +01:00
|
|
|
local _,_,error = os.execute('acpi | head -n 1 | grep Discharging')
|
2016-09-10 14:03:11 +02:00
|
|
|
return error ~= 0
|
2016-08-22 13:58:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function update_battery()
|
|
|
|
|
2016-09-10 14:03:11 +02:00
|
|
|
local percent = batteryInfo()
|
2016-08-22 13:58:25 +02:00
|
|
|
local color
|
|
|
|
local symbol
|
|
|
|
|
|
|
|
if percent < 15 then
|
|
|
|
color="red"
|
|
|
|
elseif percent < 30 then
|
|
|
|
color="orange"
|
|
|
|
elseif percent > 90 then
|
|
|
|
color="green"
|
|
|
|
else
|
|
|
|
color="white"
|
|
|
|
end
|
|
|
|
|
2016-09-10 14:03:11 +02:00
|
|
|
if isCharging() then
|
2016-08-22 13:58:25 +02:00
|
|
|
color = 'green'
|
|
|
|
symbol = '⚡'
|
|
|
|
else
|
|
|
|
symbol = '%'
|
|
|
|
end
|
|
|
|
|
|
|
|
if previous_percent >= 15 and percent < 15 then
|
|
|
|
naughty.notify({
|
|
|
|
title = "Low battery...",
|
|
|
|
text = "Battery level is lower than 15% !",
|
|
|
|
fg="#000000",
|
|
|
|
bg="#ff0000",
|
|
|
|
timeout=5
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
battery_widget:set_markup('<span color="' .. color .. '">' .. percent .. ' ' .. symbol .. '</span>')
|
|
|
|
|
|
|
|
previous_percent = percent
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
battery_widget = wibox.widget.textbox()
|
|
|
|
battery_widget.timer = timer({timeout=5})
|
|
|
|
battery_widget.timer:connect_signal("timeout", update_battery)
|
|
|
|
battery_widget.timer:start()
|
|
|
|
update_battery()
|