summaryrefslogtreecommitdiff
path: root/home-config/waybar/custom_cmus.lua
diff options
context:
space:
mode:
Diffstat (limited to 'home-config/waybar/custom_cmus.lua')
-rwxr-xr-xhome-config/waybar/custom_cmus.lua58
1 files changed, 58 insertions, 0 deletions
diff --git a/home-config/waybar/custom_cmus.lua b/home-config/waybar/custom_cmus.lua
new file mode 100755
index 0000000..d03eb9c
--- /dev/null
+++ b/home-config/waybar/custom_cmus.lua
@@ -0,0 +1,58 @@
+#!/usr/bin/lua
+
+json = require("dkjson")
+posix = require("posix")
+
+function htmlescape(s)
+ s = string.gsub(s, "&", "&")
+ s = string.gsub(s, "<", "&lt;")
+ s = string.gsub(s, ">", "&gt;")
+ 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