forked from ashpie/simple-awesome
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
b39eac91a5 | |||
2d79eb8a5e | |||
ecbf661115 | |||
00533c13f6 | |||
e36d42dfeb |
@ -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
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -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,8 +139,62 @@ gears.timer {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
--- Batteries
|
||||||
|
---
|
||||||
|
rawBatteries = {}
|
||||||
|
function refreshRawBatteries()
|
||||||
|
rawBatteries = readCommand("ls -A1 /sys/class/power_supply")
|
||||||
|
end
|
||||||
|
|
||||||
local battery = wibox.widget {
|
function getBatteryLevel(battery)
|
||||||
|
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"
|
||||||
|
end
|
||||||
|
return string.format("%." .. config.battery.precision .. "f", (charge / capacity) * 100) .. "%"
|
||||||
|
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 = "-- %",
|
markup = "-- %",
|
||||||
align = "center",
|
align = "center",
|
||||||
@ -143,60 +205,40 @@ local battery = wibox.widget {
|
|||||||
widget = wibox.container.margin,
|
widget = wibox.container.margin,
|
||||||
}
|
}
|
||||||
|
|
||||||
local batteryText = battery:get_children()[1]
|
-- tooltip
|
||||||
|
local name = readFile(path .. "/model_name")
|
||||||
|
local tooltip = awful.tooltip {
|
||||||
|
text = name
|
||||||
|
}
|
||||||
|
tooltip:add_to_object(widget)
|
||||||
|
|
||||||
local rawBatteries = readCommand("ls -A1 /sys/class/power_supply")
|
-- update battery level
|
||||||
|
local batteryText = widget:get_children()[1]
|
||||||
function getBatteries()
|
local timer = gears.timer {
|
||||||
return rawBatteries:gmatch("([^\n]+)")
|
|
||||||
end
|
|
||||||
|
|
||||||
function hasBattery()
|
|
||||||
return rawBatteries:len() > 0
|
|
||||||
end
|
|
||||||
|
|
||||||
function getBatteryCapacity()
|
|
||||||
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
|
|
||||||
end
|
|
||||||
return charge / capacity
|
|
||||||
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
|
|
||||||
end
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
if hasBattery() then
|
|
||||||
gears.timer {
|
|
||||||
timeout = config.battery.refresh_interval,
|
timeout = config.battery.refresh_interval,
|
||||||
call_now = true,
|
call_now = true,
|
||||||
autostart = true,
|
autostart = true,
|
||||||
callback = function()
|
callback = function()
|
||||||
batteryText.markup = string.format("%." .. config.battery.precision .. "f", getBatteryCapacity() * 100) .. "%"
|
batteryText.markup = (isCharging(path) and "+" or "") .. getBatteryLevel(path)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
else
|
table.insert(timers, timer)
|
||||||
battery = nil
|
|
||||||
|
batteryContainer:add(widget)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
gears.timer {
|
||||||
|
timeout = config.battery.list_refresh_interval,
|
||||||
|
call_now = true,
|
||||||
|
autostart = true,
|
||||||
|
callback = function()
|
||||||
|
refreshBatteries()
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
bars = bars,
|
bars = bars,
|
||||||
battery = battery,
|
battery = batteryContainer,
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user