Compare commits

...

5 Commits

2 changed files with 102 additions and 59 deletions

View File

@ -171,7 +171,8 @@ config.widgets = {
battery = {
enabled = true,
screens = { 1, 2 },
refresh_interval = 30, -- In seconds
refresh_interval = 15, -- In seconds
list_refresh_interval = 2, -- In seconds
precision = 0, -- How many decimals
},
},

View File

@ -1,3 +1,4 @@
local awful = require("awful")
local beautiful = require("beautiful")
local gears = require("gears")
local naughty = require("naughty")
@ -50,6 +51,13 @@ function readCommand(command)
return r
end
function readFile(file)
local handle = io.open(file, "r")
local r = handle:read("*a")
handle:close()
return r
end
local cpuBar = bars:get_children()[1]:get_children()[1]:get_children()[1]
function getCpuData()
@ -131,72 +139,106 @@ gears.timer {
local battery = wibox.widget {
{
markup = "-- %",
align = "center",
valign = "center",
widget = wibox.widget.textbox,
},
margins = beautiful.system_resources_widget_battery_margin,
widget = wibox.container.margin,
}
local batteryText = battery:get_children()[1]
local rawBatteries = readCommand("ls -A1 /sys/class/power_supply")
function getBatteries()
return rawBatteries:gmatch("([^\n]+)")
---
--- Batteries
---
rawBatteries = {}
function refreshRawBatteries()
rawBatteries = readCommand("ls -A1 /sys/class/power_supply")
end
function hasBattery()
return rawBatteries:len() > 0
end
function getBatteryCapacity()
function getBatteryLevel(battery)
local charge = 0
local capacity = 0
for b in getBatteries() do
if b:sub(1,3) ~= 'hid' and readCommand("cat /sys/class/power_supply/" .. b .. "/type"):match("Battery") then
if fileExists("/sys/class/power_supply/" .. b .. "/energy_now") then
charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_now")
capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_full")
else
charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/charge_now")
capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/charge_full")
end
end
if fileExists(battery .. "/energy_now") then
charge = charge + readFile(battery .. "/energy_now")
capacity = capacity + readFile(battery .. "/energy_full")
elseif fileExists(battery .. "/charge_now") then
charge = charge + readFile(battery .. "/charge_now")
capacity = capacity + readFile(battery .. "/charge_full")
elseif fileExists(battery .. "/capacity_level") then
return readFile(battery .. "/capacity_level")
else
return "Unsupported"
end
return charge / capacity
return string.format("%." .. config.battery.precision .. "f", (charge / capacity) * 100) .. "%"
end
function isCharging()
for b in getBatteries() do
if readCommand("cat /sys/class/power_supply/" .. b .. "/type"):match("Battery") then
return readCommand("cat /sys/class/power_supply/" .. b .. "/status") ~= "Discharging"
end
function isCharging(battery)
return readFile(battery .. "/status") == "Charging\n"
end
local batteryContainer = wibox.widget {
layout = wibox.layout.fixed.horizontal
}
local timers = {}
function refreshBatteries()
-- refresh raw batteries and check for change
local oldBatteries = rawBatteries
refreshRawBatteries()
if rawBatteries == oldBatteries then
return
end
-- clear timers
for i,v in ipairs(timers) do
v:stop()
end
timers = {};
-- clear layout
batteryContainer:set_children({})
for battery in rawBatteries:gmatch("([^\n]+)") do
local path = "/sys/class/power_supply/" .. battery
if readFile(path .. "/type"):match("Battery") then
-- widget
local widget = wibox.widget {
{
markup = "-- %",
align = "center",
valign = "center",
widget = wibox.widget.textbox,
},
margins = beautiful.system_resources_widget_battery_margin,
widget = wibox.container.margin,
}
-- tooltip
local name = readFile(path .. "/model_name")
local tooltip = awful.tooltip {
text = name
}
tooltip:add_to_object(widget)
-- update battery level
local batteryText = widget:get_children()[1]
local timer = gears.timer {
timeout = config.battery.refresh_interval,
call_now = true,
autostart = true,
callback = function()
batteryText.markup = (isCharging(path) and "+" or "") .. getBatteryLevel(path)
end
}
table.insert(timers, timer)
batteryContainer:add(widget)
end
end
end
gears.timer {
timeout = config.battery.list_refresh_interval,
call_now = true,
autostart = true,
callback = function()
refreshBatteries()
end
return nil
end
if hasBattery() then
gears.timer {
timeout = config.battery.refresh_interval,
call_now = true,
autostart = true,
callback = function()
batteryText.markup = string.format("%." .. config.battery.precision .. "f", getBatteryCapacity() * 100) .. "%"
end
}
else
battery = nil
end
}
return {
bars = bars,
battery = battery,
battery = batteryContainer,
}