helpers: index subdirectories in pathtotable()
This commit is contained in:
parent
7cbf987a2a
commit
49b1b0972f
16
helpers.lua
16
helpers.lua
|
@ -11,6 +11,7 @@
|
|||
local pairs = pairs
|
||||
local io = { open = io.open }
|
||||
local setmetatable = setmetatable
|
||||
local getmetatable = getmetatable
|
||||
local string = {
|
||||
upper = string.upper,
|
||||
format = string.format
|
||||
|
@ -28,14 +29,21 @@ local scroller = {}
|
|||
|
||||
-- {{{ Helper functions
|
||||
-- {{{ Expose path as a Lua table
|
||||
function pathtotable(path)
|
||||
return setmetatable({},
|
||||
{ __index = function(_, name)
|
||||
local f = io.open(path .. '/' .. name)
|
||||
function pathtotable(dir)
|
||||
return setmetatable({ _path = dir },
|
||||
{ __index = function(table, index)
|
||||
local path = table._path .. '/' .. index
|
||||
local f = io.open(path)
|
||||
if f then
|
||||
local s = f:read("*all")
|
||||
f:close()
|
||||
if s then
|
||||
return s
|
||||
else
|
||||
local o = { _path = path }
|
||||
setmetatable(o, getmetatable(table))
|
||||
return o
|
||||
end
|
||||
end
|
||||
end
|
||||
})
|
||||
|
|
|
@ -28,7 +28,6 @@ local function worker(format, disk)
|
|||
|
||||
local disk_lines = { [disk] = {} }
|
||||
local disk_stats = helpers.pathtotable("/sys/block/" .. disk)
|
||||
local disk_queue = helpers.pathtotable("/sys/block/" .. disk .. "/queue")
|
||||
|
||||
if disk_stats.stat then
|
||||
local match = string.gmatch(disk_stats.stat, "[%s]+([%d]+)")
|
||||
|
@ -62,8 +61,8 @@ local function worker(format, disk)
|
|||
helpers.uformat(disk_usage[disk], "total", diff_total[disk][7] + diff_total[disk][3], unit)
|
||||
|
||||
-- Store I/O scheduler
|
||||
if disk_queue.scheduler then
|
||||
disk_usage[disk]["{sched}"] = string.gmatch(disk_queue.scheduler, "%[([%a]+)%]")
|
||||
if disk_stats.queue.scheduler then
|
||||
disk_usage[disk]["{sched}"] = string.gmatch(disk_stats.queue.scheduler, "%[([%a]+)%]")
|
||||
end
|
||||
|
||||
return disk_usage[disk]
|
||||
|
|
Loading…
Reference in New Issue