Skip to content

Conversation

treatmesubj
Copy link
Contributor

It's nice to have a winbar for no-name buffers, such as when you pipe stdout to nvim like kubectl get pod worker-0 -o yaml | nvim -c "set ft=yaml"

@cuducos
Copy link
Owner

cuducos commented Dec 24, 2024

Nice, I love that!

Let me just share a bit of my thoughts on the docs in the README.md but they are absolutely non-blocking — I am merging this PR, just want to make sure we get the best out of it.

My point is: I try to keep the examples in the docs as simple as possible. Beginners feel oh, that simple? nice!, while more experienced users probably would know how to use those building blocks to take that to another level. And I am not saying I get that all the time, this is just my mindset, what I try to do.

So, reading your PR I feel like this "." .. (require("yaml_nvim").get_yaml_key() or "") carries a lot and is not that beginner-friendly. So I was thinking: what about keeping the existing example and adding to it, instead of replacing it?

This way we could have a simpler require("yaml_nvim").get_yaml_key() in one example, and your great contribution next. What do you think?

@treatmesubj
Copy link
Contributor Author

oh yeah, whoops - yep, makes sense to me.

@cuducos
Copy link
Owner

cuducos commented Dec 25, 2024

Thnk you so much : )

@cuducos cuducos merged commit 01e643a into cuducos:main Dec 25, 2024
4 checks passed
@treatmesubj treatmesubj deleted the patch-1 branch December 25, 2024 21:38
@treatmesubj
Copy link
Contributor Author

hey @cuducos since I'm back at work, I actually used this in my workflow for a bit and noticed it's a little goofy and not as robust as it needs to be. Only on FileType (set ft=yaml) events is the nested CursorMoved autocmd to update the winbar created, but then the CursorMoved autocmd persists for all other buffers afterward, even if they're not yaml buffers, which is silly.

So given this goofy side-effect, maybe you want to revert this PR.

Here's the more involved config to make it truly work as I intended:

vim.api.nvim_create_autocmd({ "BufEnter", "FileType" }, {
  group = vim.api.nvim_create_augroup("bufent_winbar", { clear = true }),
  callback = function(opts)
    -- https://github.com/cuducos/yaml.nvim
    if vim.bo[opts.buf].filetype == "yaml" then
      vim.api.nvim_create_autocmd({ "CursorMoved" }, {
        group = vim.api.nvim_create_augroup("curs_winbar", { clear = true }),
        callback = function()
          vim.opt_local.winbar = "." .. (require("yaml_nvim").get_yaml_key() or "")
        end,
      })
    -- https://github.com/phelipetls/jsonpath.nvim
    elseif vim.bo[opts.buf].filetype == "json" then
      vim.api.nvim_create_autocmd({ "CursorMoved" }, {
        group = vim.api.nvim_create_augroup("curs_winbar", { clear = true }),
        callback = function()
          vim.opt_local.winbar = "." .. (require("jsonpath").get():sub(2) or "")
        end,
      })
    else
      vim.opt_local.winbar = ""
      vim.api.nvim_create_augroup("curs_winbar", { clear = true })
    end
  end,
})

On every BufEnter or FileType event, CursorMoved winbar autocmd is only created if necessary (for the filetype) and cleared if not via augroup.
This way, under the hood, when you switch to other non-yaml buffers, nvim isn't needlessly tracking cursor movements and setting empty winbars for those buffers.
And most importantly if you have some other winbar stuff for other buffers, you won't get weird effects; you can hop around panes/buffers between yaml, json, or other, and winbar acts as you'd expect for each.

Happy to open another PR w/ something like below, if you'd like.

vim.api.nvim_create_autocmd({ "BufEnter", "FileType" }, {
  group = vim.api.nvim_create_augroup("bufent_winbar", { clear = true }),
  callback = function(opts)
    if vim.bo[opts.buf].filetype == "yaml" then
      vim.api.nvim_create_autocmd({ "CursorMoved" }, {
        group = vim.api.nvim_create_augroup("curs_winbar", { clear = true }),
        callback = function()
          vim.opt_local.winbar = require("yaml_nvim").get_yaml_key_and_value()
        end,
      })
    else
      vim.opt_local.winbar = ""
      vim.api.nvim_create_augroup("curs_winbar", { clear = true })
    end
  end,
})

@cuducos
Copy link
Owner

cuducos commented Jan 3, 2025

Ah, got it! I'll sort that out, but feel free to edit later to enhance it!

cuducos added a commit that referenced this pull request Jan 3, 2025
This reverts commit 01e643a, reversing
changes made to 1c1096a.
cuducos added a commit that referenced this pull request Jan 3, 2025
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

Successfully merging this pull request may close these issues.

2 participants