1
1
Fork 0
mirror of https://github.com/vicious-widgets/vicious synced 2023-12-14 07:13:07 +01:00

Fallback asynchronous widget types to use spawn.lua

pkg_all was also optimized and use more updated package manager front-ends
This commit is contained in:
Nguyễn Gia Phong 2019-05-13 12:30:35 +07:00
parent 830f72346e
commit 5a0573ebef
5 changed files with 55 additions and 39 deletions

View file

@ -7,7 +7,7 @@
local setmetatable = setmetatable
local pcall = pcall
local helpers = require("vicious.helpers")
local spawn = require("awful.spawn")
local spawn = require("vicious.spawn")
local success, json = pcall(require, "cjson")
if not success then
@ -51,6 +51,13 @@ function btc_all.async(format, warg, callback)
spawn.easy_async(cmd, function(stdout) callback(parse(stdout)) end)
end
local function worker(format, warg)
local ret
btc_all.async(format, warg, function (price) ret = price end)
while ret == nil do end
return ret
end
-- }}}
return btc_all
return setmetatable(btc_all, { __call = function(_, ...) return worker(...) end })

View file

@ -16,8 +16,14 @@
-- You should have received a copy of the GNU Affero General Public License
-- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
local status, spawn = pcall(require, "awful.spawn")
if status then return spawn end
local status, awful = pcall(require, "awful")
if status then
local spawn = awful.spawn
function spawn.with_line_callback_with_shell(cmd, callbacks)
spawn.with_line_callback({ awful.util.shell, "-c", cmd }, callbacks)
end
return spawn
end
local io = { popen = io.popen }
@ -26,7 +32,7 @@ local io = { popen = io.popen }
local spawn = {}
--- Spawn a program and capture its output line by line.
-- @tparam string|table cmd The command.
-- @tparam string cmd The command.
-- @tab callbacks Table containing callbacks that should be invoked on
-- various conditions.
-- @tparam[opt] function callbacks.stdout Function that is called with each
@ -44,7 +50,7 @@ local spawn = {}
-- For "signal", the second argument is the signal causing process
-- termination.
-- @treturn boolean|nil true if cmd terminated successfully, or nil otherwise
function spawn.with_line_callback(cmd, callbacks)
function spawn.with_line_callback_with_shell(cmd, callbacks)
local stdout_callback, stdout = callbacks.stdout, io.popen(cmd)
for line in stdout:lines() do stdout_callback(line) end
if callbacks.output_done then callbacks.output_done() end
@ -55,7 +61,7 @@ function spawn.with_line_callback(cmd, callbacks)
end
--- Spawn a program and capture its output.
-- @tparam string|table cmd The command.
-- @tparam string cmd The command.
-- @tab callback Function with the following arguments
-- @tparam string callback.stdout Output on stdout.
-- @tparam string callback.stderr Output on stderr,
@ -64,7 +70,7 @@ end
-- @tparam integer callback.exitcode Exit code (exit code or signal number,
-- depending on "exitreason").
-- @treturn boolean|nil true if cmd terminated successfully, or nil otherwise
function spawn.easy_async(cmd, callback)
function spawn.easy_async_with_shell(cmd, callback)
local out_stream = io.popen(cmd)
local stdout = out_stream:read("*all")
local success, reason, code = out_stream:close() -- requiring Lua 5.2
@ -72,5 +78,9 @@ function spawn.easy_async(cmd, callback)
return success
end
-- Since io.popen always use a shell
spawn.easy_async = spawn.easy_async_with_shell
spawn.with_line_callback = spawn.with_line_callback_with_shell
return spawn
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View file

@ -7,7 +7,7 @@
local io = { popen = io.popen }
local math = { max = math.max }
local setmetatable = setmetatable
local spawn = require("awful.spawn")
local spawn = require("vicious.spawn")
-- }}}
@ -15,39 +15,32 @@ local spawn = require("awful.spawn")
-- vicious.widgets.pkg
local pkg_all = {}
local PKGMGR = {
["Arch"] = { cmd = "pacman -Qu" },
["Arch C"] = { cmd = "checkupdates" },
["Arch S"] = { cmd = "yes | pacman -Sup", sub = 1 },
["Debian"] = { cmd = "apt list --upgradable", sub = 1 },
["Ubuntu"] = { cmd = "apt list --upgradable", sub = 1 },
["Fedora"] = { cmd = "dnf check-update", sub = 2 },
["FreeBSD"] = { cmd = "pkg version -I -l '<'" },
["Mandriva"] = { cmd = "urpmq --auto-select" }
}
-- {{{ Packages widget type
function pkg_all.async(format, warg, callback)
if not warg then return end
if not warg then return callback{} end
local pkgmgr = PKGMGR[warg]
-- Initialize counters
local manager = {
["Arch"] = { cmd = "pacman -Qu" },
["Arch C"] = { cmd = "checkupdates" },
["Arch S"] = { cmd = "yes | pacman -Sup", sub = 1 },
["Debian"] = { cmd = "apt-show-versions -u -b" },
["Ubuntu"] = { cmd = "aptitude search '~U'" },
["Fedora"] = { cmd = "yum list updates", sub = 3 },
["FreeBSD"] ={ cmd = "pkg version -I -l '<'" },
["Mandriva"]={ cmd = "urpmq --auto-select" }
}
-- Check if updates are available
local function parse(str, skiprows)
local size, lines, first = 0, "", skiprows or 0
for line in str:gmatch("[^\r\n]+") do
if size >= first then
lines = lines .. (size == first and "" or "\n") .. line
end
local size, lines = -pkgmgr.sub, ""
spawn.with_line_callback_with_shell(pkgmgr.cmd, {
stdout = function (str)
size = size + 1
if size > 0 then lines = lines .. str .. "\n" end
end,
output_done = function ()
callback{ size, lines }
end
size = math.max(size-first, 0)
return {size, lines}
end
-- Select command
local _pkg = manager[warg]
spawn.easy_async(_pkg.cmd, function(stdout) callback(parse(stdout, _pkg.sub)) end)
})
end
-- }}}

View file

@ -9,6 +9,7 @@ local io = { popen = io.popen }
local setmetatable = setmetatable
local string = { match = string.match }
local helpers = require("vicious.helpers")
local spawn = require("vicious.spawn")
-- }}}
@ -16,6 +17,11 @@ local helpers = require("vicious.helpers")
-- vicious.widgets.volume
local volume_linux = {}
local STATES = { on = '🔉', off = '🔈' }
function volume_linux.async(format, warg, callback)
if not warg then return callback{} end
end
-- {{{ Volume widget type
local function worker(format, warg)

View file

@ -11,7 +11,7 @@ local math = { ceil = math.ceil }
local string = { match = string.match }
-- Awesome library for spawning programs
local spawn = require"awful.spawn"
local spawn = require"vicious.spawn"
local helpers = require("vicious.helpers")
-- }}}
@ -86,12 +86,12 @@ local function parse(ws)
end
function weather_all.async(format, warg, callback)
if not warg then return end
if not warg then return callback{} end
-- Get weather forceast by the station ICAO code, from:
-- * US National Oceanic and Atmospheric Administration
local url = ("https://tgftp.nws.noaa.gov/data/observations/metar/decoded/%s.TXT"):format(warg)
local cmd = "curl -fs " .. helpers.shellquote(url)
local cmd = "curl -fs " .. url
spawn.easy_async(cmd, function (stdout) callback(parse(stdout)) end)
end