From 96a8e557d3bb9b887dd7d504a4c20f05a87e6cbe Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Sat, 13 Mar 2010 02:11:41 +0100 Subject: Make use of io.lines() where appropriate --- cpu.lua | 8 +++----- cpuinf.lua | 8 +++----- mem.lua | 8 +++----- net.lua | 8 +++----- org.lua | 7 ++----- 5 files changed, 14 insertions(+), 25 deletions(-) diff --git a/cpu.lua b/cpu.lua index 9f3839e..7c4907c 100644 --- a/cpu.lua +++ b/cpu.lua @@ -6,7 +6,7 @@ -- {{{ Grab environment local ipairs = ipairs -local io = { open = io.open } +local io = { lines = io.lines } local setmetatable = setmetatable local math = { floor = math.floor } local table = { insert = table.insert } @@ -28,11 +28,10 @@ local cpu_active = {} -- {{{ CPU widget type local function worker(format) - -- Get /proc/stat - local f = io.open("/proc/stat") local cpu_lines = {} - for line in f:lines() do + -- Get CPU stats + for line in io.lines("/proc/stat") do if string.find(line, "^cpu") then cpu_lines[#cpu_lines+1] = {} @@ -41,7 +40,6 @@ local function worker(format) end end end - f:close() -- Ensure tables are initialized correctly while #cpu_total < #cpu_lines do diff --git a/cpuinf.lua b/cpuinf.lua index 2d99356..3604dee 100644 --- a/cpuinf.lua +++ b/cpuinf.lua @@ -5,7 +5,7 @@ -- {{{ Grab environment local tonumber = tonumber -local io = { open = io.open } +local io = { lines = io.lines } local setmetatable = setmetatable local string = { match = string.match } -- }}} @@ -17,12 +17,11 @@ module("vicious.cpuinf") -- {{{ CPU Information widget type local function worker(format) - -- Get cpuinfo - local f = io.open("/proc/cpuinfo") local cpu_id = nil local cpu_info = {} - for line in f:lines() do + -- Get CPU info + for line in io.lines("/proc/cpuinfo") do if string.match(line, "^processor.*") then cpu_id = string.match(line, "([%d]+)") elseif string.match(line, "^cpu MHz.*") then @@ -35,7 +34,6 @@ local function worker(format) cpu_info["{cpu"..cpu_id.." mb}"] = cpu_cache / 1024 end end - f:close() return cpu_info end diff --git a/mem.lua b/mem.lua index f331ae4..7c18c89 100644 --- a/mem.lua +++ b/mem.lua @@ -5,7 +5,7 @@ --------------------------------------------------- -- {{{ Grab environment -local io = { open = io.open } +local io = { lines = io.lines } local setmetatable = setmetatable local math = { floor = math.floor } local string = { gmatch = string.gmatch } @@ -18,11 +18,10 @@ module("vicious.mem") -- {{{ Memory widget type local function worker(format) - -- Get meminfo - local f = io.open("/proc/meminfo") local mem = { buf = {}, swp = {} } - for line in f:lines() do + -- Get MEM info + for line in io.lines("/proc/meminfo") do for k, v in string.gmatch(line, "([%a]+):[%s]+([%d]+).+") do if k == "MemTotal" then mem.total = math.floor(v/1024) elseif k == "MemFree" then mem.buf.f = math.floor(v/1024) @@ -33,7 +32,6 @@ local function worker(format) end end end - f:close() -- Calculate memory percentage mem.free = mem.buf.f + mem.buf.b + mem.buf.c diff --git a/net.lua b/net.lua index 365d3c6..0550557 100644 --- a/net.lua +++ b/net.lua @@ -7,7 +7,7 @@ -- {{{ Grab environment local tonumber = tonumber local os = { time = os.time } -local io = { open = io.open } +local io = { lines = io.lines } local setmetatable = setmetatable local string = { match = string.match } local helpers = require("vicious.helpers") @@ -27,11 +27,10 @@ local unit = { ["b"] = 1, ["kb"] = 1024, -- {{{ Net widget type local function worker(format) - -- Get /proc/net/dev - local f = io.open("/proc/net/dev") local args = {} - for line in f:lines() do + -- Get NET stats + for line in io.lines("/proc/net/dev") do -- Match wmaster0 as well as rt0 (multiple leading spaces) local name = string.match(line, "^[%s]?[%s]?[%s]?[%s]?([%w]+):") if name ~= nil then @@ -68,7 +67,6 @@ local function worker(format) nets[name][2] = send end end - f:close() return args end diff --git a/org.lua b/org.lua index 716cd47..3222167 100644 --- a/org.lua +++ b/org.lua @@ -5,7 +5,7 @@ --------------------------------------------------- -- {{{ Grab environment -local io = { open = io.open } +local io = { lines = io.lines } local setmetatable = setmetatable local string = { find = string.find } local os = { @@ -31,9 +31,7 @@ local function worker(format, warg) -- Get data from agenda files for i=1, #warg do - local f = io.open(warg[i]) - - for line in f:lines() do + for line in io.lines(warg[i]) do local scheduled = string.find(line, "SCHEDULED:") local closed = string.find(line, "CLOSED:") local deadline = string.find(line, "DEADLINE:") @@ -56,7 +54,6 @@ local function worker(format, warg) end end end - f:close() end return {count.past, count.today, count.soon, count.future} -- cgit v1.2.3