diff options
| author | Tyler Murphy <tylerm@tylerm.dev> | 2023-09-04 23:21:01 -0400 |
|---|---|---|
| committer | Tyler Murphy <tylerm@tylerm.dev> | 2023-09-04 23:21:01 -0400 |
| commit | db49f683129771d95828b01594c69431a717e8e8 (patch) | |
| tree | d2cb1c0b865e4d81ce81f9a3176b8ad93a864950 /home-config/waybar/custom_cmus.lua | |
| download | dotfiles-guix-db49f683129771d95828b01594c69431a717e8e8.tar.gz dotfiles-guix-db49f683129771d95828b01594c69431a717e8e8.tar.bz2 dotfiles-guix-db49f683129771d95828b01594c69431a717e8e8.zip | |
guix
Diffstat (limited to '')
| -rwxr-xr-x | home-config/waybar/custom_cmus.lua | 58 |
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, "<", "<") + 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 |