| 1 |
|
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
local tonumber = tonumber |
| 22 |
local io = { open = io.open } |
| 23 |
local setmetatable = setmetatable |
| 24 |
local math = { |
| 25 |
min = math.min, |
| 26 |
floor = math.floor |
| 27 |
} |
| 28 |
local string = { |
| 29 |
find = string.find, |
| 30 |
match = string.match, |
| 31 |
format = string.format |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
local batproc_linux = {} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
local function worker(format, batid) |
| 43 |
local battery_state = { |
| 44 |
["full"] = "↯", |
| 45 |
["unknown"] = "⌁", |
| 46 |
["charged"] = "↯", |
| 47 |
["charging"] = "+", |
| 48 |
["discharging"] = "-" |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
local f = io.open("/proc/acpi/battery/"..batid.."/info") |
| 53 |
|
| 54 |
if not f then return {battery_state["unknown"], 0, "N/A"} end |
| 55 |
local infofile = f:read("*all") |
| 56 |
f:close() |
| 57 |
|
| 58 |
|
| 59 |
if infofile == nil or string.find(infofile, "present:[%s]+no") then |
| 60 |
return {battery_state["unknown"], 0, "N/A"} |
| 61 |
end |
| 62 |
|
| 63 |
|
| 64 |
local capacity = string.match(infofile, "last full capacity:[%s]+([%d]+).*") |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
local f = io.open("/proc/acpi/battery/"..batid.."/state") |
| 69 |
local statefile = f:read("*all") |
| 70 |
f:close() |
| 71 |
|
| 72 |
|
| 73 |
local state = string.match(statefile, "charging state:[%s]+([%a]+).*") |
| 74 |
local state = battery_state[state] or battery_state["unknown"] |
| 75 |
|
| 76 |
|
| 77 |
local rate = string.match(statefile, "present rate:[%s]+([%d]+).*") |
| 78 |
local remaining = string.match(statefile, "remaining capacity:[%s]+([%d]+).*") |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
local percent = math.min(math.floor(remaining / capacity * 100), 100) |
| 83 |
|
| 84 |
|
| 85 |
if state == "+" then |
| 86 |
timeleft = (tonumber(capacity) - tonumber(remaining)) / tonumber(rate) |
| 87 |
elseif state == "-" then |
| 88 |
timeleft = tonumber(remaining) / tonumber(rate) |
| 89 |
else |
| 90 |
return {state, percent, "N/A"} |
| 91 |
end |
| 92 |
local hoursleft = math.floor(timeleft) |
| 93 |
local minutesleft = math.floor((timeleft - hoursleft) * 60 ) |
| 94 |
local time = string.format("%02d:%02d", hoursleft, minutesleft) |
| 95 |
|
| 96 |
return {state, percent, time} |
| 97 |
end |
| 98 |
|
| 99 |
|
| 100 |
return setmetatable(batproc_linux, { __call = function(_, ...) return worker(...) end }) |