From 864be87fc29b16247cbf46bad2ed0b78d99858f9 Mon Sep 17 00:00:00 2001 From: Tigran Aivazian Date: Tue, 25 Sep 2012 11:08:06 +0100 Subject: [PATCH] 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. --- filechooser.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/filechooser.lua b/filechooser.lua index f8fffff10..5d0c1b859 100644 --- a/filechooser.lua +++ b/filechooser.lua @@ -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