[FreeBSD] switched to async call (and fixed previous bug)
This commit is contained in:
parent
992d835c2e
commit
73b664b63e
|
@ -17,7 +17,7 @@ local bat_freebsd = {}
|
|||
-- {{{ Battery widget type
|
||||
local function parse(stdout, stderr, exitreason, exitcode)
|
||||
local bat_info = {}
|
||||
for line in string.gmatch(s,"[^\n]+") do
|
||||
for line in string.gmatch(stdout, "[^\n]+") do
|
||||
for key,value in string.gmatch(line, "(.+):%s+(.+)") do
|
||||
bat_info[key] = value
|
||||
end
|
||||
|
@ -65,11 +65,11 @@ local function parse(stdout, stderr, exitreason, exitcode)
|
|||
return {state, percent, time, wear, rate}
|
||||
end
|
||||
|
||||
function battery_freebsd.async(format, warg, callback)
|
||||
function bat_freebsd.async(format, warg, callback)
|
||||
local battery = warg or "batt"
|
||||
spawn.easy_async("acpiconf -i " .. helpers.shellquote(battery),
|
||||
function (...) callback(parse(...)) end)
|
||||
end
|
||||
-- }}}
|
||||
|
||||
return helpers.setasyncall(battery_freebsd)
|
||||
return helpers.setasyncall(bat_freebsd)
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
-- {{{ Grab environment
|
||||
local tonumber = tonumber
|
||||
local setmetatable = setmetatable
|
||||
local math = { floor = math.floor }
|
||||
local io = { popen = io.popen }
|
||||
local helpers = require("vicious.helpers")
|
||||
local spawn = require("vicious.spawn")
|
||||
local string = {
|
||||
match = string.match,
|
||||
gmatch = string.gmatch
|
||||
}
|
||||
-- }}}
|
||||
|
||||
-- Mem: provides RAM and Swap usage statistics
|
||||
|
@ -12,7 +15,7 @@ local mem_freebsd = {}
|
|||
|
||||
|
||||
-- {{{ Memory widget type
|
||||
local function worker(format)
|
||||
local function parse(stdout, stderr, exitreason, exitcode)
|
||||
local pagesize = tonumber(helpers.sysctl("hw.pagesize"))
|
||||
local vm_stats = helpers.sysctl_table("vm.stats.vm")
|
||||
local _mem = { buf = {}, total = nil }
|
||||
|
@ -54,19 +57,20 @@ local function worker(format)
|
|||
_swp.total = 0
|
||||
_swp.buf.free = 0
|
||||
|
||||
-- Read output of swapinfo in Mbytes
|
||||
local f = io.popen("swapinfo -m")
|
||||
-- Skip first line (table header)
|
||||
f:read("*line")
|
||||
-- Read output of swapinfo in Mbytes (from async function call)
|
||||
-- Read content and sum up
|
||||
for line in f:lines() do
|
||||
local counter = 1
|
||||
for line in string.gmatch(stdout, "[^\n]+") do
|
||||
-- Skip first line (table header)
|
||||
if counter ~= 1 then
|
||||
local ltotal, lused, lfree = string.match(line, "%s+([%d]+)%s+([%d]+)%s+([%d]+)")
|
||||
-- Add swap space in Mbytes
|
||||
_swp.total = _swp.total + tonumber(ltotal)
|
||||
_swp.inuse = _swp.inuse + tonumber(lused)
|
||||
_swp.buf.free = _swp.buf.free + tonumber(lfree)
|
||||
end
|
||||
f:close()
|
||||
counter = counter + 1
|
||||
end
|
||||
|
||||
-- Calculate percentage
|
||||
_swp.usep = math.floor(_swp.inuse / _swp.total * 100)
|
||||
|
@ -82,4 +86,10 @@ local function worker(format)
|
|||
_mem.wirep, _mem.wire, _mem.notfreeablep, _mem.notfreeable }
|
||||
end
|
||||
|
||||
return setmetatable(mem_freebsd, { __call = function(_, ...) return worker(...) end })
|
||||
function mem_freebsd.async(format, warg, callback)
|
||||
spawn.easy_async("swapinfo -m",
|
||||
function (...) callback(parse(...)) end)
|
||||
end
|
||||
-- }}}
|
||||
|
||||
return helpers.setasyncall(mem_freebsd)
|
||||
|
|
|
@ -16,14 +16,14 @@ local STATE = { on = '🔉', off = '🔈' }
|
|||
|
||||
local function parse(stdout, stderr, exitreason, exitcode)
|
||||
-- Capture mixer control state, e.g. 42 : 42
|
||||
local voll, volr = string.match(stdout, "([%d]+):([%d]+)$")
|
||||
local voll, volr = string.match(stdout, "([%d]+):([%d]+)\n$")
|
||||
if voll == "0" and volr == "0" then return { 0, 0, STATE.off } end
|
||||
return { tonumber(voll), tonumber(volr), STATE.on }
|
||||
end
|
||||
|
||||
function volume_freebsd.async(format, warg, callback)
|
||||
if not warg then return callback{} end
|
||||
spawn.easy_async("mixer -s " .. helpers.shellquote(warg),
|
||||
spawn.easy_async("mixer " .. helpers.shellquote(warg),
|
||||
function (...) callback(parse(...)) end)
|
||||
end
|
||||
-- }}}
|
||||
|
|
Loading…
Reference in New Issue