Compare commits

...

5 Commits

2 changed files with 102 additions and 59 deletions

View File

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

View File

@ -1,3 +1,4 @@
local awful = require("awful")
local beautiful = require("beautiful") local beautiful = require("beautiful")
local gears = require("gears") local gears = require("gears")
local naughty = require("naughty") local naughty = require("naughty")
@ -50,6 +51,13 @@ function readCommand(command)
return r return r
end 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] local cpuBar = bars:get_children()[1]:get_children()[1]:get_children()[1]
function getCpuData() function getCpuData()
@ -131,72 +139,106 @@ gears.timer {
---
local battery = wibox.widget { --- Batteries
{ ---
markup = "-- %", rawBatteries = {}
align = "center", function refreshRawBatteries()
valign = "center", rawBatteries = readCommand("ls -A1 /sys/class/power_supply")
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]+)")
end end
function hasBattery() function getBatteryLevel(battery)
return rawBatteries:len() > 0
end
function getBatteryCapacity()
local charge = 0 local charge = 0
local capacity = 0 local capacity = 0
for b in getBatteries() do if fileExists(battery .. "/energy_now") then
if b:sub(1,3) ~= 'hid' and readCommand("cat /sys/class/power_supply/" .. b .. "/type"):match("Battery") then charge = charge + readFile(battery .. "/energy_now")
if fileExists("/sys/class/power_supply/" .. b .. "/energy_now") then capacity = capacity + readFile(battery .. "/energy_full")
charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_now") elseif fileExists(battery .. "/charge_now") then
capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_full") charge = charge + readFile(battery .. "/charge_now")
else capacity = capacity + readFile(battery .. "/charge_full")
charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/charge_now") elseif fileExists(battery .. "/capacity_level") then
capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/charge_full") return readFile(battery .. "/capacity_level")
end else
end return "Unsupported"
end end
return charge / capacity return string.format("%." .. config.battery.precision .. "f", (charge / capacity) * 100) .. "%"
end end
function isCharging() function isCharging(battery)
for b in getBatteries() do return readFile(battery .. "/status") == "Charging\n"
if readCommand("cat /sys/class/power_supply/" .. b .. "/type"):match("Battery") then end
return readCommand("cat /sys/class/power_supply/" .. b .. "/status") ~= "Discharging"
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 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 { return {
bars = bars, bars = bars,
battery = battery, battery = batteryContainer,
} }