Skip to content

Commit 5d33f5e

Browse files
authored
feat: wrapped-compact renderer (#223)
1 parent 8904b8b commit 5d33f5e

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ See `:help notify-render()` for details
226226

227227
![image](https://user-images.githubusercontent.com/24252670/212632432-86621888-f885-4074-aed4-d12b5e291ab2.png)
228228

229+
5. "wrapped-compact"
230+
231+
Mostly same as `compact`, but lines are wrapped based on `max_width`, some padding is added.
232+
233+
![image](https://github.com/rcarriga/nvim-notify/assets/73286100/72237d45-6e3b-4c2a-8010-513a26871682)
234+
229235
Feel free to submit custom rendering functions to share with others!
230236

231237
### Animation Style

doc/nvim-notify.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ Built-in renderers:
208208
- `"default"`
209209
- `"minimal"`
210210
- `"simple"`
211+
- `"wrapped-compact"`
211212

212213
Custom functions should accept a buffer, a notification record and a highlights table
213214

@@ -225,4 +226,4 @@ Fields~
225226
{body} `(string)`
226227

227228

228-
vim:tw=78:ts=8:noet:ft=help:norl:
229+
vim:tw=78:ts=8:noet:ft=help:norl:

lua/notify/render/wrapped-compact.lua

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
-- alternative compact renderer for nvim-notify.
2+
-- Wraps text and adds some padding (only really to the left, since padding to
3+
-- the right is somehow not display correctly).
4+
-- Modified version of https://github.com/rcarriga/nvim-notify/blob/master/lua/notify/render/compact.lua
5+
--------------------------------------------------------------------------------
6+
7+
---@param line string
8+
---@param width number
9+
---@return string[]
10+
local function split_length(line, width)
11+
local text = {}
12+
local next_line
13+
while true do
14+
if #line == 0 then
15+
return text
16+
end
17+
next_line, line = line:sub(1, width), line:sub(width)
18+
text[#text + 1] = next_line
19+
end
20+
end
21+
22+
---@param lines string[]
23+
---@param max_width number
24+
---@return string[]
25+
local function custom_wrap(lines, max_width)
26+
local wrapped_lines = {}
27+
for _, line in pairs(lines) do
28+
local new_lines = split_length(line, max_width)
29+
for _, nl in ipairs(new_lines) do
30+
nl = nl:gsub("^%s*", " "):gsub("%s*$", " ") -- ensure padding
31+
table.insert(wrapped_lines, nl)
32+
end
33+
end
34+
return wrapped_lines
35+
end
36+
37+
---@param bufnr number
38+
---@param notif object
39+
---@param highlights object
40+
---@param config object plugin config_obj
41+
return function(bufnr, notif, highlights, config)
42+
local namespace = require("notify.render.base").namespace()
43+
local icon = notif.icon
44+
local title = notif.title[1]
45+
local prefix
46+
47+
-- wrap the text & add spacing
48+
local max_width = config.max_width()
49+
notif.message = custom_wrap(notif.message, max_width)
50+
51+
local default_titles = { "Error", "Warning", "Notify" }
52+
local has_valid_manual_title = type(title) == "string"
53+
and #title > 0
54+
and not vim.tbl_contains(default_titles, title)
55+
56+
if has_valid_manual_title then
57+
-- has title = icon + title as header row
58+
prefix = string.format(" %s %s", icon, title)
59+
table.insert(notif.message, 1, prefix)
60+
else
61+
-- no title = prefix the icon
62+
prefix = string.format(" %s", icon)
63+
notif.message[1] = string.format("%s %s", prefix, notif.message[1])
64+
end
65+
66+
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, notif.message)
67+
68+
local icon_length = vim.str_utfindex(icon)
69+
local prefix_length = vim.str_utfindex(prefix) + 1
70+
71+
vim.api.nvim_buf_set_extmark(bufnr, namespace, 0, 0, {
72+
hl_group = highlights.icon,
73+
end_col = icon_length + 1,
74+
priority = 50,
75+
})
76+
vim.api.nvim_buf_set_extmark(bufnr, namespace, 0, icon_length + 1, {
77+
hl_group = highlights.title,
78+
end_col = prefix_length + 1,
79+
priority = 50,
80+
})
81+
vim.api.nvim_buf_set_extmark(bufnr, namespace, 0, prefix_length + 1, {
82+
hl_group = highlights.body,
83+
end_line = #notif.message,
84+
priority = 50,
85+
})
86+
end

0 commit comments

Comments
 (0)