Tidy up BatteryLevel() function.

1. Remove assert() around io.popen() as this function never fails, even
when opening a pipe to a non-existent program.
2. Remove the second assert() as well, because it is not wise to fail the whole
application just because we don't know or cannot ascertain the battery
level.
3. Guard against the failure of gasgauge-info (e.g. on emulator where it
does not exist, so, effectively "fails") by checking what we read from
the pipe and return "?" if need be.
This commit is contained in:
Tigran Aivazian
2012-09-25 11:08:06 +01:00
parent 06a0b27664
commit 864be87fc2

View File

@@ -56,9 +56,9 @@ end
function BatteryLevel()
-- NuPogodi, 18.05.12: This command seems to work even without Amazon Kindle framework
local cmd="gasgauge-info -s 2> /dev/null"
local p = assert(io.popen(cmd, "r"))
local battery = assert(p:read("*a"))
local p = io.popen("gasgauge-info -s 2> /dev/null", "r") -- io.popen() _never_ fails!
local battery = p:read("*a") or "?"
if battery == "" then battery = "?" end
p:close()
return string.gsub(battery, "[\n\r]+", "")
end