Skip to content

Commit 5116ed6

Browse files
committed
Copy ellipsize function from Spotify widget
1 parent 3bb3d56 commit 5116ed6

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

cmus-widget/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ awful.key({ }, "XF86AudioPlay", function () cmus_widget:play()
3636
awful.key({ }, "XF86AudioPause", function () cmus_widget:play() end, {description = "pause track", group = "cmus"}),
3737
awful.key({ }, "XF86AudioNext", function () cmus_widget:next_track() end, {description = "next track", group = "cmus"}),
3838
awful.key({ }, "XF86AudioPrev", function () cmus_widget:prev_track() end, {description = "previous track", group = "cmus"}),
39-
awful.key({ }, "XF86AudioStop", function () cmus_widget:stop() end, {description = "stop cmus", group = "cmus"}),
39+
awful.key({ }, "XF86AudioStop", function () cmus_widget:stop() end, {description = "stop track", group = "cmus"}),
4040
```
4141

4242
## Customization
@@ -50,4 +50,5 @@ It is possible to customize the widget by providing a table with all or some of
5050
| `font` | `beautiful.font` | Font name and size, like `Play 12` |
5151
| `path_to_icons` | `/usr/share/icons/Arc/actions/symbolic/` | Alternative path for the icons |
5252
| `timeout`| `10` | Refresh cooldown |
53+
| `max_length` | `30` | Maximum lentgh of title. Text will be ellipsized if longer. |
5354
| `space` | `3` | Space between icon and track title |

cmus-widget/cmus.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ local watch = require("awful.widget.watch")
1212
local spawn = require("awful.spawn")
1313
local beautiful = require('beautiful')
1414

15+
local function ellipsize(text, length)
16+
-- utf8 only available in Lua 5.3+
17+
if utf8 == nil then
18+
return text:sub(0, length)
19+
end
20+
return (utf8.len(text) > length and length > 0)
21+
and text:sub(0, utf8.offset(text, length - 2) - 1) .. '...'
22+
or text
23+
end
24+
1525
local cmus_widget = {}
1626

1727
local function worker(user_args)
@@ -21,6 +31,7 @@ local function worker(user_args)
2131

2232
local path_to_icons = args.path_to_icons or "/usr/share/icons/Arc/actions/symbolic/"
2333
local timeout = args.timeout or 10
34+
local max_length = args.max_length or 30
2435
local space = args.space or 3
2536

2637
cmus_widget.widget = wibox.widget {
@@ -86,7 +97,7 @@ local function worker(user_args)
8697
widget:update_icon("media-playback-stop-symbolic.svg")
8798
end
8899

89-
widget:set_title(title)
100+
widget:set_title(ellipsize(title, max_length))
90101
widget.visible = true
91102
else
92103
widget.visible = false

0 commit comments

Comments
 (0)