Skip to content

Commit 2ac2cba

Browse files
committed
Merge pull request NuckChorris#13 from LoathingKernel/battery_path
awesomewm: Setting to specify the prefix of the battery current and full charge path
2 parents c4d2757 + 0bab0e9 commit 2ac2cba

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

awesomewm/assault.lua

+23-13
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@ local round = function (num, idp)
2626
end
2727

2828
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')
3138
end
3239

3340
local acpi_battery_is_charging = function (battery)
@@ -37,11 +44,12 @@ local acpi_battery_is_charging = function (battery)
3744
end
3845

3946
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())
4553
end
4654

4755
local battery_bolt_generate = function (width, height)
@@ -156,10 +164,12 @@ function assault.draw (assault, wibox, cr, width, height)
156164
local percent = acpi_battery_percent(data[assault].battery)
157165

158166
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
163173
end
164174

165175
-- Draw fill
@@ -172,7 +182,7 @@ function assault.draw (assault, wibox, cr, width, height)
172182
cr:translate( bolt_x, bolt_y)
173183
cr:append_path(battery_bolt_generate(data[assault].bolt_width, data[assault].bolt_height))
174184
cr:translate(-bolt_x, -bolt_y)
175-
else
185+
elseif acpi_battery_is_present(data[assault].battery) then
176186
local percentstr = string.format('%d%%', round(percent * 100))
177187
battery_text_draw(cr, data[assault], percentstr)
178188
end
@@ -244,7 +254,7 @@ function assault.new (args)
244254
end
245255

246256
function assault.mt:__call(...)
247-
return assault.new(...)
257+
return assault.new(...)
248258
end
249259

250260
return setmetatable(assault, assault.mt)

0 commit comments

Comments
 (0)