|
| 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