mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user