Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 36 additions & 38 deletions lua/notify/render/wrapped-compact.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- 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
-- WRAPS TEXT AND ADDS SOME PADDING.
--------------------------------------------------------------------------------

---@param line string
Expand All @@ -14,7 +11,7 @@ local function split_length(line, width)
if #line == 0 then
return text
end
next_line, line = line:sub(1, width), line:sub(width)
next_line, line = line:sub(1, width), line:sub(width + 1)
text[#text + 1] = next_line
end
end
Expand All @@ -23,12 +20,12 @@ end
---@param max_width number
---@return string[]
local function custom_wrap(lines, max_width)
local right_pad = " "
local wrapped_lines = {}
for _, line in pairs(lines) do
local new_lines = split_length(line, max_width)
local new_lines = split_length(line, max_width - #right_pad)
for _, nl in ipairs(new_lines) do
nl = nl:gsub("^%s*", " "):gsub("%s*$", " ") -- ensure padding
table.insert(wrapped_lines, nl)
table.insert(wrapped_lines, nl:gsub("^%s+", "") .. right_pad)
end
end
return wrapped_lines
Expand All @@ -41,55 +38,56 @@ end
return function(bufnr, notif, highlights, config)
local namespace = require("notify.render.base").namespace()
local icon = notif.icon
local icon_length = #icon
local prefix = ""
local prefix_length = 0
local message = custom_wrap(notif.message, config.max_width() or 80)
local title = notif.title[1]
local prefix

-- wrap the text & add spacing
local max_width = config.max_width()
if max_width == nil then
max_width = 80
end
local 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)
prefix = string.format("%s %s ", icon, title)
if notif.duplicates then
prefix = string.format('%s x%d', prefix, #notif.duplicates)
end
prefix_length = #prefix + 2
table.insert(message, 1, prefix)
else
-- no title = prefix the icon
prefix = string.format(" %s", icon)
if notif.duplicates then
prefix = string.format('%s x%d', prefix, #notif.duplicates)
end
message[1] = string.format("%s %s", prefix, message[1])
end

message[1] = " " .. message[1]
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, message)

local icon_length = #icon
local prefix_length = #prefix + 1
if has_valid_manual_title then
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,
})
end

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 = #message,
priority = 50,
})

-- padding to the left/right
for ln = 1, #message do
vim.api.nvim_buf_set_extmark(bufnr, namespace, ln, 0, {
virt_text = { { " ", highlights.body } },
virt_text_pos = "inline",
})
vim.api.nvim_buf_set_extmark(bufnr, namespace, ln, 0, {
virt_text = { { " ", highlights.body } },
virt_text_pos = "right_align",
})
end
end