From 0d73f6d8ae32f1cd48ce9f089b902eb0877605e1 Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Mon, 26 Oct 2009 20:32:48 +0100 Subject: Ensure returned numbers are of type number Thanks to Felix for bringing this to my attention. Obviously there was already a safety net for feeding progressbars and graphs... and while this makes for a good coding practice it's not a big deal. We have widgets of type textbox for one, and a lot of string concatenation happens. Strings are formatted, markup is applied... --- bat.lua | 4 ++-- batat.lua | 5 +++-- cpufreq.lua | 3 ++- cpuinf.lua | 8 ++++---- entropy.lua | 3 ++- fs.lua | 9 +++++---- gmail.lua | 5 +++-- hddtemp.lua | 3 ++- load.lua | 3 ++- thermal.lua | 5 +++-- volume.lua | 5 +++-- wifi.lua | 9 +++++---- 12 files changed, 36 insertions(+), 26 deletions(-) diff --git a/bat.lua b/bat.lua index 984ed1d..0e09717 100644 --- a/bat.lua +++ b/bat.lua @@ -36,13 +36,13 @@ local function worker(format, batid) -- Get /proc/acpi/battery info local f = io.open("/proc/acpi/battery/"..batid.."/info") -- Handler for incompetent users - if not f then return {battery_state["unknown"], "0", "N/A"} end + if not f then return {battery_state["unknown"], 0, "N/A"} end local infofile = f:read("*all") f:close() -- Check if the battery is present if infofile == nil or string.find(infofile, "present:[%s]+no") then - return {battery_state["unknown"], "0", "N/A"} + return {battery_state["unknown"], 0, "N/A"} end -- Get capacity information diff --git a/batat.lua b/batat.lua index 860eb26..bf84101 100644 --- a/batat.lua +++ b/batat.lua @@ -4,6 +4,7 @@ --------------------------------------------------- -- {{{ Grab environment +local tonumber = tonumber local io = { popen = io.popen } local setmetatable = setmetatable local table = { insert = table.insert } @@ -34,11 +35,11 @@ local function worker(format) if string.match(line, "^[%s]+Battery.*") then -- Store state and charge information table.insert(battery_info, (battery_state[string.match(line, "([%a]*),") or "unknown"])) - table.insert(battery_info, (string.match(line, "([%d]?[%d]?[%d])%.") or "0")) + table.insert(battery_info, (tonumber(string.match(line, "([%d]?[%d]?[%d])%.")) or 0)) -- Store remaining time information table.insert(battery_info, (string.match(line, "%%,%s(.*)") or "N/A")) else - return {battery_state["unknown"], "0", "N/A"} + return {battery_state["unknown"], 0, "N/A"} end end f:close() diff --git a/cpufreq.lua b/cpufreq.lua index 0acc304..436e48b 100644 --- a/cpufreq.lua +++ b/cpufreq.lua @@ -4,6 +4,7 @@ --------------------------------------------------- -- {{{ Grab environment +local tonumber = tonumber local io = { open = io.open } local setmetatable = setmetatable local string = { @@ -45,7 +46,7 @@ local function worker(format, cpuid) local f = io.open("/sys/devices/system/cpu/"..cpuid.."/cpufreq/scaling_voltages") if f then for line in f:lines() do if string.find(line, "^"..freq) then - voltage.mv = string.match(line, "[%d]+[%s]([%d]+)") + voltage.mv = tonumber(string.match(line, "[%d]+[%s]([%d]+)")) break end end diff --git a/cpuinf.lua b/cpuinf.lua index 9d1e7c1..d9cd786 100644 --- a/cpuinf.lua +++ b/cpuinf.lua @@ -26,13 +26,13 @@ local function worker(format) if string.match(line, "^processor.*") then cpu_id = string.match(line, "([%d]+)") elseif string.match(line, "^cpu MHz.*") then - local cpu_speed = string.match(line, "([%d]+)%.") + local cpu_speed = tonumber(string.match(line, "([%d]+)%.")) cpu_info["{cpu"..cpu_id.." mhz}"] = cpu_speed - cpu_info["{cpu"..cpu_id.." ghz}"] = tonumber(cpu_speed) / 1000 + cpu_info["{cpu"..cpu_id.." ghz}"] = cpu_speed / 1000 elseif string.match(line, "^cache size.*") then - local cpu_cache = string.match(line, "([%d]+)[%s]KB") + local cpu_cache = tonumber(string.match(line, "([%d]+)[%s]KB")) cpu_info["{cpu"..cpu_id.." kb}"] = cpu_cache - cpu_info["{cpu"..cpu_id.." mb}"] = tonumber(cpu_cache) / 1024 + cpu_info["{cpu"..cpu_id.." mb}"] = cpu_cache / 1024 end end f:close() diff --git a/entropy.lua b/entropy.lua index d36a8a9..f09dc1e 100644 --- a/entropy.lua +++ b/entropy.lua @@ -4,6 +4,7 @@ --------------------------------------------------- -- {{{ Grab environment +local tonumber = tonumber local io = { open = io.open } local setmetatable = setmetatable local math = { ceil = math.ceil } @@ -21,7 +22,7 @@ local function worker(format, poolsize) -- Get available entropy local f = io.open("/proc/sys/kernel/random/entropy_avail") - local ent = f:read("*line") + local ent = tonumber(f:read("*line")) f:close() -- Calculate percentage diff --git a/fs.lua b/fs.lua index 3aaaefb..4a5ab06 100644 --- a/fs.lua +++ b/fs.lua @@ -5,6 +5,7 @@ --------------------------------------------------- -- {{{ Grab environment +local tonumber = tonumber local io = { popen = io.popen } local setmetatable = setmetatable local string = { match = string.match } @@ -30,10 +31,10 @@ local function worker(format, nfs) -- Match all at once, including network file systems string.match(line, "^[%w%p]+[%s]+([%d%.]+)[%a]?[%s]+([%d%.]+)[%a]?[%s]+([%d%.]+)[%a]?[%s]+([%d]+)%%[%s]+([%w%p]+)$") - fs_info["{"..mount.." size}"] = size - fs_info["{"..mount.." used}"] = used - fs_info["{"..mount.." avail}"] = avail - fs_info["{"..mount.." usep}"] = usep + fs_info["{"..mount.." size}"] = tonumber(size) + fs_info["{"..mount.." used}"] = tonumber(used) + fs_info["{"..mount.." avail}"] = tonumber(avail) + fs_info["{"..mount.." usep}"] = tonumber(usep) end end f:close() diff --git a/gmail.lua b/gmail.lua index 345f6a6..2cdc9b9 100644 --- a/gmail.lua +++ b/gmail.lua @@ -4,6 +4,7 @@ --------------------------------------------------- -- {{{ Grab environment +local tonumber = tonumber local io = { popen = io.popen } local setmetatable = setmetatable local string = { match = string.match } @@ -24,7 +25,7 @@ local function worker(format, feed) local auth = user .. ":" .. pass local feed = feed or "https://mail.google.com/mail/feed/atom/unread" local mail = { - ["{count}"] = "0", + ["{count}"] = 0, ["{subject}"] = "N/A" } @@ -34,7 +35,7 @@ local function worker(format, feed) -- Could be huge don't read it all at once, info we are after is at the top for line in f:lines() do mail["{count}"] = -- Count comes before messages and matches at least 0 - string.match(line, "([%d]+)") or mail["{count}"] + tonumber(string.match(line, "([%d]+)")) or mail["{count}"] -- Find subject tags local title = string.match(line, "(.*)") diff --git a/hddtemp.lua b/hddtemp.lua index 9e35b5f..11952b9 100644 --- a/hddtemp.lua +++ b/hddtemp.lua @@ -4,6 +4,7 @@ --------------------------------------------------- -- {{{ Grab environment +local tonumber = tonumber local io = { popen = io.popen } local setmetatable = setmetatable local string = { match = string.match } @@ -27,7 +28,7 @@ local function worker(format, port) local disk, temp = string.match(line, "|([%/%a]+)|.*|([%d]+)|[CF]+|") if disk ~= nil and temp ~= nil then - hdd_temp["{"..disk.."}"] = temp + hdd_temp["{"..disk.."}"] = tonumber(temp) end end f:close() diff --git a/load.lua b/load.lua index 1448341..5769c07 100644 --- a/load.lua +++ b/load.lua @@ -4,6 +4,7 @@ --------------------------------------------------- -- {{{ Grab environment +local tonumber = tonumber local io = { open = io.open } local setmetatable = setmetatable local string = { match = string.match } @@ -23,7 +24,7 @@ local function worker(format) local l1, l5, l15 = -- Get load averages for past 1, 5 and 15 minutes string.match(line, "([%d]*%.[%d]*)%s([%d]*%.[%d]*)%s([%d]*%.[%d]*)") - return {l1, l5, l15} + return {tonumber(l1), tonumber(l5), tonumber(l15)} end -- }}} diff --git a/thermal.lua b/thermal.lua index 1697eb0..f83be13 100644 --- a/thermal.lua +++ b/thermal.lua @@ -4,6 +4,7 @@ --------------------------------------------------- -- {{{ Grab environment +local tonumber = tonumber local io = { open = io.open } local setmetatable = setmetatable local string = { match = string.match } @@ -19,11 +20,11 @@ local function worker(format, thermal_zone) -- Get an ACPI thermal zone local f = io.open("/proc/acpi/thermal_zone/"..thermal_zone.."/temperature") -- Handler for incompetent users - if not f then return {"N/A"} end + if not f then return {0} end local line = f:read("*line") f:close() - local temperature = string.match(line, "[%d]?[%d]?[%d]") + local temperature = tonumber(string.match(line, "[%d]?[%d]?[%d]")) return {temperature} end diff --git a/volume.lua b/volume.lua index f5b832f..16d4682 100644 --- a/volume.lua +++ b/volume.lua @@ -4,6 +4,7 @@ --------------------------------------------------- -- {{{ Grab environment +local tonumber = tonumber local io = { popen = io.popen } local setmetatable = setmetatable local string = { @@ -24,8 +25,8 @@ local function worker(format, channel) local mixer = f:read("*all") f:close() - local vol = string.match(mixer, "([%d]?[%d]?[%d])%%") - -- If muted return 0 (not "Mute") so we dont break progressbars + local vol = tonumber(string.match(mixer, "([%d]?[%d]?[%d])%%")) + -- If mute return 0 (not "Mute") so we don't break progressbars if string.find(mixer, "%[off%]") or vol == nil then vol = 0 end diff --git a/wifi.lua b/wifi.lua index 4ef9c00..5e307a0 100644 --- a/wifi.lua +++ b/wifi.lua @@ -4,6 +4,7 @@ --------------------------------------------------- -- {{{ Grab environment +local tonumber = tonumber local io = { popen = io.popen } local setmetatable = setmetatable local string = { @@ -30,7 +31,7 @@ local function worker(format, iface) ["{mode}"] = "N/A", ["{chan}"] = "N/A", ["{rate}"] = "N/A", - ["{link}"] = "N/A", + ["{link}"] = 0, ["{sign}"] = "N/A" } @@ -47,13 +48,13 @@ local function worker(format, iface) winfo["{mode}"] = -- Modes are simple, but also match the "-" in Ad-Hoc string.match(iw, "Mode[=:]([%w%-]*)") or winfo["{mode}"] winfo["{chan}"] = -- Channels are plain digits - string.match(iw, "Channel[=:]([%d]+)") or winfo["{chan}"] + tonumber(string.match(iw, "Channel[=:]([%d]+)") or winfo["{chan}"]) winfo["{rate}"] = -- Bitrate can start with a space and we want to display Mb/s string.match(iw, "Bit Rate[=:]([%s]?[%d%.]*[%s][%/%a]+)") or winfo["{rate}"] -- winfo["{link}"] = -- Link quality can contain a slash: 32/100 -- string.match(iw, "Link Quality[=:]([%d]+[%/%d]*)") or winfo["{link}"] - winfo["{link}"] = -- * match only the first number, great data for a progressbar - string.match(iw, "Link Quality[=:]([%d]+)") or winfo["{link}"] + winfo["{link}"] = -- * match only the first number, also suitable for a progressbar + tonumber(string.match(iw, "Link Quality[=:]([%d]+)") or winfo["{link}"]) winfo["{sign}"] = -- Signal level can be a negative value, also display decibel notation string.match(iw, "Signal level[=:]([%-%d]+[%s][%a]*)") or winfo["{sign}"] -- cgit v1.2.3