diff --git a/contrib/btc_all.lua b/contrib/btc_all.lua index 5942711..b107a47 100644 --- a/contrib/btc_all.lua +++ b/contrib/btc_all.lua @@ -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 }) diff --git a/spawn.lua b/spawn.lua index d252684..858d15f 100644 --- a/spawn.lua +++ b/spawn.lua @@ -16,8 +16,14 @@ -- You should have received a copy of the GNU Affero General Public License -- along with Vicious. If not, see . -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 diff --git a/widgets/pkg_all.lua b/widgets/pkg_all.lua index 62cc61e..cda1b06 100644 --- a/widgets/pkg_all.lua +++ b/widgets/pkg_all.lua @@ -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 -- }}} diff --git a/widgets/volume_linux.lua b/widgets/volume_linux.lua index 0fd6820..96725f0 100644 --- a/widgets/volume_linux.lua +++ b/widgets/volume_linux.lua @@ -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) diff --git a/widgets/weather_all.lua b/widgets/weather_all.lua index 7c5a96d..7b38208 100644 --- a/widgets/weather_all.lua +++ b/widgets/weather_all.lua @@ -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