Skip to content

Commit f27e2c0

Browse files
Nomarianflashcode
authored andcommitted
New script cmus_announce.lua: Messages current buffer/channel now-playing song from cmus.
1 parent dfba089 commit f27e2c0

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

lua/cmus_announce.lua

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
2+
-- cmus_announce.lua - Announce currently playing file in cmus to channel
3+
4+
--[[
5+
BSD Zero Clause License
6+
7+
iCopyright © 2024-2024 by <[email protected]>
8+
9+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
10+
11+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
12+
--]]
13+
14+
--[[
15+
NOTE!
16+
cmus-remote could hang if you don't have mpris installed giving you a warning at the beginning
17+
18+
Design
19+
A defaultFormat is passed to cmus format_print, output is absorbed and s/\n/ /
20+
be aware that spaces in cmus become newlines, so s/\n/ / is used
21+
string.sub is run to limit the output
22+
23+
TODO
24+
Probably add/support colors %{red} and so on
25+
defaultFormat should be more colorful, with some emojis
26+
--]]
27+
28+
local defaultFormat = "%{artist} - %{album} - %{title}"
29+
local msgLimit = 250
30+
31+
local
32+
io, string, os
33+
=
34+
io, string, os
35+
36+
local Gsub = string.gsub
37+
38+
local ShellArgS = function -- string
39+
(
40+
cmdS -- string
41+
)
42+
return "'" .. Gsub(cmdS, "'","'\\''") .. "'"
43+
end
44+
45+
local ExistsB, Exists do
46+
local Rename = os.rename
47+
ExistsB = function(fileS)
48+
-- returns true/false if fileS exists
49+
local B, _, code = Rename(fileS,fileS)
50+
return B or code==13
51+
end
52+
53+
-- returns fileS if fileS exists or falsy
54+
Exists = function(fileS)
55+
return ExistsB(fileS) and fileS
56+
end
57+
end
58+
59+
local Getenv = os.getenv
60+
61+
local XDG_RUNTIME_DIR =
62+
Getenv"XDG_RUNTIME_DIR"
63+
or "/run/nmz"
64+
65+
local cmusSocketFile = Getenv"CMUS_SOCKET"
66+
67+
68+
local IsAlive = function ()
69+
if cmusSocketFile then
70+
return ExistsB(cmusSocketFile)
71+
end
72+
cmusSocketFile = Exists(XDG_RUNTIME_DIR .. "/cmus-socket")
73+
return cmusSocketFile
74+
end
75+
76+
-- Get Current Song
77+
local Current = function -- string
78+
(
79+
format -- string|falsy
80+
)
81+
format = format or defaultFormat
82+
83+
local arg = ShellArgS("format_print " .. format)
84+
local S
85+
do
86+
local H = io.popen("cmus-remote -C " .. arg)
87+
S = H:read"a"
88+
H:close()
89+
end
90+
return S or ""
91+
end
92+
93+
-- displays filename if nothing useful returns
94+
local Display = function -- string
95+
(
96+
format -- string|nil
97+
)
98+
local msg = Current(format)
99+
-- if Current is empty, then just print {path}
100+
if not msg:match"[%ul]+" then
101+
msg = Current"%{filename}"
102+
end
103+
msg =
104+
Gsub(msg or "", "%s+"," ")
105+
:gsub("^%s+","")
106+
:gsub("%s+$","")
107+
-- spaces turn into newlines, this reverses that
108+
return msg
109+
end
110+
111+
if weechat then --------------------------- Weechat Section
112+
local w = weechat
113+
114+
do local name, author, version, license, description, shutdown_function, charset
115+
name = "cmus_announce"
116+
author = "nmz"
117+
version = "1"
118+
license = "0BSD"
119+
description = "Messages current buffer/channel the currently listened to song"
120+
charset = ""
121+
shutdown_function = ""
122+
w.register(name, author, version, license, description, shutdown_function, charset)
123+
end
124+
125+
-- semi global variables
126+
local OK,ERR = w.WEECHAT_RC_OK, w.WEECHAT_RC_ERROR
127+
128+
local Sub = string.sub
129+
130+
-- data, buffer, args(string)
131+
function cmus_announce(d,b,a)
132+
if IsAlive() then
133+
local msg = Sub(Display(a:match"%%{%g+}" and a),1,msgLimit) -- BUG: if its utf8 this might cut the last byte
134+
w.command(b, "/me Is Listening To: " .. msg)
135+
else
136+
w.print("","cmus_announce.lua: cmus is not running!")
137+
-- return ERR
138+
end
139+
return OK
140+
end
141+
142+
-- should be a /me
143+
do
144+
local command = "cmus_announce"
145+
local description = 'Announces currently playing song in current buffer'
146+
local args = 'format'
147+
local args_description =
148+
"passed to cmus-remote command, so look at cmus(1) for the relevant formatting"
149+
local completion = ""
150+
local callback = command
151+
local callback_data = ""
152+
w.hook_command(command, description, args, args_description, completion, callback, callback_data)
153+
end
154+
return OK
155+
end -- weechat
156+
157+
-- This is also a program which you can run as well.
158+
-- run it to test it.
159+
if ...==nil then
160+
if IsAlive() then
161+
print(os.date())
162+
print(Display())
163+
os.exit(0)
164+
end
165+
166+
os.exit(1)
167+
end

0 commit comments

Comments
 (0)