NStefan002 / screenkey.nvim

Screencast your keys in Neovim
MIT License
291 stars 5 forks source link

Send user event when updating screenkey #26

Closed nicolas-goudry closed 2 months ago

nicolas-goudry commented 3 months ago

Problem

I have a fully custom statusline using heirline and would like to show screenkey in it. In order to do so, I need to be able to tell heirline when it should update the component which displays screenkey, which is currently not possible (afaict).

Expected behavior

I think using a user event would be the best way to handle this use case. The change should be quite small and would look something like this:

diff --git a/lua/screenkey/init.lua b/lua/screenkey/init.lua
index cd5b7f3..ea13c2c 100644
--- a/lua/screenkey/init.lua
+++ b/lua/screenkey/init.lua
@@ -68,6 +68,7 @@ local function create_timer()
                 if active then
                     clear_screenkey_buffer()
                 end
+                vim.api.nvim_exec_autocmds("User", { pattern = "ScreenkeyChange" })
             end
         end)
     )
@@ -288,6 +289,7 @@ vim.on_key(function(key, typed)
     if active then
         display_text()
     end
+    vim.api.nvim_exec_autocmds("User", { pattern = "ScreenkeyChange" })
 end, ns_id)

 ---@param opts? screenkey.config

Then, in heirline the component would look like this:

{
  provider = function() return screenkey.get_keys() end,
  update = {
    "User",
    pattern = "ScreenkeyChange",
    callback = vim.schedule_wrap(function() vim.cmd("redrawstatus") end),
  },
}

I actually tested this implementation and it works as expected (with a few changes needed to the heirline component to make it look cleaner). If the change is ok as is, I would be willing to open a PR with these changes.

NStefan002 commented 2 months ago

Thanks for the suggestion!

If the change is ok as is, I would be willing to open a PR with these changes.

Before you create a PR, I'd like to ask one question, since I don't know much about nvim_exec_autocmds. Does sending events every keypress affect the performance of screenkey and/or neovim? (I guess not but I had to ask)

nicolas-goudry commented 2 months ago

I’m not aware of any performance implication related to executing a fair amount of autocommands in a short time period. I couldn’t find anything online, nor in the documentation. All I can say is that it works well in my setup!

I’ll let someone more experienced answer this question…

If you fear bad perf, maybe this could be an opt-in option?

NStefan002 commented 2 months ago

I just tested it and I'm not experiencing any issues. If you want you can open up a PR, and if someone reports performance-related problems I can make this feature opt-in (or you can do it right away in the PR, if you're interested).

I think ScreenkeyChanged should only be fired if vim.g.screenkey_statusline_component is true so you can toggle your status line component with :Screenkey toggle_statusline_component or with vim.g.screenkey_statusline_component = not vim.g.screenkey_statusline_component. Can you check if that approach works with heirline since I don't use it?

One last thing: it would be awesome if you included an example of setting up heirline component in the README (you don't have to add it to doc/screenkey.nvim.txt it will be autogenerated from the README).

nicolas-goudry commented 2 months ago

Neat! I will open a PR shortly.