@@ -26,8 +26,15 @@ local round = function (num, idp)
26
26
end
27
27
28
28
local acpi_is_on_ac_power = function (adapter )
29
- local f = io.open (' /sys/class/power_supply/' .. adapter .. ' /online' ):read ()
30
- return string.find (f , ' 1' )
29
+ local f = io.open (' /sys/class/power_supply/' .. adapter .. ' /online' )
30
+ if f == nil then return false end
31
+ return string.find (f :read (), ' 1' )
32
+ end
33
+
34
+ local acpi_battery_is_present = function (battery )
35
+ local f = io.open (' /sys/class/power_supply/' .. battery .. ' /present' )
36
+ if f == nil then return false end
37
+ return string.find (f :read (), ' 1' )
31
38
end
32
39
33
40
local acpi_battery_is_charging = function (battery )
@@ -37,11 +44,12 @@ local acpi_battery_is_charging = function (battery)
37
44
end
38
45
39
46
local acpi_battery_percent = function (battery )
40
- local f = io.open (' /sys/class/power_supply/' .. battery .. ' /energy_now' )
41
- if f == nil then return 0 end
42
- local now = tonumber (f :read ())
43
- local full = tonumber (io.open (' /sys/class/power_supply/' .. battery .. ' /energy_full' ):read ())
44
- return now / full
47
+ local now = io.open (' /sys/class/power_supply/' .. battery .. ' /energy_now' ) or
48
+ io.open (' /sys/class/power_supply/' .. battery .. ' /charge_now' )
49
+ local full = io.open (' /sys/class/power_supply/' .. battery .. ' /energy_full' ) or
50
+ io.open (' /sys/class/power_supply/' .. battery .. ' /charge_full' )
51
+ if (now == nil ) or (full == nil ) then return 0 end
52
+ return tonumber (now :read ())/ tonumber (full :read ())
45
53
end
46
54
47
55
local battery_bolt_generate = function (width , height )
@@ -156,10 +164,12 @@ function assault.draw (assault, wibox, cr, width, height)
156
164
local percent = acpi_battery_percent (data [assault ].battery )
157
165
158
166
local draw_color = color (data [assault ].normal_color )
159
- if acpi_battery_is_charging (data [assault ].battery ) then
160
- draw_color = color (data [assault ].charging_color )
161
- elseif percent <= data [assault ].critical_level then
162
- draw_color = color (data [assault ].critical_color )
167
+ if acpi_battery_is_present (data [assault ].battery ) then
168
+ if acpi_battery_is_charging (data [assault ].battery ) then
169
+ draw_color = color (data [assault ].charging_color )
170
+ elseif percent <= data [assault ].critical_level then
171
+ draw_color = color (data [assault ].critical_color )
172
+ end
163
173
end
164
174
165
175
-- Draw fill
@@ -172,7 +182,7 @@ function assault.draw (assault, wibox, cr, width, height)
172
182
cr :translate ( bolt_x , bolt_y )
173
183
cr :append_path (battery_bolt_generate (data [assault ].bolt_width , data [assault ].bolt_height ))
174
184
cr :translate (- bolt_x , - bolt_y )
175
- else
185
+ elseif acpi_battery_is_present ( data [ assault ]. battery ) then
176
186
local percentstr = string.format (' %d%%' , round (percent * 100 ))
177
187
battery_text_draw (cr , data [assault ], percentstr )
178
188
end
@@ -244,7 +254,7 @@ function assault.new (args)
244
254
end
245
255
246
256
function assault .mt :__call (...)
247
- return assault .new (... )
257
+ return assault .new (... )
248
258
end
249
259
250
260
return setmetatable (assault , assault .mt )
0 commit comments