Skip to content

Commit 5f6acea

Browse files
JuanKman94Mic92
authored andcommitted
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
1 parent f3d6914 commit 5f6acea

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

contrib/README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ To use contrib widgets uncomment the line that loads them in
1313
init.lua. Or you can load them in your rc.lua after you require
1414
Vicious:
1515

16-
vicious = require("vicious")
17-
vicious.contrib = require("vicious.contrib")
16+
```lua
17+
vicious = require("vicious")
18+
vicious.contrib = require("vicious.contrib")
19+
```
1820

1921

2022
Widget types
@@ -60,6 +62,18 @@ Supported platforms: Linux (required tools: `sysfs`)
6062

6163
**vicious.contrib.countfiles**
6264

65+
**vicious.widgets.cmus**
66+
67+
Provides cmus player information using `cmus-remote`.
68+
Supported platforms: platform independent.
69+
70+
- Arguments:
71+
* Takes a table as an argument, 1st field should be the socket including host
72+
(or nil).
73+
- Returns:
74+
* Returns a table with string keys: `{status}`, `{artist}`, `{title}`,
75+
`{duration}`, `{file}`, `{continue}`, `{shuffle}`, `{repeat}`.
76+
6377
**vicious.contrib.dio**
6478

6579
Provides I/O statistics for requested storage devices
@@ -166,6 +180,8 @@ Supported Platforms: platform independent
166180

167181
Usage examples
168182
---------------------------------
183+
184+
```lua
169185
Pulse Audio widget
170186
vol = wibox.widget.textbox()
171187
vicious.register(vol, vicious.contrib.pulse, " $1%", 2, "alsa_output.pci-0000_00_1b.0.analog-stereo")
@@ -182,3 +198,4 @@ Buildbot widget
182198
{builder="tarball-slave", url="http://buildbot.buildbot.net"}
183199
}
184200
vicious.register(buildbotwidget, vicious.contrib.buildbot, "$1,", 3600, buildbotwidget_warg)
201+
```

contrib/cmus_all.lua

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
-----------------------------------------------------------
2+
-- Licensed under the GNU General Public License v2
3+
-- * (c) 2017, JuanKman94 <[email protected]>
4+
-----------------------------------------------------------
5+
6+
-- {{{ Grab environment
7+
local tonumber = tonumber
8+
local io = { popen = io.popen }
9+
local setmetatable = setmetatable
10+
local string = { gmatch = string.gmatch, format = string.format }
11+
local helpers = require("vicious.helpers")
12+
-- }}}
13+
14+
15+
-- Cmus: provides CMUS information
16+
-- vicious.widgets.cmus
17+
local cmus_all = {}
18+
19+
20+
-- {{{ CMUS widget type
21+
local function worker(format, warg)
22+
local cmus_state = {
23+
["{duration}"] = 0,
24+
["{file}"] = "N/A",
25+
["{status}"] = "N/A",
26+
["{title}"] = "N/A",
27+
["{artist}"] = "N/A",
28+
["{continue}"] = "off",
29+
["{shuffle}"] = "off",
30+
["{repeat}"] = "off",
31+
}
32+
33+
-- Fallback to CMUS defaults
34+
local host = warg and (warg.host or warg[1]) or os.getenv("CMUS_SOCKET")
35+
36+
if not host then
37+
if os.getenv("XDG_RUNTIME_DIR") then
38+
host = os.getenv("XDG_RUNTIME_DIR") .. "/cmus-socket"
39+
else
40+
host = os.getenv("HOME") .. "/.config/cmus/socket"
41+
end
42+
end
43+
44+
-- Get data from CMUS server
45+
local f = io.popen("cmus-remote --query --server " .. helpers.escape(host))
46+
47+
for line in f:lines() do
48+
for module, value in string.gmatch(line, "([%w]+) (.*)$") do
49+
if module == "file" then
50+
cmus_state["{"..module.."}"] = helpers.escape(value)
51+
elseif module == "duration" then
52+
cmus_state["{"..module.."}"] = tonumber(value)
53+
elseif module == "status" then
54+
cmus_state["{"..module.."}"] = value
55+
else
56+
for k, v in string.gmatch(value, "([%w]+) (.*)$") do
57+
if module == "tag" then
58+
if k == "title" or k == "artist" then
59+
cmus_state["{"..k.."}"] = v
60+
end
61+
elseif module == "set" then
62+
if k == "continue" or k == "shuffle" or k == "repeat" then
63+
if v == "true" then
64+
cmus_state["{"..k.."}"] = "on"
65+
end
66+
end
67+
end
68+
end
69+
end
70+
end
71+
end
72+
f:close()
73+
74+
return cmus_state
75+
end
76+
-- }}}
77+
78+
return setmetatable(cmus_all, { __call = function(_, ...) return worker(...) end })

0 commit comments

Comments
 (0)