From 7a3699cf4c1102cddb7ace1c089364634c25a227 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Thu, 9 Feb 2012 12:43:22 +0100 Subject: division by zero, if battery is full charged If the battery state change from charging to full, power_now is reseted to zero for a little time. This cause division by zero, which was visible as a very big negative number because of the behaviour of string.format. Signed-off-by: Adrian C. (anrxc) --- widgets/bat.lua | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'widgets') diff --git a/widgets/bat.lua b/widgets/bat.lua index cf953a9..25a3aac 100644 --- a/widgets/bat.lua +++ b/widgets/bat.lua @@ -10,7 +10,6 @@ local string = { format = string.format } local helpers = require("vicious.helpers") local math = { min = math.min, - max = math.max, floor = math.floor } -- }}} @@ -57,9 +56,9 @@ local function worker(format, warg) -- Get charge information if battery.current_now then - rate = battery.current_now + rate = tonumber(battery.current_now) elseif battery.power_now then - rate = battery.power_now + rate = tonumber(battery.power_now) else return {state, percent, "N/A"} end @@ -67,7 +66,7 @@ local function worker(format, warg) -- Calculate remaining (charging or discharging) time local time = "N/A" - if tonumber(rate) then + if rate ~= nil and rate ~= 0 then if state == "+" then timeleft = (tonumber(capacity) - tonumber(remaining)) / tonumber(rate) elseif state == "-" then @@ -76,9 +75,9 @@ local function worker(format, warg) return {state, percent, time} end - -- Calculate time (but work around broken BAT/ACPI implementations) - local hoursleft = math.max(math.floor(timeleft), 0) - local minutesleft = math.max(math.floor((timeleft - hoursleft) * 60 ), 0) + -- Calculate time + local hoursleft = math.floor(timeleft) + local minutesleft = math.floor((timeleft - hoursleft) * 60 ) time = string.format("%02d:%02d", hoursleft, minutesleft) end -- cgit v1.2.3