[cmus] Deprecate io.popen and promote to widgets/

This commit is contained in:
Nguyễn Gia Phong 2019-07-04 21:46:58 +07:00
parent 0e7f5e5bcb
commit b8afc3aafc
4 changed files with 62 additions and 74 deletions

View File

@ -154,6 +154,16 @@ Supported platforms: GNU/Linux (require `sysfs`), FreeBSD (require `acpiconf`),
* `$4`: Wear level in percent
* `$5`: Current (dis)charge rate in Watt
### vicious.contrib.cmus
Provides cmus player information using `cmus-remote`.
Supported platforms: platform independent.
* Argument: a table whose first field is the socket including host (or nil).
* Returns a table with string keys: `${status}`, `${artist}`, `${title}`,
`${duration}`, `${file}`, `${continue}`, `${shuffle}`, `${repeat}`.
### vicious.widgets.cpu
Provides CPU usage for all available CPUs/cores. Since this widget type give

View File

@ -87,6 +87,8 @@ yellow - in progress.
### vicious.contrib.cmus
NOTE: This widget type has been promoted to `widgets`.
Provides cmus player information using `cmus-remote`.
Supported platforms: platform independent.

View File

@ -1,74 +0,0 @@
-----------------------------------------------------------
-- Licensed under the GNU General Public License v2
-- * (c) 2017, JuanKman94 <juan.carlos.menonita@gmail.com>
-----------------------------------------------------------
-- {{{ Grab environment
local tonumber = tonumber
local io = { popen = io.popen }
local setmetatable = setmetatable
local string = { gmatch = string.gmatch, format = string.format }
local helpers = require("vicious.helpers")
-- }}}
-- Cmus: provides CMUS information
-- vicious.widgets.cmus
local cmus_all = {}
-- {{{ CMUS widget type
local function worker(format, warg)
local cmus_state = {
["{duration}"] = 0,
["{file}"] = "N/A",
["{status}"] = "N/A",
["{title}"] = "N/A",
["{artist}"] = "N/A",
["{continue}"] = "off",
["{shuffle}"] = "off",
["{repeat}"] = "off",
}
-- Fallback to CMUS defaults
local host = warg and (warg.host or warg[1]) or os.getenv("CMUS_SOCKET")
if not host then
if os.getenv("XDG_RUNTIME_DIR") then
host = os.getenv("XDG_RUNTIME_DIR") .. "/cmus-socket"
else
host = os.getenv("HOME") .. "/.config/cmus/socket"
end
end
-- Get data from CMUS server
local f = io.popen("cmus-remote --query --server " .. helpers.shellquote(host))
for line in f:lines() do
for module, value in string.gmatch(line, "([%w]+) (.*)$") do
if module == "file" or module == "status" then
cmus_state["{"..module.."}"] = value
elseif module == "duration" then
cmus_state["{"..module.."}"] = tonumber(value)
else
for k, v in string.gmatch(value, "([%w]+) (.*)$") do
if module == "tag" then
if k == "title" or k == "artist" then
cmus_state["{"..k.."}"] = v
end
elseif module == "set" then
if k == "continue" or k == "shuffle" or k == "repeat" then
if v == "true" then
cmus_state["{"..k.."}"] = "on"
end
end
end
end
end
end
end
f:close()
return cmus_state
end
-- }}}
return setmetatable(cmus_all, { __call = function(_, ...) return worker(...) end })

1
contrib/cmus_all.lua Symbolic link
View File

@ -0,0 +1 @@
widgets/cmus_all.lua

49
widgets/cmus_all.lua Normal file
View File

@ -0,0 +1,49 @@
-----------------------------------------------------------
-- Licensed under the GNU General Public License v2
-- * (c) 2017, JuanKman94 <juan.carlos.menonita@gmail.com>
-----------------------------------------------------------
-- {{{ Grab environment
local type = type
local tonumber = tonumber
local os = { getenv = os.getenv }
local helpers = require"vicious.helpers"
local spawn = require"vicious.spawn"
-- }}}
-- Cmus: provides CMUS information
-- vicious.widgets.cmus
return helpers.setasyncall{
async = function (format, warg, callback)
local server = ""
if type(warg) == "table" then
server = " --server " .. helpers.shellquote(warg.host or warg[1])
elseif CMUS_SOCKET then
server = " --server " .. helpers.shellquote(os.getenv"CMUS_SOCKET")
end
local cmus_state = { ["{duration}"] = 0, ["{file}"] = "N/A",
["{status}"] = "N/A", ["{title}"] = "N/A",
["{artist}"] = "N/A", ["{continue}"] = "off",
["{shuffle}"] = "off", ["{repeat}"] = "off" }
spawn.with_line_callback("cmus-remote --query" .. server, {
stdout = function (line)
for module, value in line:gmatch"([%w]+) (.*)$" do
if module == "file" or module == "status" then
cmus_state["{"..module.."}"] = value
elseif module == "duration" then
cmus_state["{"..module.."}"] = tonumber(value)
else
local k, v = value:gmatch("([%w]+) (.*)$")()
if module == "tag" then
cmus_state["{"..k.."}"] = v
elseif module == "set" and v == "true" then
cmus_state["{"..k.."}"] = "on"
end
end
end
end,
output_done = function () callback(cmus_state) end })
end }