port bat widget to freebsd

This commit is contained in:
mutlusun 2017-01-25 17:59:31 +01:00 committed by Jörg Thalheim
parent 78e1242601
commit 5628c9e478
No known key found for this signature in database
GPG Key ID: CA4106B8D7CC79FA
2 changed files with 71 additions and 2 deletions

69
widgets/bat_freebsd.lua Normal file
View File

@ -0,0 +1,69 @@
-- {{{ 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
}
-- }}}
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
f:close()
-- current state
local state
if bat_info["State"] == "high" then
state = ""
elseif bat_info["State"] == "charging" then
state = "+"
elseif bat_info["State"] == "discharging" then
state = "-"
else
state = ""
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
local l_full = tonumber(string.match(bat_info["Last full capacity"], "[%d]+"))
local design = tonumber(string.match(bat_info["Design capacity"], "[%d]+"))
wear = math.floor(100 - (l_full / design * 100))
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 })

View File

@ -18,7 +18,7 @@ local math = {
-- Bat: provides state, charge, remaining time, and wear for a requested battery -- Bat: provides state, charge, remaining time, and wear for a requested battery
-- vicious.widgets.bat -- vicious.widgets.bat
local bat = {} local bat_linux = {}
-- {{{ Battery widget type -- {{{ Battery widget type
@ -91,4 +91,4 @@ local function worker(format, warg)
end end
-- }}} -- }}}
return setmetatable(bat, { __call = function(_, ...) return worker(...) end }) return setmetatable(bat_linux, { __call = function(_, ...) return worker(...) end })