[net_freebsd] Switched to async spawn

This commit is contained in:
mutlusun 2019-05-27 09:52:00 +02:00
parent 73b664b63e
commit 06d96a92ba
1 changed files with 14 additions and 10 deletions

View File

@ -1,10 +1,12 @@
-- {{{ Grab environment -- {{{ Grab environment
local tonumber = tonumber local tonumber = tonumber
local os = { time = os.time } local os = { time = os.time }
local setmetatable = setmetatable
local helpers = require("vicious.helpers") local helpers = require("vicious.helpers")
local io = { popen = io.popen } local spawn = require("vicious.spawn")
local string = { match = string.match } local string = {
match = string.match,
gmatch = string.gmatch
}
-- }}} -- }}}
@ -21,15 +23,13 @@ local unit = { ["b"] = 1, ["kb"] = 1024,
} }
-- {{{ Net widget type -- {{{ Net widget type
local function worker(format, warg) local function parse(stdout, stderr, exitreason, exitcode)
if not warg then return end
local args = {} local args = {}
local buffer = nil local buffer = nil
local f = io.popen("netstat -n -b -I " .. helpers.shellquote(warg))
local now = os.time() local now = os.time()
for line in f:lines() do for line in string.gmatch(stdout, "[^\n]+") do
if not (line:find("<Link") or line:find("Name")) then -- skipping missleading lines if not (line:find("<Link") or line:find("Name")) then -- skipping missleading lines
local split = { line:match(("([^%s]*)%s*"):rep(12)) } local split = { line:match(("([^%s]*)%s*"):rep(12)) }
@ -41,8 +41,6 @@ local function worker(format, warg)
end end
end end
f:close()
if buffer == nil then if buffer == nil then
args["{carrier}"] = 0 args["{carrier}"] = 0
helpers.uformat(args, "rx", 0, unit) helpers.uformat(args, "rx", 0, unit)
@ -78,6 +76,12 @@ local function worker(format, warg)
return args return args
end end
function net_freebsd.async(format, warg, callback)
if not warg then return callback{} end
spawn.easy_async("netstat -n -b -I " .. helpers.shellquote(warg),
function (...) callback(parse(...)) end)
end
-- }}} -- }}}
return setmetatable(net_freebsd, { __call = function(_, ...) return worker(...) end }) return helpers.setasyncall(net_freebsd)