From 6f002577b993d0c8e9a35f1bac27139bb35b063f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Gia=20Phong?= Date: Thu, 16 May 2019 18:52:22 +0700 Subject: [PATCH] [volume_freebsd] Use spawn.easy_async instead of io.popen NOTE: Similar to volume_linux, the default mute symbol has also been changed. --- Changes.md | 4 ++-- widgets/volume_freebsd.lua | 35 ++++++++++++++--------------------- widgets/volume_linux.lua | 1 + 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/Changes.md b/Changes.md index cb0f732..3ca064e 100644 --- a/Changes.md +++ b/Changes.md @@ -1,6 +1,6 @@ IMPORTANT: -- `volume_linux` now uses 🔉 and 🔈 instead of ♫ and ♩ to show mute state. +- `volume` now uses 🔉 and 🔈 instead of ♫ and ♩ to show mute state. This BREAKS backward compatibility if users substitute custom symbols from these default. @@ -16,7 +16,7 @@ Added: Fixed: -- [volume_linux] Deprecate `io.popen` +- [volume] Deprecate `io.popen` - [mpd] Lua 5.3 compatibility (for real this time); also correct a typo - [pkg,weather,contrib/btc] Allow function call without Awesome - [pkg] Use more updated front-ends for Debian/Ubuntu (apt) and Fedora (dnf) diff --git a/widgets/volume_freebsd.lua b/widgets/volume_freebsd.lua index 200f4b7..5927150 100644 --- a/widgets/volume_freebsd.lua +++ b/widgets/volume_freebsd.lua @@ -1,9 +1,8 @@ -- {{{ Grab environment local tonumber = tonumber -local io = { popen = io.popen } -local setmetatable = setmetatable local string = { match = string.match } -local helpers = require("vicious.helpers") +local helpers = require"vicious.helpers" +local spawn = require"vicious.spawn" -- }}} @@ -13,26 +12,20 @@ local volume_freebsd = {} -- {{{ Volume widget type -local function worker(format, warg) - if not warg then return end +local STATE = { on = '🔉', off = '🔈' } - local mixer_state = { "♫", "♩" } - - -- Get mixer control contents - f = io.popen("mixer -s " .. helpers.shellquote(warg)) - local mixer = f:read() - f:close() - - -- Capture mixer control state: [5%] ... ... [on] - local voll, volr = string.match(mixer, "([%d]+):([%d]+)$") - - if voll == "0" and volr == "0" then - return {0, 0, mixer_state[2]} - else - return {voll, volr, mixer_state[1]} - end +local function parse(stdout, stderr, exitreason, exitcode) + -- Capture mixer control state, e.g. 42 : 42 + local voll, volr = string.match(stdout, "([%d]+):([%d]+)$") + 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), + function (...) callback(parse(...)) end) end -- }}} -return setmetatable(volume_freebsd, { __call = function(_, ...) return worker(...) end }) +return helpers.setasyncall(volume_freebsd) diff --git a/widgets/volume_linux.lua b/widgets/volume_linux.lua index 098017d..3c281af 100644 --- a/widgets/volume_linux.lua +++ b/widgets/volume_linux.lua @@ -4,6 +4,7 @@ --------------------------------------------------- -- {{{ Grab environment +local type = type local tonumber = tonumber local string = { match = string.match } local table = { concat = table.concat }