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:
parent
830f72346e
commit
5a0573ebef
|
@ -7,7 +7,7 @@
|
||||||
local setmetatable = setmetatable
|
local setmetatable = setmetatable
|
||||||
local pcall = pcall
|
local pcall = pcall
|
||||||
local helpers = require("vicious.helpers")
|
local helpers = require("vicious.helpers")
|
||||||
local spawn = require("awful.spawn")
|
local spawn = require("vicious.spawn")
|
||||||
|
|
||||||
local success, json = pcall(require, "cjson")
|
local success, json = pcall(require, "cjson")
|
||||||
if not success then
|
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)
|
spawn.easy_async(cmd, function(stdout) callback(parse(stdout)) end)
|
||||||
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 })
|
||||||
|
|
22
spawn.lua
22
spawn.lua
|
@ -16,8 +16,14 @@
|
||||||
-- You should have received a copy of the GNU Affero General Public License
|
-- You should have received a copy of the GNU Affero General Public License
|
||||||
-- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
|
-- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
local status, spawn = pcall(require, "awful.spawn")
|
local status, awful = pcall(require, "awful")
|
||||||
if status then return spawn end
|
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 }
|
local io = { popen = io.popen }
|
||||||
|
|
||||||
|
@ -26,7 +32,7 @@ local io = { popen = io.popen }
|
||||||
local spawn = {}
|
local spawn = {}
|
||||||
|
|
||||||
--- Spawn a program and capture its output line by line.
|
--- 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
|
-- @tab callbacks Table containing callbacks that should be invoked on
|
||||||
-- various conditions.
|
-- various conditions.
|
||||||
-- @tparam[opt] function callbacks.stdout Function that is called with each
|
-- @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
|
-- For "signal", the second argument is the signal causing process
|
||||||
-- termination.
|
-- termination.
|
||||||
-- @treturn boolean|nil true if cmd terminated successfully, or nil otherwise
|
-- @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)
|
local stdout_callback, stdout = callbacks.stdout, io.popen(cmd)
|
||||||
for line in stdout:lines() do stdout_callback(line) end
|
for line in stdout:lines() do stdout_callback(line) end
|
||||||
if callbacks.output_done then callbacks.output_done() end
|
if callbacks.output_done then callbacks.output_done() end
|
||||||
|
@ -55,7 +61,7 @@ function spawn.with_line_callback(cmd, callbacks)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Spawn a program and capture its output.
|
--- 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
|
-- @tab callback Function with the following arguments
|
||||||
-- @tparam string callback.stdout Output on stdout.
|
-- @tparam string callback.stdout Output on stdout.
|
||||||
-- @tparam string callback.stderr Output on stderr,
|
-- @tparam string callback.stderr Output on stderr,
|
||||||
|
@ -64,7 +70,7 @@ end
|
||||||
-- @tparam integer callback.exitcode Exit code (exit code or signal number,
|
-- @tparam integer callback.exitcode Exit code (exit code or signal number,
|
||||||
-- depending on "exitreason").
|
-- depending on "exitreason").
|
||||||
-- @treturn boolean|nil true if cmd terminated successfully, or nil otherwise
|
-- @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 out_stream = io.popen(cmd)
|
||||||
local stdout = out_stream:read("*all")
|
local stdout = out_stream:read("*all")
|
||||||
local success, reason, code = out_stream:close() -- requiring Lua 5.2
|
local success, reason, code = out_stream:close() -- requiring Lua 5.2
|
||||||
|
@ -72,5 +78,9 @@ function spawn.easy_async(cmd, callback)
|
||||||
return success
|
return success
|
||||||
end
|
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
|
return spawn
|
||||||
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
local io = { popen = io.popen }
|
local io = { popen = io.popen }
|
||||||
local math = { max = math.max }
|
local math = { max = math.max }
|
||||||
local setmetatable = setmetatable
|
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
|
-- vicious.widgets.pkg
|
||||||
local pkg_all = {}
|
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
|
-- {{{ Packages widget type
|
||||||
function pkg_all.async(format, warg, callback)
|
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 size, lines = -pkgmgr.sub, ""
|
||||||
local manager = {
|
spawn.with_line_callback_with_shell(pkgmgr.cmd, {
|
||||||
["Arch"] = { cmd = "pacman -Qu" },
|
stdout = function (str)
|
||||||
["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
|
|
||||||
size = size + 1
|
size = size + 1
|
||||||
|
if size > 0 then lines = lines .. str .. "\n" end
|
||||||
|
end,
|
||||||
|
output_done = function ()
|
||||||
|
callback{ size, lines }
|
||||||
end
|
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
|
end
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ local io = { popen = io.popen }
|
||||||
local setmetatable = setmetatable
|
local setmetatable = setmetatable
|
||||||
local string = { match = string.match }
|
local string = { match = string.match }
|
||||||
local helpers = require("vicious.helpers")
|
local helpers = require("vicious.helpers")
|
||||||
|
local spawn = require("vicious.spawn")
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,6 +17,11 @@ local helpers = require("vicious.helpers")
|
||||||
-- vicious.widgets.volume
|
-- vicious.widgets.volume
|
||||||
local volume_linux = {}
|
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
|
-- {{{ Volume widget type
|
||||||
local function worker(format, warg)
|
local function worker(format, warg)
|
||||||
|
|
|
@ -11,7 +11,7 @@ local math = { ceil = math.ceil }
|
||||||
local string = { match = string.match }
|
local string = { match = string.match }
|
||||||
|
|
||||||
-- Awesome library for spawning programs
|
-- Awesome library for spawning programs
|
||||||
local spawn = require"awful.spawn"
|
local spawn = require"vicious.spawn"
|
||||||
|
|
||||||
local helpers = require("vicious.helpers")
|
local helpers = require("vicious.helpers")
|
||||||
-- }}}
|
-- }}}
|
||||||
|
@ -86,12 +86,12 @@ local function parse(ws)
|
||||||
end
|
end
|
||||||
|
|
||||||
function weather_all.async(format, warg, callback)
|
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:
|
-- Get weather forceast by the station ICAO code, from:
|
||||||
-- * US National Oceanic and Atmospheric Administration
|
-- * US National Oceanic and Atmospheric Administration
|
||||||
local url = ("https://tgftp.nws.noaa.gov/data/observations/metar/decoded/%s.TXT"):format(warg)
|
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)
|
spawn.easy_async(cmd, function (stdout) callback(parse(stdout)) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue