rcarriga / nvim-notify

A fancy, configurable, notification manager for NeoVim
MIT License
3.08k stars 80 forks source link

[Question] Handling Long Notifications When `max_width` is Set #129

Closed unrealapex closed 2 years ago

unrealapex commented 2 years ago

Instead of truncating very long messages if they are longer than a set max_width, would it be possible to move truncated part of a message to the next line of the notification? See an example(well it did actually occur so I'm not sure if I can call it an example anymore) situation below:

Here are some notifications with no max_width set, as you can see, this message is rather long and blocks the top portion of my screen.

image

Here are the same notifications but with max_width set to 50. Here the notification is at the preferred length but the end of the notification text is truncated. It would be nice if that truncated part was on a new line. . image

rcarriga commented 2 years ago

I wouldn't want nvim-notify to do this because messages could have specific formatting that wouldn't make sense if truncated (e.g. messages that use treesitter highlighting) but you could easily do this manually with something like this:

local function split_length(text, length)
  local lines = {}
  local next_line
  while true do
    if #text == 0 then
      return lines
    end
    next_line, text = text:sub(1, length), text:sub(length)
    lines[#lines + 1] = next_line
  end
end

vim.notify = function(msg, level, opts)
  if type(msg) == "string" then
    msg = vim.split(msg, "\n")
  end
  local truncated = {}
  for i, line in ipairs(msg) do
    local new_lines = split_length(line, max_width)
    for _, l in ipairs(t) do
      truncated[#truncated + 1] = l
    end
  end
  return require("notify")(truncated, level, opts)
end

I have not tested this so almost certainly contains bugs :sweat_smile:

unrealapex commented 2 years ago

Sorry for the late reply. I understand your reasoning behind your decision. I will try out your snippet and see if it works.

unrealapex commented 2 years ago

@rcarriga whoops, forgot about this issue. Unfortunately the snippet you provided did not work. Perhaps reopen this issue?

rcarriga commented 2 years ago

The snippet was a just a quick example of what you can do, as I said it was not tested.

chrisgrieser commented 1 year ago

had exactly the same issue. a modified version of the snippet above solved it for me.

Since it seems that wrapping is an issue regularly coming up (e.g., #137 #88 #143 ), I'd suggest simply making this an setting in the config?

I wouldn't want nvim-notify to do this because messages could have specific formatting that wouldn't make sense if truncated (e.g. messages that use treesitter highlighting)

I see the point, here, but in all my plugins / personal uses of vim.notify in my config, there is not a single instance where treesitter-highlighting is used in the notifications. (I don't even know which plugins send treesitter notices tbf 😅) So an options would be quite useful

delucca commented 1 year ago

@chrisgrieser would you mind sharing the modified snippet that actually works, please? 😄

chrisgrieser commented 1 year ago

sure, here is my nvim-notify config: https://github.com/chrisgrieser/.config/blob/main/nvim/lua/plugins/notifications.lua

rcarriga commented 1 year ago

Since it seems that wrapping is an issue regularly coming up (e.g., https://github.com/rcarriga/nvim-notify/issues/137 https://github.com/rcarriga/nvim-notify/issues/88 https://github.com/rcarriga/nvim-notify/pull/143 ), I'd suggest simply making this an setting in the config?

I can see that this is a popular request still, I'd open to PRs allowing for this behaviour behind a config option if anyone would like to open one :smile: