Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set notification frequence or possibly a DND mode? #210

Open
2KAbhishek opened this issue Jul 6, 2023 · 3 comments
Open

Set notification frequence or possibly a DND mode? #210

2KAbhishek opened this issue Jul 6, 2023 · 3 comments

Comments

@2KAbhishek
Copy link

In cases where a plugin does multiple notify calls, is it possible to set the frequency at which notifications can be shown?

e.g - Show notification after every 15 mins

Another approach would be to add something like a :DNDToggle command that disables and enables notifications

@2KAbhishek
Copy link
Author

Asking cause this is a very common issue that hampers workflow focus sometimes, lsp being the most noisy

e.g:

image

@rcarriga
Copy link
Owner

The API is meant to be quite simple and to be extended by users.
You can implement a do-not-disturb mode quite easily:

local notify = require("notify")

local dnd = false
vim.api.nvim_create_user_command("NotifyDND", function()
  dnd = not dnd
end)

vim.notify = function(...)
  if not dnd then
    notify(...)
  end
end

but blanket silencing might be overkill.
You could instead use the replace functionality to prevent noisy messages
showing more than once at a time:

local notify = require("notify")

local buffered_messages = {
  "Client %d+ quit",
}
local message_notifications = {}

vim.notify = function(msg, level, opts)
  opts = opts or {}
  for _, pattern in ipairs(buffered_messages) do
    if string.find(msg, pattern) then

      if message_notifications[pattern] then
        opts.replace = message_notifications[pattern]
      end

      opts.on_close = function()
        message_notifications[pattern] = nil
      end
      message_notifications[pattern] = notify.notify(msg, level, opts)
      return
    end
  end

  notify.notify(msg, level, opts)
end

and if you wanted the noisy messages to also have a timeout:

local notify = require("notify")

local MIN = 1000 * 60 * 60

local buffered_messages = {
  ["Client %d+ quit"] = false
}
local message_notifications = {}

vim.notify = function(msg, level, opts)
  opts = opts or {}

  for pattern, is_visible in pairs(buffered_messages) do
    if string.find(msg, pattern) then
      if message_notifications[pattern] then
        if not is_visible then
          return
        end
        opts.replace = message_notifications[pattern]
      end

      buffered_messages[pattern] = true
      opts.on_close = function()
        buffered_messages[pattern] = false
        vim.defer_fn(function()
          message_notifications[pattern] = nil
        end, MIN * 15)
      end
      message_notifications[pattern] = notify.notify(msg, level, opts)
      return
    end
  end

  notify.notify(msg, level, opts)
end

Those are just for example, there are lots of small customisations you could
make which is why keeping it outside of this plugin is preferable 😄

@2KAbhishek
Copy link
Author

Thanks a lot for the answer, I'll try it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants