| 1 |
|
| 2 |
|
| 3 |
m> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
local tonumber = tonumber |
| 22 |
local math = { floor = math.floor } |
| 23 |
local string = { |
| 24 |
gmatch = string.gmatch, |
| 25 |
format = string.format |
| 26 |
} |
| 27 |
|
| 28 |
local helpers = require("vicious.helpers") |
| 29 |
local spawn = require("vicious.spawn") |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
local bat_freebsd = {} |
| 35 |
|
| 36 |
|
| 37 |
local function parse(stdout, stderr, exitreason, exitcode) |
| 38 |
local bat_info = {} |
| 39 |
for line in string.gmatch(stdout, "[^\n]+") do |
| 40 |
for key,value in string.gmatch(line, "(.+):%s+(.+)") do |
| 41 |
bat_info[key] = value |
| 42 |
end |
| 43 |
end |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
local battery_state = { |
| 48 |
["high"] = "↯", |
| 49 |
["charging"] = "+", |
| 50 |
["critical charging"] = "+", |
| 51 |
["discharging"] = "-", |
| 52 |
["critical discharging"] = "!", |
| 53 |
["critical"] = "!", |
| 54 |
} |
| 55 |
local state = battery_state[bat_info["State"]] or "N/A" |
| 56 |
|
| 57 |
|
| 58 |
local percent = tonumber(bat_info["Remaining capacity"]:match"[%d]+") |
| 59 |
|
| 60 |
|
| 61 |
local time = bat_info["Remaining time"] |
| 62 |
if time == "unknown" then |
| 63 |
time = "∞" |
| 64 |
end |
| 65 |
|
| 66 |
|
| 67 |
local wear = "N/A" |
| 68 |
if bat_info["Last full capacity"] and bat_info["Design capacity"] then |
| 69 |
local l_full = tonumber(bat_info["Last full capacity"]:match"[%d]+") |
| 70 |
local design = tonumber(bat_info["Design capacity"]:match"[%d]+") |
| 71 |
wear = math.floor(l_full / design * 100) |
| 72 |
end |
| 73 |
|
| 74 |
|
| 75 |
local rate = bat_info["Present rate"]:match"([%d]+)%smW" |
| 76 |
rate = string.format("%2.1f", tonumber(rate / 1000)) |
| 77 |
|
| 78 |
|
| 79 |
⌁" } |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
return {state, percent, time, wear, rate} |
| 85 |
end |
| 86 |
|
| 87 |
function bat_freebsd.async(format, warg, callback) |
| 88 |
local battery = warg or "batt" |
| 89 |
spawn.easy_async("acpiconf -i " .. helpers.shellquote(battery), |
| 90 |
function (...) callback(parse(...)) end) |
| 91 |
end |
| 92 |
|
| 93 |
|
| 94 |
return helpers.setasyncall(bat_freebsd) |