Debian Codegraph @

vicious/widgets/bat_linux.lua

raw symbols SCIP
1 -- battery widget type for GNU/Linux
2 -- Copyright (C) 2010 Adrian C. <[email protected]>
3 -- Copyright (C) 2013 NormalRa <[email protected]>
4 -- Copyright (C) 2017 David Udelson <[email protected]>
5 -- Copyright (C) 2017 Roberto <[email protected]>
6 -- Copyright (C) 2017 mutlusun <[email protected]>
7 --
8 -- This file is part of Vicious.
9 --
10 -- Vicious is free software: you can redistribute it and/or modify
11 -- it under the terms of the GNU General Public License as
12 -- published by the Free Software Foundation, either version 2 of the
13 -- License, or (at your option) any later version.
14 --
15 -- Vicious is distributed in the hope that it will be useful,
16 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
17 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 -- GNU General Public License for more details.
19 --
20 -- You should have received a copy of the GNU General Public License
21 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
22
23 -- {{{ Grab environment
24 local tonumber = tonumber
25 local string = { format = string.format }
26 local math = {
27 min = math.min,
28 floor = math.floor
29 }
30
31 local helpers = require"vicious.helpers"
32 -- }}}
33
34 -- {{{ Battery widget type
35 return helpers.setcall(function (format, warg)
36 if not warg then return end
37
38 local battery = helpers.pathtotable("/sys/class/power_supply/"..warg)
39 local battery_state = {
40 ["Full\n"] = "↯",
41 ["Unknown\n"] = "⌁",
42 ["Charged\n"] = "↯",
43 ["Charging\n"] = "+",
44 ["Discharging\n"] = "-"
45 }
46
47 -- Get current power usage in watt
48 local curpower = "N/A"
49 if battery.power_now then
50 curpower = string.format("%.2f", tonumber(battery.power_now) /1000000)
51 end
52
53 -- Check if the battery is present
54 if battery.present ~= "1\n" then
55 return {battery_state["Unknown\n"], 0, "N/A", 0, curpower}
56 end
57
58 -- Get state information
59 local state = battery_state[battery.status] or battery_state["Unknown\n"]
60
61 -- Get capacity information
62 local remaining, capacity, capacity_design
63 if battery.charge_now then
64 remaining, capacity = battery.charge_now, battery.charge_full
65 capacity_design = battery.charge_full_design or capacity
66 elseif battery.energy_now then
67 remaining, capacity = battery.energy_now, battery.energy_full
68 capacity_design = battery.energy_full_design or capacity
69 else
70 return {battery_state["Unknown\n"], 0, "N/A", 0, curpower}
71 end
72
73 -- Calculate capacity and wear percentage (but work around broken BAT/ACPI implementations)
74 local percent = math.min(math.floor(remaining / capacity * 100), 100)
75 local wear = math.floor(100 - capacity / capacity_design * 100)
76
77 -- Get charge information
78 local rate
79 if battery.current_now then
80 rate = tonumber(battery.current_now)
81 elseif battery.power_now then
82 rate = tonumber(battery.power_now)
83 else
84 return {state, percent, "N/A", wear, curpower}
85 end
86
87 -- Calculate remaining (charging or discharging) time
88 local time = "N/A"
89
90 if rate ~= nil and rate ~= 0 then
91 local timeleft
92 if state == "+" then
93 timeleft = (tonumber(capacity)-tonumber(remaining)) / tonumber(rate)
94 elseif state == "-" then
95 timeleft = tonumber(remaining) / tonumber(rate)
96 else
97 return {state, percent, time, wear, curpower}
98 end
99
100 -- Calculate time
101 local hoursleft = math.floor(timeleft)
102 local minutesleft = math.floor((timeleft - hoursleft) * 60 )
103
104 time = string.format("%02d:%02d", hoursleft, minutesleft)
105 end
106
107 return {state, percent, time, wear, curpower}
108 end)
109 -- }}}