aboutsummaryrefslogtreecommitdiff
path: root/dio.lua
diff options
context:
space:
mode:
authorAdrian C. (anrxc) <anrxc@sysphere.org>2009-11-11 03:00:33 +0100
committerAdrian C. (anrxc) <anrxc@sysphere.org>2009-11-11 03:00:33 +0100
commit5f9818f9d32013481e9a777bfa0e651024856e08 (patch)
treed1393bc661fb10015b8aec6ef95242d17f32c8e8 /dio.lua
parent92be5fbae59b5d63c09f35f435a2738cdcb3a953 (diff)
downloadvicious-legacy-5f9818f9d32013481e9a777bfa0e651024856e08.tar.xz
dio: widget type rewritten
I wrote this module with graphs in mind and returned values were somewhat hostile towards textbox widgets. Now KB/MB values are formatted to one decimal point. There is still work to be done.
Diffstat (limited to 'dio.lua')
-rw-r--r--dio.lua36
1 files changed, 13 insertions, 23 deletions
diff --git a/dio.lua b/dio.lua
index ea3aa9d..41c0663 100644
--- a/dio.lua
+++ b/dio.lua
@@ -5,13 +5,13 @@
-- {{{ Grab environment
local ipairs = ipairs
-local io = { open = io.open }
local setmetatable = setmetatable
local math = { floor = math.floor }
local table = { insert = table.insert }
+local helpers = require("vicious.helpers")
local string = {
- match = string.match,
- gmatch = string.gmatch
+ gmatch = string.gmatch,
+ format = string.format
}
-- }}}
@@ -26,28 +26,18 @@ local disk_total = {}
-- {{{ Disk I/O widget type
local function worker(format, disk)
- -- Get /proc/diskstats
- local f = io.open("/proc/diskstats")
local disk_lines = {}
+ local disk_stats = setmetatable(
+ { _path = "/sys/block/" .. disk },
+ helpers.pathtotable
+ )
- for line in f:lines() do
- if string.match(line, "("..disk..")%s") then
- -- Todo: find a way to do this
- --for stat in string.gmatch(line, "%s([%d]+)") do
- -- table.insert(disk_lines, stat)
- --end
- --
- -- Skip first two matches
- local stat = string.gmatch(line, "%s([%d]+)")
- stat()
- stat()
- -- Store the rest
- for i = 1, 11 do
- table.insert(disk_lines, stat())
- end
+ if disk_stats.stat then
+ local match = string.gmatch(disk_stats.stat, "[%s]+([%d]+)")
+ for i = 1, 11 do -- Store disk stats
+ table.insert(disk_lines, match())
end
end
- f:close()
-- Ensure tables are initialized correctly
while #disk_total < #disk_lines do
@@ -67,8 +57,8 @@ local function worker(format, disk)
-- Calculate I/O
disk_usage["{raw}"] = diff_total[7] + diff_total[3]
-- Divide "sectors read" by 2 and 1024 to get KB and MB
- disk_usage["{kb}"] = math.floor(diff_total[7] + diff_total[3])/2
- disk_usage["{mb}"] = math.floor(diff_total[7] + diff_total[3])/1024
+ disk_usage["{kb}"] = string.format("%.1f", math.floor(diff_total[7] + diff_total[3])/2)
+ disk_usage["{mb}"] = string.format("%.1f", math.floor(diff_total[7] + diff_total[3])/1024)
return disk_usage
end