Make use of io.lines() where appropriate

This commit is contained in:
Adrian C. (anrxc) 2010-03-13 02:11:41 +01:00
parent 6441db0a94
commit 96a8e557d3
5 changed files with 14 additions and 25 deletions

View File

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

View File

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

View File

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

View File

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

View File

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