2017-01-25 17:59:31 +01:00
|
|
|
-- {{{ Grab environment
|
|
|
|
local tonumber = tonumber
|
|
|
|
local math = { floor = math.floor }
|
|
|
|
local string = {
|
|
|
|
gmatch = string.gmatch,
|
|
|
|
format = string.format
|
|
|
|
}
|
2019-08-23 17:50:13 +02:00
|
|
|
|
|
|
|
local helpers = require("vicious.helpers")
|
|
|
|
local spawn = require("vicious.spawn")
|
2017-01-25 17:59:31 +01:00
|
|
|
-- }}}
|
2019-05-26 19:17:21 +02:00
|
|
|
|
|
|
|
-- Battery: provides battery level of requested battery
|
|
|
|
-- vicious.widgets.battery_freebsd
|
2017-01-25 17:59:31 +01:00
|
|
|
local bat_freebsd = {}
|
|
|
|
|
2019-05-26 19:17:21 +02:00
|
|
|
-- {{{ Battery widget type
|
|
|
|
local function parse(stdout, stderr, exitreason, exitcode)
|
2017-01-25 17:59:31 +01:00
|
|
|
local bat_info = {}
|
2019-05-27 09:42:22 +02:00
|
|
|
for line in string.gmatch(stdout, "[^\n]+") do
|
2017-01-25 17:59:31 +01:00
|
|
|
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
|
2019-05-26 19:17:21 +02:00
|
|
|
local battery_state = {
|
|
|
|
["high"] = "↯",
|
|
|
|
["charging"] = "+",
|
|
|
|
["critical charging"] = "+",
|
|
|
|
["discharging"] = "-",
|
|
|
|
["critical discharging"] = "!",
|
|
|
|
["critical"] = "!",
|
|
|
|
}
|
|
|
|
local state = battery_state[bat_info["State"]] or "N/A"
|
2017-01-25 17:59:31 +01:00
|
|
|
|
|
|
|
-- battery capacity in percent
|
2019-08-23 17:50:13 +02:00
|
|
|
local percent = tonumber(bat_info["Remaining capacity"]:match"[%d]+")
|
2017-01-25 17:59:31 +01:00
|
|
|
|
|
|
|
-- 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
|
2019-08-23 17:50:13 +02:00
|
|
|
local l_full = tonumber(bat_info["Last full capacity"]:match"[%d]+")
|
|
|
|
local design = tonumber(bat_info["Design capacity"]:match"[%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
|
2019-08-23 17:50:13 +02:00
|
|
|
local rate = bat_info["Present rate"]:match"([%d]+)%smW"
|
2017-01-25 17:59:31 +01:00
|
|
|
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
|
|
|
|
|
2019-05-27 09:42:22 +02:00
|
|
|
function bat_freebsd.async(format, warg, callback)
|
2019-05-26 19:17:21 +02:00
|
|
|
local battery = warg or "batt"
|
|
|
|
spawn.easy_async("acpiconf -i " .. helpers.shellquote(battery),
|
|
|
|
function (...) callback(parse(...)) end)
|
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
2019-05-27 09:42:22 +02:00
|
|
|
return helpers.setasyncall(bat_freebsd)
|