Compare commits
No commits in common. "develop" and "develop" have entirely different histories.
@ -171,8 +171,7 @@ config.widgets = {
|
||||
battery = {
|
||||
enabled = true,
|
||||
screens = { 1, 2 },
|
||||
refresh_interval = 15, -- In seconds
|
||||
list_refresh_interval = 2, -- In seconds
|
||||
refresh_interval = 30, -- In seconds
|
||||
precision = 0, -- How many decimals
|
||||
},
|
||||
},
|
||||
|
@ -1,4 +1,3 @@
|
||||
local awful = require("awful")
|
||||
local beautiful = require("beautiful")
|
||||
local gears = require("gears")
|
||||
local naughty = require("naughty")
|
||||
@ -51,13 +50,6 @@ 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()
|
||||
@ -139,106 +131,72 @@ gears.timer {
|
||||
|
||||
|
||||
|
||||
---
|
||||
--- Batteries
|
||||
---
|
||||
rawBatteries = {}
|
||||
function refreshRawBatteries()
|
||||
rawBatteries = readCommand("ls -A1 /sys/class/power_supply")
|
||||
|
||||
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]+)")
|
||||
end
|
||||
|
||||
function getBatteryLevel(battery)
|
||||
function hasBattery()
|
||||
return rawBatteries:len() > 0
|
||||
end
|
||||
|
||||
function getBatteryCapacity()
|
||||
local charge = 0
|
||||
local capacity = 0
|
||||
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"
|
||||
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
|
||||
end
|
||||
return string.format("%." .. config.battery.precision .. "f", (charge / capacity) * 100) .. "%"
|
||||
return charge / capacity
|
||||
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()
|
||||
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
|
||||
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 = batteryContainer,
|
||||
}
|
||||
battery = battery,
|
||||
}
|
Loading…
Reference in New Issue
Block a user