kristijanhusak / vim-dadbod-ui

Simple UI for https://github.com/tpope/vim-dadbod
MIT License
1.54k stars 94 forks source link

Possible to Add Option for Nvim-Notify to Track Query Time? #212

Closed jtw023 closed 11 months ago

jtw023 commented 12 months ago

I was looking at this December 2021 feature request from the nvim-notify repo and thought it would be kind of cool to have notify also handle tracking the query length since all my other notifications are routed through there(see below screenshot). I gave it a shot using that update function to make use of the spinner frames shown in the video but I just don't know enough vimscript/lua. Figured I would swing by to see if anyone else is interested in this or had the time to make it happen.

Screenshot 2023-11-27 at 11 30 55 PM

jtw023 commented 11 months ago

In order to get some practice I decided to change the "Execute query" time format - see screenshot. Not sure if this has been asked for but let me know if you'd like a pull request for this one. Could probably make it into a flag that is set in your config. Haven't made any progress on turning this into a notify alert. Screenshot 2023-11-28 at 6 50 04 PM

jtw023 commented 11 months ago

Closed by mistake. Reopening issue.

kristijanhusak commented 11 months ago

The current window works in both Vim and Neovim. You can see how it's implemented starting from here.

I would suggest creating a separate small plugin that would do this, since it's more straightforward. You can hook into same autocommands like it's done here, and you can remove these by overriding autocmds in the augroup, or we can add an option here to hide the progress if the user does not want to see it.

I'm trying to keep the plugin compatible with both Vim and Neovim since vim-dadbod does the same, so having some implementations that are strictly supported for one or another is out of scope. Using vim.notify was an exception because it does not depend on any other plugin, but since you need to use update from nvim-notify, it creates a dependency on that plugin.

jtw023 commented 11 months ago

Ok, thanks! I'll play around with it over the next few weeks and report back when I get something to show.

jiz4oh commented 3 months ago
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 function format_message(number)
  return 'Executing query - ' .. tostring(number) .. 's'
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
    local start_time = notif_data.notification.start_time
    local current_time = vim.uv.clock_gettime('monotonic')
    local interval = (current_time.sec * 1e9 + current_time.nsec - start_time.sec * 1e9 - start_time.nsec) / 1e9
    local message = format_message(interval)

    notif_data.notification = vim.notify(message, "info", {
      hide_from_history = true,
      icon = spinner_frames[new_spinner],
      replace = notif_data.notification,
    })
    notif_data.notification.start_time = start_time

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

vim.api.nvim_create_autocmd('User', {
  pattern = {"DBQueryPre", "*DBExecutePre"},
  callback = function(event)
    local token = event.buf

    local notif_data = get_notif_data('dadbod-ui', token)

    local message = format_message(0.0)

    notif_data.notification =
      vim.notify(message, "info", {
        title = '[DBUI]',
        icon = spinner_frames[1],
        timeout = false,
        hide_from_history = false,
      })
    notif_data.notification.start_time = vim.uv.clock_gettime('monotonic')

    notif_data.spinner = 1
    update_spinner('dadbod-ui', token)
  end
})

vim.api.nvim_create_autocmd('User', {
  pattern = {"DBQueryPost", "*DBExecutePost"},
  callback = function(event)
    local token = event.buf

    local notif_data = get_notif_data('dadbod-ui', token)

    notif_data.notification =
      vim.notify(nil, nil, {
        hide_from_history = true,
        icon = "",
        replace = notif_data.notification,
        timeout = 0,
      })

    notif_data.spinner = nil
  end
})

https://github.com/user-attachments/assets/eddff7f6-9eff-4a3a-bcc7-5a14d1330ac8

Hi, I have implemented this by lua. But there is a message Executing query ... triggered at the same time by dadbod-ui if I set let g:db_ui_use_nvim_notify = 1 and even if let g:db_ui_disable_progress_bar = 1. it's trigged by here.

image

@kristijanhusak, could you help revise this message? and I also noticed that there are two autocmd for notifications