Debian Codegraph @

vicious/widgets/bat_freebsd.lua

raw symbols SCIP
1 -- battery widget type for FreeBSD
2 -- Copyright (C) 2017-2019 mutlusun <[email protected]>
3 -- Copyright (C) 2019 Nguyễn Gia Phong <[email protected]m>
4 --
5 -- This file is part of Vicious.
6 --
7 -- Vicious is free software: you can redistribute it and/or modify
8 -- it under the terms of the GNU General Public License as
9 -- published by the Free Software Foundation, either version 2 of the
10 -- License, or (at your option) any later version.
11 --
12 -- Vicious is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 -- GNU General Public License for more details.
16 --
17 -- You should have received a copy of the GNU General Public License
18 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
19
20 -- {{{ Grab environment
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 -- Battery: provides battery level of requested battery
33 -- vicious.widgets.battery_freebsd
34 local bat_freebsd = {}
35
36 -- {{{ Battery widget type
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 -- current state
46 -- see: https://github.com/freebsd/freebsd/blob/master/usr.sbin/acpi/acpiconf/acpiconf.c
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 -- battery capacity in percent
58 local percent = tonumber(bat_info["Remaining capacity"]:match"[%d]+")
59
60 -- use remaining (charging or discharging) time calculated by acpiconf
61 local time = bat_info["Remaining time"]
62 if time == "unknown" then
63 time = "∞"
64 end
65
66 -- calculate wear level from (last full / design) capacity
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 -- dis-/charging rate as presented by battery
75 local rate = bat_info["Present rate"]:match"([%d]+)%smW"
76 rate = string.format("%2.1f", tonumber(rate / 1000))
77
78 -- returns
79 -- * state (high "↯", discharging "-", charging "+", N/A "⌁" }
80 -- * remaining_capacity (percent)
81 -- * remaining_time, by battery
82 -- * wear level (percent)
83 -- * present_rate (mW)
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)