| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
local io = io |
| 7 |
local setmetatable = setmetatable |
| 8 |
local pairs = pairs |
| 9 |
local lib = { |
| 10 |
widget = require("obvious.lib.widget") |
| 11 |
} |
| 12 |
|
| 13 |
|
| 14 |
local function fs(path) |
| 15 |
local df = io.popen("LC_ALL=C df -hP " .. path) |
| 16 |
local key |
| 17 |
local ret = nil |
| 18 |
|
| 19 |
if not df then |
| 20 |
return nil |
| 21 |
end |
| 22 |
|
| 23 |
for line in df:lines() do |
| 24 |
local mountpoint = line:match("%% ([-/%w]+)$") |
| 25 |
local dev = line:match("^[%w/-]+") |
| 26 |
local perc = line:match("(%d+)%%") |
| 27 |
|
| 28 |
if perc ~= nil and mountpoint ~= nil and dev ~= nil then |
| 29 |
ret = perc |
| 30 |
end |
| 31 |
end |
| 32 |
|
| 33 |
df:close() |
| 34 |
return ret |
| 35 |
end |
| 36 |
|
| 37 |
local function get(data) |
| 38 |
return fs(data.path) |
| 39 |
end |
| 40 |
|
| 41 |
local function get_data_source(path) |
| 42 |
local path = path or "/" |
| 43 |
local ret = {} |
| 44 |
|
| 45 |
ret.max = 100 |
| 46 |
ret.get = get |
| 47 |
ret.path = path |
| 48 |
|
| 49 |
return lib.widget.from_data_source(ret) |
| 50 |
end |
| 51 |
|
| 52 |
return setmetatable({ |
| 53 |
fs = fs, |
| 54 |
}, { __call = function (_, ...) return get_data_source(...) end }) |
| 55 |
|
| 56 |
|