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...
This commit is contained in:
Adrian C. (anrxc) 2009-10-26 20:32:48 +01:00
parent b105ae21cd
commit 0d73f6d8ae
12 changed files with 36 additions and 26 deletions

View File

@ -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

View File

@ -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()

View File

@ -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

View File

@ -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()

View File

@ -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

9
fs.lua
View File

@ -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()

View File

@ -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, "<fullcount>([%d]+)</fullcount>") or mail["{count}"]
tonumber(string.match(line, "<fullcount>([%d]+)</fullcount>")) or mail["{count}"]
-- Find subject tags
local title = string.match(line, "<title>(.*)</title>")

View File

@ -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()

View File

@ -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
-- }}}

View File

@ -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

View File

@ -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

View File

@ -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}"]