summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAugusto Gunsch <augustogunsch@tutanota.com>2022-01-23 21:41:44 -0300
committerAugusto Gunsch <augustogunsch@tutanota.com>2022-01-23 21:41:44 -0300
commit56c8e3439b43f819f7a73bd075737ce93d38e5ef (patch)
tree9e9261905165b9294f8242a95acc837c52306b69
parentac49ade9951b591e859a470803e166acfa2b7bf2 (diff)
Add cmus widget
-rw-r--r--cmus-widget/README.md48
-rw-r--r--cmus-widget/cmus-widget.pngbin0 -> 6959 bytes
-rw-r--r--cmus-widget/cmus.lua127
3 files changed, 175 insertions, 0 deletions
diff --git a/cmus-widget/README.md b/cmus-widget/README.md
new file mode 100644
index 0000000..c24d975
--- /dev/null
+++ b/cmus-widget/README.md
@@ -0,0 +1,48 @@
+# Cmus widget
+
+Cmus widget that shows the current playing track.
+
+![widget](cmus-widget.png)
+
+Left click toggles playback.
+
+## Installation
+
+Clone the repo under **~/.config/awesome/** and add widget in **rc.lua**:
+
+```lua
+local cmus_widget = require('awesome-wm-widgets.cmus-widget.cmus')
+...
+s.mytasklist, -- Middle widget
+ { -- Right widgets
+ layout = wibox.layout.fixed.horizontal,
+ ...
+ -- default
+ cmus_widget(),
+ -- customized
+ cmus_widget{
+ space = 5,
+ timeout = 5
+ },
+```
+
+### Shortcuts
+
+To improve responsiveness of the widget when playback is changed by a shortcut use corresponding methods of the widget:
+
+```lua
+awful.key({ modkey, "Shift" }, "p", function () cmus_widget:play_pause() end, {description = "play/pause cmus", group = "custom"}),
+```
+
+## Customization
+
+It is possible to customize the widget by providing a table with all or some of the following config parameters:
+
+### Generic parameter
+
+| Name | Default | Description |
+|---|---|---|
+| `font` | `Play 9` | Font used for the track title |
+| `path_to_icons` | `/usr/share/icons/Arc/actions/symbolic/` | Alternative path for the icons |
+| `timeout`| `10` | Refresh cooldown |
+| `space` | `3` | Space between icon and track title |
diff --git a/cmus-widget/cmus-widget.png b/cmus-widget/cmus-widget.png
new file mode 100644
index 0000000..ba04401
--- /dev/null
+++ b/cmus-widget/cmus-widget.png
Binary files differ
diff --git a/cmus-widget/cmus.lua b/cmus-widget/cmus.lua
new file mode 100644
index 0000000..51daa89
--- /dev/null
+++ b/cmus-widget/cmus.lua
@@ -0,0 +1,127 @@
+-------------------------------------------------
+-- Cmus Widget for Awesome Window Manager
+-- Show what's playing, play/pause, etc
+
+-- @author Augusto Gunsch
+-- @copyright 2022 Augusto Gunsch
+-------------------------------------------------
+
+local awful = require("awful")
+local wibox = require("wibox")
+local watch = require("awful.widget.watch")
+local spawn = require("awful.spawn")
+local naughty = require("naughty")
+
+local cmus_widget = {}
+
+local function show_warning(message)
+ naughty.notify{
+ preset = naughty.config.presets.critical,
+ title = "Cmus Widget",
+ text = message}
+end
+
+local function worker(user_args)
+
+ local args = user_args or {}
+ local font = args.font or "Play 9"
+
+ local path_to_icons = args.path_to_icons or "/usr/share/icons/Arc/actions/symbolic/"
+ local timeout = args.timeout or 10
+ local space = args.space or 3
+
+ cmus_widget.widget = wibox.widget {
+ {
+ {
+ id = "playback_icon",
+ resize = false,
+ widget = wibox.widget.imagebox,
+ },
+ layout = wibox.container.place
+ },
+ {
+ id = "text",
+ font = font,
+ widget = wibox.widget.textbox
+ },
+ layout = wibox.layout.fixed.horizontal,
+ update_icon = function(self, name)
+ self:get_children_by_id("playback_icon")[1]:set_image(path_to_icons .. name)
+ end,
+ set_title = function(self, title)
+ self:get_children_by_id("text")[1]:set_text(title)
+ end
+ }
+
+ function update_widget(widget, stdout, _, _, code)
+ if code == 0 then
+ local cmus_info = {}
+
+ for s in stdout:gmatch("[^\r\n]+") do
+ local key, val = string.match(s, "^tag (%a+) (.+)$")
+
+ if key and val then
+ cmus_info[key] = val
+ else
+ local key, val = string.match(s, "^set (%a+) (.+)$")
+
+ if key and val then
+ cmus_info[key] = val
+ else
+ local key, val = string.match(s, "^(%a+) (.+)$")
+ if key and val then
+ cmus_info[key] = val
+ end
+ end
+ end
+ end
+
+ local title = cmus_info.title
+
+ if not title and cmus_info.file then
+ title = cmus_info.file:gsub("%..-$", "")
+ title = title:gsub("^.+/", "")
+ end
+
+ if title then
+ if cmus_info["status"] == "playing" then
+ widget:update_icon("media-playback-start-symbolic.svg")
+ elseif cmus_info["status"] == "paused" then
+ widget:update_icon("media-playback-pause-symbolic.svg")
+ else
+ widget:update_icon("media-playback-stop-symbolic.svg")
+ end
+
+ widget:set_title(title)
+ widget.visible = true
+ else
+ widget.visible = false
+ widget.width = 0
+ end
+ else
+ widget.visible = false
+ end
+ end
+
+ function cmus_widget:play_pause()
+ spawn("cmus-remote -u")
+ spawn.easy_async("cmus-remote -Q",
+ function(stdout, _, _, code)
+ update_widget(cmus_widget.widget, stdout, _, _, code)
+ end)
+ end
+
+ cmus_widget.widget:buttons(
+ awful.util.table.join(
+ awful.button({}, 1, function() cmus_widget:play_pause() end)
+ )
+ )
+
+ watch("cmus-remote -Q", timeout, update_widget, cmus_widget.widget)
+
+ return cmus_widget.widget
+end
+
+return setmetatable(cmus_widget, { __call = function(_, ...)
+ return worker(...)
+end })