Skip to content

Commit 983253a

Browse files
getzzeMic92
authored andcommitted
Allow asynchronous call (for Awesome4+) (#32)
1 parent fe10ee8 commit 983253a

File tree

3 files changed

+71
-36
lines changed

3 files changed

+71
-36
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,11 @@ Supported platforms: platform independent.
395395

396396
- Arguments:
397397
* Takes the Linux or BSD distribution name as an argument, i.e. `"Arch"`,
398-
`"FreeBSD"`
398+
`"Arch C"`, `"Arch S"`, `"Debian"`, `"Ubuntu"`, `"Fedora"`, `"FreeBSD"`,
399+
`"Mandriva"`
399400
- Returns:
400-
* Returns 1st value as the count of available updates
401+
* Returns 1st value as the count of available updates, 2nd as the list of
402+
packages to update
401403

402404
**vicious.widgets.raid**
403405

init.lua

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,38 +49,52 @@ local function update(widget, reg, disablecache)
4949
local t = os.time()
5050
local data = {}
5151

52-
-- Check for chached output newer than the last update
53-
if widget_cache[reg.wtype] ~= nil then
54-
local c = widget_cache[reg.wtype]
55-
56-
if (c.time == nil or c.time <= t-reg.timer) or disablecache then
57-
c.time, c.data = t, reg.wtype(reg.format, reg.warg)
52+
local function format_data(data)
53+
local ret
54+
if type(data) == "table" then
55+
if type(reg.format) == "string" then
56+
ret = helpers.format(reg.format, data)
57+
elseif type(reg.format) == "function" then
58+
ret = reg.format(widget, data)
59+
end
5860
end
59-
60-
data = c.data
61-
else
62-
data = reg.wtype and reg.wtype(reg.format, reg.warg)
61+
return ret or data
6362
end
6463

65-
if type(data) == "table" then
66-
if type(reg.format) == "string" then
67-
data = helpers.format(reg.format, data)
68-
elseif type(reg.format) == "function" then
69-
data = reg.format(widget, data)
64+
local function update_value(data, t, cache)
65+
if widget.add_value ~= nil then
66+
widget:add_value(tonumber(data) and tonumber(data)/100)
67+
elseif widget.set_value ~= nil then
68+
widget:set_value(tonumber(data) and tonumber(data)/100)
69+
elseif widget.set_markup ~= nil then
70+
widget:set_markup(data)
71+
else
72+
widget.text = data
73+
end
74+
-- Update cache
75+
if t and cache then
76+
cache.time, cache.data = t, data
7077
end
7178
end
72-
73-
if widget.add_value ~= nil then
74-
widget:add_value(tonumber(data) and tonumber(data)/100)
75-
elseif widget.set_value ~= nil then
76-
widget:set_value(tonumber(data) and tonumber(data)/100)
77-
elseif widget.set_markup ~= nil then
78-
widget:set_markup(data)
79-
else
80-
widget.text = data
79+
80+
-- Check for cached output newer than the last update
81+
local c = widget_cache[reg.wtype]
82+
if c and c.time and c.data and t < c.time+reg.timer and not disablecache then
83+
return update_value(format_data(c.data))
84+
elseif reg.wtype then
85+
if reg.wtype.async then
86+
if not reg.lock then
87+
reg.lock = true
88+
return reg.wtype.async(reg.warg,
89+
function(data)
90+
update_value(format_data(data), t, c)
91+
reg.lock=false
92+
end)
93+
end
94+
else
95+
return update_value(format_data(reg.wtype(nil, reg.warg)), t, c)
96+
end
8197
end
82-
83-
return data
8498
end
8599
-- }}}
86100

@@ -147,6 +161,7 @@ function vicious.register(widget, wtype, format, timer, warg)
147161
local reg = {
148162
-- Set properties
149163
wtype = wtype,
164+
lock = false,
150165
format = format,
151166
timer = timer,
152167
warg = warg,

widgets/pkg_all.lua

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
local io = { popen = io.popen }
88
local math = { max = math.max }
99
local setmetatable = setmetatable
10+
local spawn = require("awful.spawn")
1011
-- }}}
1112

1213

@@ -16,11 +17,10 @@ local pkg_all = {}
1617

1718

1819
-- {{{ Packages widget type
19-
local function worker(format, warg)
20+
function pkg_all.async(warg, callback)
2021
if not warg then return end
2122

2223
-- Initialize counters
23-
local updates = 0
2424
local manager = {
2525
["Arch"] = { cmd = "pacman -Qu" },
2626
["Arch C"] = { cmd = "checkupdates" },
@@ -33,16 +33,34 @@ local function worker(format, warg)
3333
}
3434

3535
-- Check if updates are available
36+
local function parse(str, skiprows)
37+
local size, lines, first = 0, "", skiprows or 0
38+
for line in str:gmatch("[^\r\n]+") do
39+
if size >= first then
40+
lines = lines .. (size == first and "" or "\n") .. line
41+
end
42+
size = size + 1
43+
end
44+
size = math.max(size-first, 0)
45+
return {size, lines}
46+
end
47+
48+
-- Select command
3649
local _pkg = manager[warg]
37-
local f = io.popen(_pkg.cmd)
50+
spawn.easy_async(_pkg.cmd, function(stdout) callback(parse(stdout, _pkg.sub)) end)
51+
end
52+
-- }}}
3853

39-
for line in f:lines() do
40-
updates = updates + 1
41-
end
42-
f:close()
54+
-- {{{ Packages widget type
55+
local function worker(format, warg)
56+
local ret = nil
57+
58+
pkg_all.async(warg, function(data) ret = data end)
4359

44-
return {_pkg.sub and math.max(updates-_pkg.sub, 0) or updates}
60+
while ret==nil do end
61+
return ret
4562
end
4663
-- }}}
4764

65+
4866
return setmetatable(pkg_all, { __call = function(_, ...) return worker(...) end })

0 commit comments

Comments
 (0)