59 lines
1.6 KiB
Lua
59 lines
1.6 KiB
Lua
|
#!/usr/bin/lua
|
||
|
|
||
|
json = require("dkjson")
|
||
|
posix = require("posix")
|
||
|
|
||
|
function htmlescape(s)
|
||
|
s = string.gsub(s, "&", "&")
|
||
|
s = string.gsub(s, "<", "<")
|
||
|
s = string.gsub(s, ">", ">")
|
||
|
return s
|
||
|
end
|
||
|
|
||
|
function timefmt(n)
|
||
|
local s = n % 60
|
||
|
local m = math.floor(n / 60)
|
||
|
return string.format("%d:%02d", m, s)
|
||
|
end
|
||
|
|
||
|
function output(text, tooltip)
|
||
|
text = htmlescape(text)
|
||
|
tooltip = htmlescape(tooltip)
|
||
|
print(json.encode{text=text, tooltip=tooltip, class="custom-cmus"})
|
||
|
end
|
||
|
|
||
|
function getstat(status, name)
|
||
|
for _, line in ipairs(status) do
|
||
|
if string.match(line, "^" .. name) then
|
||
|
return string.sub(line, string.len(name)+2)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
while true do
|
||
|
local pipe = io.popen("cmus-remote -Q")
|
||
|
local status = {}
|
||
|
for line in pipe:lines() do
|
||
|
table.insert(status, line)
|
||
|
end
|
||
|
local success = pipe:close()
|
||
|
if not success then
|
||
|
output(" Not running", "Not running")
|
||
|
elseif getstat(status, "status") == "stopped" then
|
||
|
output(" Not running", "Not running")
|
||
|
else
|
||
|
local playing = getstat(status, "status")
|
||
|
local symbol = ({playing="", paused=""})[playing]
|
||
|
local title = getstat(status, "tag title")
|
||
|
local artist = getstat(status, "tag artist")
|
||
|
local duration = getstat(status, "duration")
|
||
|
local position = getstat(status, "position")
|
||
|
local text = string.format("%s %s (%s)", symbol, title, timefmt(position))
|
||
|
local tooltip = string.format("%s - %s (%s / %s)",
|
||
|
artist, title, timefmt(position), timefmt(duration)
|
||
|
)
|
||
|
output(text, tooltip)
|
||
|
end
|
||
|
posix.sleep(1)
|
||
|
end
|