Skip to content

Commit

Permalink
feat: wrapped-compact renderer (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Sep 10, 2023
1 parent 8904b8b commit 5d33f5e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -226,6 +226,12 @@ See `:help notify-render()` for details

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

5. "wrapped-compact"

Mostly same as `compact`, but lines are wrapped based on `max_width`, some padding is added.

![image](https://github.com/rcarriga/nvim-notify/assets/73286100/72237d45-6e3b-4c2a-8010-513a26871682)

Feel free to submit custom rendering functions to share with others!

### Animation Style
Expand Down
3 changes: 2 additions & 1 deletion doc/nvim-notify.txt
Expand Up @@ -208,6 +208,7 @@ Built-in renderers:
- `"default"`
- `"minimal"`
- `"simple"`
- `"wrapped-compact"`

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

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


vim:tw=78:ts=8:noet:ft=help:norl:
vim:tw=78:ts=8:noet:ft=help:norl:
86 changes: 86 additions & 0 deletions lua/notify/render/wrapped-compact.lua
@@ -0,0 +1,86 @@
-- alternative compact renderer for nvim-notify.
-- Wraps text and adds some padding (only really to the left, since padding to
-- the right is somehow not display correctly).
-- Modified version of https://github.com/rcarriga/nvim-notify/blob/master/lua/notify/render/compact.lua
--------------------------------------------------------------------------------

---@param line string
---@param width number
---@return string[]
local function split_length(line, width)
local text = {}
local next_line
while true do
if #line == 0 then
return text
end
next_line, line = line:sub(1, width), line:sub(width)
text[#text + 1] = next_line
end
end

---@param lines string[]
---@param max_width number
---@return string[]
local function custom_wrap(lines, max_width)
local wrapped_lines = {}
for _, line in pairs(lines) do
local new_lines = split_length(line, max_width)
for _, nl in ipairs(new_lines) do
nl = nl:gsub("^%s*", " "):gsub("%s*$", " ") -- ensure padding
table.insert(wrapped_lines, nl)
end
end
return wrapped_lines
end

---@param bufnr number
---@param notif object
---@param highlights object
---@param config object plugin config_obj
return function(bufnr, notif, highlights, config)
local namespace = require("notify.render.base").namespace()
local icon = notif.icon
local title = notif.title[1]
local prefix

-- wrap the text & add spacing
local max_width = config.max_width()
notif.message = custom_wrap(notif.message, max_width)

local default_titles = { "Error", "Warning", "Notify" }
local has_valid_manual_title = type(title) == "string"
and #title > 0
and not vim.tbl_contains(default_titles, title)

if has_valid_manual_title then
-- has title = icon + title as header row
prefix = string.format(" %s %s", icon, title)
table.insert(notif.message, 1, prefix)
else
-- no title = prefix the icon
prefix = string.format(" %s", icon)
notif.message[1] = string.format("%s %s", prefix, notif.message[1])
end

vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, notif.message)

local icon_length = vim.str_utfindex(icon)
local prefix_length = vim.str_utfindex(prefix) + 1

vim.api.nvim_buf_set_extmark(bufnr, namespace, 0, 0, {
hl_group = highlights.icon,
end_col = icon_length + 1,
priority = 50,
})
vim.api.nvim_buf_set_extmark(bufnr, namespace, 0, icon_length + 1, {
hl_group = highlights.title,
end_col = prefix_length + 1,
priority = 50,
})
vim.api.nvim_buf_set_extmark(bufnr, namespace, 0, prefix_length + 1, {
hl_group = highlights.body,
end_line = #notif.message,
priority = 50,
})
end

0 comments on commit 5d33f5e

Please sign in to comment.