2017-01-25 17:59:31 +01:00
|
|
|
-- {{{ Grab environment
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local tonumber = tonumber
|
|
|
|
local io = { popen = io.popen }
|
|
|
|
local math = { floor = math.floor }
|
|
|
|
local helpers = require("vicious.helpers")
|
|
|
|
local string = {
|
|
|
|
gmatch = string.gmatch,
|
|
|
|
match = string.match,
|
|
|
|
format = string.format
|
|
|
|
}
|
2019-03-08 08:20:27 +01:00
|
|
|
|
2017-01-25 17:59:31 +01:00
|
|
|
-- }}}
|
|
|
|
local bat_freebsd = {}
|
|
|
|
|
|
|
|
local function worker(format, warg)
|
|
|
|
local battery = warg or "batt"
|
|
|
|
local bat_info = {}
|
|
|
|
local f = io.popen("acpiconf -i " .. helpers.shellquote(battery))
|
|
|
|
for line in f:lines("*line") do
|
|
|
|
for key,value in string.gmatch(line, "(.+):%s+(.+)") do
|
|
|
|
bat_info[key] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- current state
|
2019-03-08 08:20:27 +01:00
|
|
|
-- see: https://github.com/freebsd/freebsd/blob/master/usr.sbin/acpi/acpiconf/acpiconf.c
|
2017-01-25 17:59:31 +01:00
|
|
|
local state
|
|
|
|
if bat_info["State"] == "high" then
|
|
|
|
state = "↯"
|
|
|
|
elseif bat_info["State"] == "charging" then
|
|
|
|
state = "+"
|
2018-03-12 21:16:22 +01:00
|
|
|
elseif bat_info["State"] == "critical charging" then
|
|
|
|
state = "+"
|
2017-01-25 17:59:31 +01:00
|
|
|
elseif bat_info["State"] == "discharging" then
|
|
|
|
state = "-"
|
2019-03-08 08:20:27 +01:00
|
|
|
elseif bat_info["State"] == "critical discharging" then
|
|
|
|
state = "!"
|
|
|
|
elseif bat_info["State"] == "critical" then
|
|
|
|
state = "!"
|
2017-01-25 17:59:31 +01:00
|
|
|
else
|
2019-03-08 08:20:27 +01:00
|
|
|
state = "N/A"
|
2017-01-25 17:59:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- battery capacity in percent
|
|
|
|
local percent = tonumber(string.match(bat_info["Remaining capacity"], "[%d]+"))
|
|
|
|
|
|
|
|
-- use remaining (charging or discharging) time calculated by acpiconf
|
|
|
|
local time = bat_info["Remaining time"]
|
|
|
|
if time == "unknown" then
|
|
|
|
time = "∞"
|
|
|
|
end
|
|
|
|
|
|
|
|
-- calculate wear level from (last full / design) capacity
|
|
|
|
local wear = "N/A"
|
|
|
|
if bat_info["Last full capacity"] and bat_info["Design capacity"] then
|
2017-02-23 22:36:22 +01:00
|
|
|
local l_full = tonumber(string.match(bat_info["Last full capacity"], "[%d]+"))
|
2017-01-25 17:59:31 +01:00
|
|
|
local design = tonumber(string.match(bat_info["Design capacity"], "[%d]+"))
|
2017-02-23 22:36:22 +01:00
|
|
|
wear = math.floor(l_full / design * 100)
|
2017-01-25 17:59:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- dis-/charging rate as presented by battery
|
|
|
|
local rate = string.match(bat_info["Present rate"], "([%d]+)%smW")
|
|
|
|
rate = string.format("%2.1f", tonumber(rate / 1000))
|
|
|
|
|
|
|
|
-- returns
|
|
|
|
-- * state (high "↯", discharging "-", charging "+", N/A "⌁" }
|
|
|
|
-- * remaining_capacity (percent)
|
|
|
|
-- * remaining_time, by battery
|
|
|
|
-- * wear level (percent)
|
|
|
|
-- * present_rate (mW)
|
|
|
|
return {state, percent, time, wear, rate}
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(bat_freebsd, { __call = function(_, ...) return worker(...) end })
|