From b8afc3aafc2f22cbf63f2b7d811941aa5f9f1542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Gia=20Phong?= Date: Thu, 4 Jul 2019 21:46:58 +0700 Subject: [PATCH] [cmus] Deprecate io.popen and promote to widgets/ --- README.md | 10 ++++++ contrib/README.md | 2 ++ contrib/cmus_all.lua | 75 +------------------------------------------- widgets/cmus_all.lua | 49 +++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 74 deletions(-) mode change 100644 => 120000 contrib/cmus_all.lua create mode 100644 widgets/cmus_all.lua diff --git a/README.md b/README.md index f88b39a..a6d8e59 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/contrib/README.md b/contrib/README.md index 106363f..54a39a7 100644 --- a/contrib/README.md +++ b/contrib/README.md @@ -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. diff --git a/contrib/cmus_all.lua b/contrib/cmus_all.lua deleted file mode 100644 index e2567e8..0000000 --- a/contrib/cmus_all.lua +++ /dev/null @@ -1,74 +0,0 @@ ------------------------------------------------------------ --- 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.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 }) diff --git a/contrib/cmus_all.lua b/contrib/cmus_all.lua new file mode 120000 index 0000000..1627269 --- /dev/null +++ b/contrib/cmus_all.lua @@ -0,0 +1 @@ +widgets/cmus_all.lua \ No newline at end of file diff --git a/widgets/cmus_all.lua b/widgets/cmus_all.lua new file mode 100644 index 0000000..04581bf --- /dev/null +++ b/widgets/cmus_all.lua @@ -0,0 +1,49 @@ +----------------------------------------------------------- +-- Licensed under the GNU General Public License v2 +-- * (c) 2017, JuanKman94 +----------------------------------------------------------- + +-- {{{ 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 }