From 49b1b0972f7db6ef77b459e3010c3b569fa69534 Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Mon, 15 Mar 2010 03:02:50 +0100 Subject: helpers: index subdirectories in pathtotable() --- helpers.lua | 18 +++++++++++++----- widgets/dio.lua | 5 ++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/helpers.lua b/helpers.lua index 8b6541d..c61468e 100644 --- a/helpers.lua +++ b/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() - return s + if s then + return s + else + local o = { _path = path } + setmetatable(o, getmetatable(table)) + return o + end end end }) diff --git a/widgets/dio.lua b/widgets/dio.lua index 64a56e1..e886dac 100644 --- a/widgets/dio.lua +++ b/widgets/dio.lua @@ -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] -- cgit v1.2.3