Add widget for cmus (follow mpd conventions) (#46)

* Add widget for cmus (follow mpd conventions)

* Add widget for cmus (follow mpd conventions)

* Add default socket address when XDG_RUNTIME_DIR is not set

* cmus: use on/off instead of true/false

- default value was a boolean which is not valid in our format function
  and would lead to a crash
- on/off seems more human readable

* cmus: shell-escape user supplied variables

* Add default socket address when XDG_RUNTIME_DIR is not set
This commit is contained in:
Juan Carlos Menonita 2017-11-22 15:01:30 -06:00 committed by Jörg Thalheim
parent f3d69142c6
commit 5f6acea35c
2 changed files with 97 additions and 2 deletions

View File

@ -13,8 +13,10 @@ To use contrib widgets uncomment the line that loads them in
init.lua. Or you can load them in your rc.lua after you require
Vicious:
vicious = require("vicious")
vicious.contrib = require("vicious.contrib")
```lua
vicious = require("vicious")
vicious.contrib = require("vicious.contrib")
```
Widget types
@ -60,6 +62,18 @@ Supported platforms: Linux (required tools: `sysfs`)
**vicious.contrib.countfiles**
**vicious.widgets.cmus**
Provides cmus player information using `cmus-remote`.
Supported platforms: platform independent.
- Arguments:
* Takes a table as an argument, 1st field should be the socket including host
(or nil).
- Returns:
* Returns a table with string keys: `{status}`, `{artist}`, `{title}`,
`{duration}`, `{file}`, `{continue}`, `{shuffle}`, `{repeat}`.
**vicious.contrib.dio**
Provides I/O statistics for requested storage devices
@ -166,6 +180,8 @@ Supported Platforms: platform independent
Usage examples
---------------------------------
```lua
Pulse Audio widget
vol = wibox.widget.textbox()
vicious.register(vol, vicious.contrib.pulse, " $1%", 2, "alsa_output.pci-0000_00_1b.0.analog-stereo")
@ -182,3 +198,4 @@ Buildbot widget
{builder="tarball-slave", url="http://buildbot.buildbot.net"}
}
vicious.register(buildbotwidget, vicious.contrib.buildbot, "$1,", 3600, buildbotwidget_warg)
```

78
contrib/cmus_all.lua Normal file
View File

@ -0,0 +1,78 @@
-----------------------------------------------------------
-- 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.escape(host))
for line in f:lines() do
for module, value in string.gmatch(line, "([%w]+) (.*)$") do
if module == "file" then
cmus_state["{"..module.."}"] = helpers.escape(value)
elseif module == "duration" then
cmus_state["{"..module.."}"] = tonumber(value)
elseif module == "status" then
cmus_state["{"..module.."}"] = 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 })