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

Code to update spinner on notify spams notifications #109

Closed
nick4tech opened this issue Oct 24, 2022 · 8 comments · Fixed by #639
Closed

Code to update spinner on notify spams notifications #109

nick4tech opened this issue Oct 24, 2022 · 8 comments · Fixed by #639
Labels
bug Something isn't working

Comments

@nick4tech
Copy link

nick4tech commented Oct 24, 2022

Describe the bug
When using the following code to handle updates to percentage value updates on an existing notification while also enabling noice, causes a spam of notify notifications

Which version of Neovim are you using?
0.8.0 in kitty terminal

To Reproduce
Steps to reproduce the behavior:

  1. Use the following code to configure notify
vim.lsp.handlers["$/progress"] = function(_, result, ctx)
   local client_id = ctx.client_id

   local val = result.value

   if not val.kind then
     return
   end

   local notif_data = get_notif_data(client_id, result.token)

   if val.kind == "begin" then
     local message = format_message(val.message, val.percentage)

     notif_data.notification = vim.notify(message, "info", {
       title = format_title(val.title, vim.lsp.get_client_by_id(client_id).name),
       icon = spinner_frames[1],
       timeout = false,
       hide_from_history = false,
     })

     notif_data.spinner = 1
     update_spinner(client_id, result.token)
   elseif val.kind == "report" and notif_data then
     notif_data.notification = vim.notify(format_message(val.message, val.percentage), "info", {
       replace = notif_data.notification,
       hide_from_history = true,
     })
   elseif val.kind == "end" and notif_data then
     notif_data.notification =
       vim.notify(val.message and format_message(val.message) or "Complete", "info", {
         icon = "",
         replace = notif_data.notification,
         timeout = 3000,
       })

     notif_data.spinner = nil
   end
  end

If the following code is commented (updates the value of the notification) the issue disappears:

   elseif val.kind == "report" and notif_data then
     notif_data.notification = vim.notify(format_message(val.message, val.percentage), "info", {
       replace = notif_data.notification,
       hide_from_history = true,
     })
  1. Enable noice

Expected Behavior
Receive a single notification that updates as the percentage increases

Screenshots
Espected behavior:
image

Actual behavior:
image

@nick4tech nick4tech added the bug Something isn't working label Oct 24, 2022
@folke
Copy link
Owner

folke commented Oct 24, 2022

What's your Noice config?

@nick4tech
Copy link
Author

  use {
    'folke/noice.nvim',
    event = "VimEnter",
    requires = {
      -- if you lazy-load any plugin below, make sure to add proper `module="..."` entries
      "MunifTanjim/nui.nvim",
      "rcarriga/nvim-notify",
      "hrsh7th/nvim-cmp",
    },
    config = require("noice").setup({
      views = {
        cmdline_popup = {
          border = {
            style = 'rounded',
            padding = { 0, 1 },
          },
          position = {
            row = 5,
            col = "50%",
          },
          size = {
            width = 120,
            height = "auto",
          },
          -- filter_options = {},
          -- win_options = {
            -- winhighlight = "NormalFloat:NormalFloat,FloatBorder:FloatBorder",
          -- },
        },
        popupmenu = {
          enabled = false
        },
      },
    })
  }

@folke folke closed this as completed in 0b0e8cf Oct 24, 2022
@folke
Copy link
Owner

folke commented Oct 24, 2022

Should be fixed now. Let me know if you have any more issues.

@nick4tech
Copy link
Author

I synced the last commit and it's still happening 🙈

@folke folke reopened this Oct 24, 2022
@folke
Copy link
Owner

folke commented Oct 24, 2022

Can you share all of that code, so I can test your code to see what's wrong?

@folke
Copy link
Owner

folke commented Oct 24, 2022

This test script works

local notif = nil
local pct = 0

local function update()
  notif = vim.notify(("%d%%"):format(pct), "testing", {
    replace = notif,
  })
  pct = pct + 10
  if pct <= 100 then
    vim.defer_fn(update, 200)
  end
end

update()

@nick4tech
Copy link
Author

Sure, here's my complete notify / noice config:

  use 'rcarriga/nvim-notify'

  require('notify').setup{}
  vim.notify = require('notify')

  require("telescope").load_extension("notify")

  -- Config:
  -- Utility functions shared between progress reports for LSP and DAP
  local client_notifs = {}

  local function get_notif_data(client_id, token)
   if not client_notifs[client_id] then
     client_notifs[client_id] = {}
   end

   if not client_notifs[client_id][token] then
     client_notifs[client_id][token] = {}
   end

   return client_notifs[client_id][token]
  end


  local spinner_frames = { "", "", "", "", "", "", "", "" }

  local function update_spinner(client_id, token)
   local notif_data = get_notif_data(client_id, token)

   if notif_data.spinner then
     local new_spinner = (notif_data.spinner + 1) % #spinner_frames
     notif_data.spinner = new_spinner

     notif_data.notification = vim.notify(nil, nil, {
       hide_from_history = true,
       icon = spinner_frames[new_spinner],
       replace = notif_data.notification,
     })

     vim.defer_fn(function()
       update_spinner(client_id, token)
     end, 100)
   end
  end

  local function format_title(title, client_name)
   return client_name .. (#title > 0 and ": " .. title or "")
  end

  local function format_message(message, percentage)
   return (percentage and percentage .. "%\t" or "") .. (message or "")
  end


  -- LSP integration
  -- Make sure to also have the snippet with the common helper functions in your config!

  vim.lsp.handlers["$/progress"] = function(_, result, ctx)
   local client_id = ctx.client_id

   local val = result.value

   if not val.kind then
     return
   end

   local notif_data = get_notif_data(client_id, result.token)

   if val.kind == "begin" then
     local message = format_message(val.message, val.percentage)

     notif_data.notification = vim.notify(message, "info", {
       title = format_title(val.title, vim.lsp.get_client_by_id(client_id).name),
       icon = spinner_frames[1],
       timeout = false,
       hide_from_history = false,
     })

     notif_data.spinner = 1
     update_spinner(client_id, result.token)
   elseif val.kind == "report" and notif_data then
     notif_data.notification = vim.notify(format_message(val.message, val.percentage), "info", {
       replace = notif_data.notification,
       hide_from_history = false,
     })
   elseif val.kind == "end" and notif_data then
     notif_data.notification =
       vim.notify(val.message and format_message(val.message) or "Complete", "info", {
         icon = "",
         replace = notif_data.notification,
         timeout = 3000,
       })

     notif_data.spinner = nil
   end
  end

  -- LSP Messages
  -- table from lsp severity to vim severity.
  local severity = {
    "error",
    "warn",
    "info",
    "info", -- map both hint and info to info?
  }
  vim.lsp.handlers["window/showMessage"] = function(method, params)
    vim.notify(method.message, severity[params.type])
  end

  use 'folke/noice.nvim'
  use "MunifTanjim/nui.nvim"
  use {
    'folke/noice.nvim',
    event = "VimEnter",
    requires = {
      -- if you lazy-load any plugin below, make sure to add proper `module="..."` entries
      "MunifTanjim/nui.nvim",
      "rcarriga/nvim-notify",
      "hrsh7th/nvim-cmp",
    },
    config = require("noice").setup({
      views = {
        cmdline_popup = {
          border = {
            style = 'rounded',
            padding = { 0, 1 },
          },
          position = {
            row = 5,
            col = "50%",
          },
          size = {
            width = 120,
            height = "auto",
          },
          -- filter_options = {},
          -- win_options = {
            -- winhighlight = "NormalFloat:NormalFloat,FloatBorder:FloatBorder",
          -- },
        },
        popupmenu = {
          enabled = false
        },
      },
    })
  }

@folke folke closed this as completed in 83b60f2 Oct 25, 2022
@folke
Copy link
Owner

folke commented Oct 25, 2022

Should be fixed for all cases now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants