Debian Codegraph @

vicious/widgets/cmus_all.lua

raw symbols SCIP
1 -- cmus music player widget type
2 -- Copyright (C) 2017 JuanKman94 <[email protected]>
3 -- Copyright (C) 2017 Joerg Thalheim <[email protected]>
4 -- Copyright (C) 2018-2019 Nguyễn Gia Phong <[email protected]m>
5 --
6 -- This file is part of Vicious.
7 --
8 -- Vicious is free software: you can redistribute it and/or modify
9 -- it under the terms of the GNU General Public License as
10 -- published by the Free Software Foundation, either version 2 of the
11 -- License, or (at your option) any later version.
12 --
13 -- Vicious is distributed in the hope that it will be useful,
14 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
15 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 -- GNU General Public License for more details.
17 --
18 -- You should have received a copy of the GNU General Public License
19 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
20
21 -- {{{ Grab environment
22 local type = type
23 local tonumber = tonumber
24 local os = { getenv = os.getenv }
25
26 local helpers = require"vicious.helpers"
27 local spawn = require"vicious.spawn"
28 -- }}}
29
30 local CMUS_SOCKET = helpers.shellquote(os.getenv"CMUS_SOCKET")
31
32 -- Cmus: provides CMUS information
33 -- vicious.widgets.cmus
34 return helpers.setasyncall{
35 async = function (format, warg, callback)
36 local server = ""
37 if type(warg) == "table" then
38 server = " --server " .. helpers.shellquote(warg.host or warg[1])
39 elseif CMUS_SOCKET ~= nil then
40 server = " --server " .. CMUS_SOCKET
41 end
42
43 local cmus_state = { ["{duration}"] = 0, ["{file}"] = "N/A",
44 ["{status}"] = "N/A", ["{title}"] = "N/A",
45 ["{artist}"] = "N/A", ["{continue}"] = "off",
46 ["{shuffle}"] = "off", ["{repeat}"] = "off" }
47
48 spawn.with_line_callback("cmus-remote --query" .. server, {
49 stdout = function (line)
50 for module, value in line:gmatch"([%w]+) (.*)$" do
51 if module == "file" or module == "status" then
52 cmus_state["{"..module.."}"] = value
53 elseif module == "duration" then
54 cmus_state["{"..module.."}"] = tonumber(value)
55 else
56 local k, v = value:gmatch("([%w]+) (.*)$")()
57 if module == "tag" then
58 cmus_state["{"..k.."}"] = v
59 elseif module == "set" and v == "true" then
60 cmus_state["{"..k.."}"] = "on"
61 end
62 end
63 end
64 end,
65 output_done = function () callback(cmus_state) end })
66 end }