diff --git a/contrib/README.md b/contrib/README.md index 3cd4471..a9f9284 100644 --- a/contrib/README.md +++ b/contrib/README.md @@ -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) +``` diff --git a/contrib/cmus_all.lua b/contrib/cmus_all.lua new file mode 100644 index 0000000..d0c74cd --- /dev/null +++ b/contrib/cmus_all.lua @@ -0,0 +1,78 @@ +----------------------------------------------------------- +-- Licensed under the GNU General Public License v2 +-- * (c) 2017, JuanKman94 +----------------------------------------------------------- + +-- {{{ 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 })