From d81eed6d310bce28b3709980d80c433a04eefaae Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Mon, 5 Oct 2009 00:10:47 +0200 Subject: Lots of coding style changes --- batat.lua | 9 +++++---- cpu.lua | 10 +++++++--- cpuinf.lua | 13 +++++++------ dio.lua | 10 +++++++--- entropy.lua | 6 +++--- fs.lua | 5 +++-- gmail.lua | 10 +++++----- hddtemp.lua | 3 ++- load.lua | 5 +++-- mdir.lua | 4 ++-- mpd.lua | 6 ++++-- net.lua | 11 ++++++----- pacman.lua | 3 ++- thermal.lua | 5 +++-- uptime.lua | 13 +++++++------ volume.lua | 8 ++++---- 16 files changed, 70 insertions(+), 51 deletions(-) diff --git a/batat.lua b/batat.lua index b160dc7..2e40d9f 100644 --- a/batat.lua +++ b/batat.lua @@ -7,6 +7,7 @@ local io = { popen = io.popen } local setmetatable = setmetatable local table = { insert = table.insert } +local string = { match = string.match } -- }}} @@ -30,12 +31,12 @@ local function worker(format) for line in f:lines() do -- Check if the battery is present - if line:match("^[%s]+Battery.*") then + if string.match(line, "^[%s]+Battery.*") then -- Store state and charge information - table.insert(battery_info, (battery_state[line:match("([%a]*),")] or "/")) - table.insert(battery_info, (line:match("([%d]?[%d]?[%d])%.") or "/")) + table.insert(battery_info, (battery_state[string.match(line, "([%a]*),")] or "/")) + table.insert(battery_info, (string.match(line, "([%d]?[%d]?[%d])%.") or "/")) -- Store remaining time information - table.insert(battery_info, (line:match("%%,%s(.*)") or "/")) + table.insert(battery_info, (string.match(line, "%%,%s(.*)") or "/")) else return {"/", "/", "/"} end diff --git a/cpu.lua b/cpu.lua index 73250ee..9dabb7f 100644 --- a/cpu.lua +++ b/cpu.lua @@ -10,6 +10,10 @@ local io = { open = io.open } local setmetatable = setmetatable local math = { floor = math.floor } local table = { insert = table.insert } +local string = { + find = string.find, + gmatch = string.gmatch +} -- }}} @@ -29,13 +33,13 @@ local function worker(format) local cpu_lines = {} for line in f:lines() do - if line:find("^cpu") then + if string.find(line, "^cpu") then if #cpu_lines < 1 then cpuid = 1 else cpuid = #cpu_lines + 1 end cpu_lines[cpuid] = {} - for match in line:gmatch("[%s]+([%d]+)") do - table.insert(cpu_lines[cpuid], match) + for i in string.gmatch(line, "[%s]+([%d]+)") do + table.insert(cpu_lines[cpuid], i) end end end diff --git a/cpuinf.lua b/cpuinf.lua index a21570d..9d1e7c1 100644 --- a/cpuinf.lua +++ b/cpuinf.lua @@ -7,6 +7,7 @@ local tonumber = tonumber local io = { open = io.open } local setmetatable = setmetatable +local string = { match = string.match } -- }}} @@ -22,14 +23,14 @@ local function worker(format) local cpu_info = {} for line in f:lines() do - if line:match("^processor.*") then - cpu_id = line:match("([%d]+)") - elseif line:match("^cpu MHz.*") then - local cpu_speed = line:match("([%d]+)%.") + 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]+)%.") cpu_info["{cpu"..cpu_id.." mhz}"] = cpu_speed cpu_info["{cpu"..cpu_id.." ghz}"] = tonumber(cpu_speed) / 1000 - elseif line:match("^cache size.*") then - local cpu_cache = line:match("([%d]+)[%s]KB") + elseif string.match(line, "^cache size.*") then + local cpu_cache = string.match(line, "([%d]+)[%s]KB") cpu_info["{cpu"..cpu_id.." kb}"] = cpu_cache cpu_info["{cpu"..cpu_id.." mb}"] = tonumber(cpu_cache) / 1024 end diff --git a/dio.lua b/dio.lua index dfde90c..9c94134 100644 --- a/dio.lua +++ b/dio.lua @@ -10,6 +10,10 @@ local io = { open = io.open } local setmetatable = setmetatable local math = { floor = math.floor } local table = { insert = table.insert } +local string = { + match = string.match, + gmatch = string.gmatch +} -- }}} @@ -28,14 +32,14 @@ local function worker(format, disk) local disk_lines = {} for line in f:lines() do - if line:match("("..disk..")%s") then + if string.match(line, "("..disk..")%s") then -- Todo: find a way to do this - --for stat in line:gmatch("%s([%d]+)") do + --for stat in string.gmatch(line, "%s([%d]+)") do -- table.insert(disk_lines, stat) --end -- -- Skip first two matches - local stat = line:gmatch("%s([%d]+)") + local stat = string.gmatch(line, "%s([%d]+)") stat() stat() -- Store the rest diff --git a/entropy.lua b/entropy.lua index ca7ee85..d36a8a9 100644 --- a/entropy.lua +++ b/entropy.lua @@ -21,13 +21,13 @@ local function worker(format, poolsize) -- Get available entropy local f = io.open("/proc/sys/kernel/random/entropy_avail") - local ent_avail = f:read("*line") + local ent = f:read("*line") f:close() -- Calculate percentage - local ent_avail_percent = math.ceil(ent_avail * 100 / poolsize) + local ent_percent = math.ceil(ent * 100 / poolsize) - return {ent_avail, ent_avail_percent} + return {ent, ent_percent} end -- }}} diff --git a/fs.lua b/fs.lua index a65a1ff..3aaaefb 100644 --- a/fs.lua +++ b/fs.lua @@ -7,6 +7,7 @@ -- {{{ Grab environment local io = { popen = io.popen } local setmetatable = setmetatable +local string = { match = string.match } -- }}} @@ -24,10 +25,10 @@ local function worker(format, nfs) local fs_info = {} for line in f:lines() do - if not line:match("^Filesystem.*") then + if not string.match(line, "^Filesystem.*") then local size, used, avail, usep, mount = -- Match all at once, including network file systems - line:match("^[%w%p]+[%s]+([%d%.]+)[%a]?[%s]+([%d%.]+)[%a]?[%s]+([%d%.]+)[%a]?[%s]+([%d]+)%%[%s]+([%w%p]+)$") + 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 diff --git a/gmail.lua b/gmail.lua index c31f425..345f6a6 100644 --- a/gmail.lua +++ b/gmail.lua @@ -6,6 +6,7 @@ -- {{{ Grab environment local io = { popen = io.popen } local setmetatable = setmetatable +local string = { match = string.match } local helpers = require("vicious.helpers") -- }}} @@ -15,7 +16,7 @@ module("vicious.gmail") -- User data -local user = "" -- Todo +local user = "" -- Todo: local pass = "" -- * find a safer storage -- {{{ Gmail widget type @@ -32,10 +33,11 @@ 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}"] = line:match("([%d]+)") or mail["{count}"] + mail["{count}"] = -- Count comes before messages and matches at least 0 + string.match(line, "([%d]+)") or mail["{count}"] -- Find subject tags - local title = line:match("(.*)") + local title = string.match(line, "(.*)") -- If the subject changed then break out of the loop if title ~= nil and -- Todo: find a better way to deal with 1st title title ~= "Gmail - Label 'unread' for "..user.."@gmail.com" then @@ -43,8 +45,6 @@ local function worker(format, feed) title = helpers.escape(title) -- Don't abuse the wibox, truncate, then store mail["{subject}"] = helpers.truncate(title, 22) - -- By this point we have the count, it comes before - -- messages and always matches, at least 0 break end end diff --git a/hddtemp.lua b/hddtemp.lua index 7bdb764..9e35b5f 100644 --- a/hddtemp.lua +++ b/hddtemp.lua @@ -6,6 +6,7 @@ -- {{{ Grab environment local io = { popen = io.popen } local setmetatable = setmetatable +local string = { match = string.match } -- }}} @@ -23,7 +24,7 @@ local function worker(format, port) local hdd_temp = {} for line in f:lines() do - local disk, temp = line:match("|([%/%a]+)|.*|([%d]+)|[CF]+|") + local disk, temp = string.match(line, "|([%/%a]+)|.*|([%d]+)|[CF]+|") if disk ~= nil and temp ~= nil then hdd_temp["{"..disk.."}"] = temp diff --git a/load.lua b/load.lua index f2de4b9..1448341 100644 --- a/load.lua +++ b/load.lua @@ -6,6 +6,7 @@ -- {{{ Grab environment local io = { open = io.open } local setmetatable = setmetatable +local string = { match = string.match } -- }}} @@ -15,12 +16,12 @@ module("vicious.load") -- {{{ Load widget type local function worker(format) - -- Get load averages local f = io.open('/proc/loadavg') local line = f:read("*line") f:close() - local l1, l5, l15 = line:match("([%d]*%.[%d]*)%s([%d]*%.[%d]*)%s([%d]*%.[%d]*)") + 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} end diff --git a/mdir.lua b/mdir.lua index 5aafdd1..8fc7ac6 100644 --- a/mdir.lua +++ b/mdir.lua @@ -20,12 +20,12 @@ local function worker(format, mdir) local count = { new = 0, cur = 0 } -- Recursively find new messages - local f = io.popen("find " .. mdir .. " -type f -wholename '*/new/*'") + local f = io.popen("find "..mdir.." -type f -wholename '*/new/*'") for line in f:lines() do count.new = count.new + 1 end f:close() -- Recursively find "old" messages lacking the Seen flag - local f = io.popen("find " .. mdir .. " -type f -regex '.*/cur/.*2,[^S]*$'") + local f = io.popen("find "..mdir.." -type f -regex '.*/cur/.*2,[^S]*$'") for line in f:lines() do count.cur = count.cur + 1 end f:close() diff --git a/mpd.lua b/mpd.lua index 73412eb..350858f 100644 --- a/mpd.lua +++ b/mpd.lua @@ -8,6 +8,7 @@ local io = { popen = io.popen } local setmetatable = setmetatable local helpers = require("vicious.helpers") +local string = { find = string.find } -- }}} @@ -23,7 +24,8 @@ local function worker(format) f:close() -- Check if it's stopped, off or not installed - if np == nil or (np:find("MPD_HOST") or np:find("volume:")) then + if np == nil + or (string.find(np, "MPD_HOST") or string.find(np, "volume:")) then return {"Stopped"} end @@ -31,7 +33,7 @@ local function worker(format) local nowplaying = helpers.escape(np) -- Don't abuse the wibox, truncate - local nowplaying = helpers.truncate(nowplaying, 30) + nowplaying = helpers.truncate(nowplaying, 30) return {nowplaying} end diff --git a/net.lua b/net.lua index 7d72708..5b4c07e 100644 --- a/net.lua +++ b/net.lua @@ -10,6 +10,7 @@ local os = { time = os.time } local io = { open = io.open } local setmetatable = setmetatable local math = { floor = math.floor } +local string = { match = string.match } -- }}} @@ -28,12 +29,12 @@ local function worker(format) for line in f:lines() do -- Match wmaster0 as well as rt0 (multiple leading spaces) - if line:match("^[%s]?[%s]?[%s]?[%s]?[%w]+:") then - local name = line:match("^[%s]?[%s]?[%s]?[%s]?([%w]+):") + if string.match(line, "^[%s]?[%s]?[%s]?[%s]?[%w]+:") then + local name = string.match(line, "^[%s]?[%s]?[%s]?[%s]?([%w]+):") -- Received bytes, first value after the name - local recv = tonumber(line:match(":[%s]*([%d]+)")) - -- Transmited bytes, 7 fields from end of the line - local send = tonumber(line:match("([%d]+)%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d$")) + local recv = tonumber(string.match(line, ":[%s]*([%d]+)")) + local send = -- Transmited bytes, 7 fields from end of the line + tonumber(string.match(line, "([%d]+)%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d$")) args["{"..name.." rx_b}"] = math.floor(recv*10)/10 args["{"..name.." tx_b}"] = math.floor(send*10)/10 diff --git a/pacman.lua b/pacman.lua index 73f9f03..aa2d974 100644 --- a/pacman.lua +++ b/pacman.lua @@ -7,6 +7,7 @@ local tonumber = tonumber local io = { popen = io.popen } local setmetatable = setmetatable +--local string = { match = string.match } -- }}} @@ -24,7 +25,7 @@ local function worker(format) for line in f:lines() do -- Pacman 3.2 provides the number of available updates - --updates = line:match("^Targets[%s]%(([%d]+)%)") or 0 + --updates = string.match(line, "^Targets[%s]%(([%d]+)%)") or 0 ---- If the count changed then break out of the loop --if tonumber(updates) > 0 then -- break diff --git a/thermal.lua b/thermal.lua index ab7ef89..1697eb0 100644 --- a/thermal.lua +++ b/thermal.lua @@ -6,6 +6,7 @@ -- {{{ Grab environment local io = { open = io.open } local setmetatable = setmetatable +local string = { match = string.match } -- }}} @@ -16,13 +17,13 @@ module("vicious.thermal") -- {{{ Thermal widget type local function worker(format, thermal_zone) -- Get an ACPI thermal zone - local f = io.open("/proc/acpi/thermal_zone/" .. thermal_zone .. "/temperature") + local f = io.open("/proc/acpi/thermal_zone/"..thermal_zone.."/temperature") -- Handler for incompetent users if not f then return {"N/A"} end local line = f:read("*line") f:close() - local temperature = line:match("[%d]?[%d]?[%d]") + local temperature = string.match(line, "[%d]?[%d]?[%d]") return {temperature} end diff --git a/uptime.lua b/uptime.lua index 50bc179..712bb14 100644 --- a/uptime.lua +++ b/uptime.lua @@ -9,6 +9,7 @@ local tonumber = tonumber local io = { open = io.open } local setmetatable = setmetatable local math = { floor = math.floor } +local string = { match = string.match } -- }}} @@ -23,13 +24,13 @@ local function worker(format) local line = f:read("*line") f:close() - local total_uptime = math.floor(tonumber(line:match("[%d%.]+"))) - local uptime_days = math.floor(total_uptime / (3600 * 24)) - local uptime_hours = math.floor((total_uptime % (3600 * 24)) / 3600) - local uptime_minutes = math.floor(((total_uptime % (3600 * 24)) % 3600) / 60) - local uptime_seconds = math.floor(((total_uptime % (3600 * 24)) % 3600) % 60) + local up_t = math.floor(tonumber(string.match(line, "[%d%.]+"))) + local up_d = math.floor(up_t / (3600 * 24)) + local up_h = math.floor((up_t % (3600 * 24)) / 3600) + local up_m = math.floor(((up_t % (3600 * 24)) % 3600) / 60) + local up_s = math.floor(((up_t % (3600 * 24)) % 3600) % 60) - return {total_uptime, uptime_days, uptime_hours, uptime_minutes, uptime_seconds} + return {up_t, up_d, up_h, up_m, up_s} end -- }}} diff --git a/volume.lua b/volume.lua index d02c79e..f5b832f 100644 --- a/volume.lua +++ b/volume.lua @@ -24,13 +24,13 @@ local function worker(format, channel) local mixer = f:read("*all") f:close() - local volume_level = string.match(mixer, "([%d]?[%d]?[%d])%%") + local vol = string.match(mixer, "([%d]?[%d]?[%d])%%") -- If muted return 0 (not "Mute") so we dont break progressbars - if string.find(mixer, "%[off%]") or volume_level == nil then - volume_level = 0 + if string.find(mixer, "%[off%]") or vol == nil then + vol = 0 end - return {volume_level} + return {vol} end -- }}} -- cgit v1.2.3