nvim-gps icon indicating copy to clipboard operation
nvim-gps copied to clipboard

Support show on winbar

Open leaxoy opened this issue 2 years ago • 17 comments

https://github.com/neovim/neovim/pull/17336 winbar ui has been merged into nightly, it's general to show the gps component, there is vscode example: image and neovim example: image

leaxoy avatar May 19 '22 07:05 leaxoy

You can simply configure winbar to use nvim-gps. nvim-gps is a statusline component, so how to use it for winbar, statusline, tabline, etc., is up to users or other plugins.

For example:

_G.gps_location = function()
  local gps = require "nvim-gps"
  return gps.is_available() and gps.get_location() or ""
end
vim.opt.winbar = "%{%v:lua.gps_location()%}"

Or via your favorite statusline/tabline plugin.

wookayin avatar May 19 '22 17:05 wookayin

@wookayin Thanks for the code. However, winbar won't be updated automatically like what statusline does when we move cursor, so it seems we need to use autocmd CursorMoved * to update it.

rockyzhang24 avatar May 19 '22 17:05 rockyzhang24

However, winbar won't be updated automatically as statusline when we move cursor, so it seems we need to use autocmd CursorMoved * to update it.

@rockyzhang24 No, that's not correct and the above code works as you'd expect. Did you try them out?%{...%} expression means that the function will be called whenever needed (including after cursor update). Read the documentation::help statusline

wookayin avatar May 19 '22 19:05 wookayin

@wookayin Yes, I know it works as expected and no bugs for statusline. I've been using this plugin for a long time.

Confirmed that this is a bug of the newly added winbar(see this https://github.com/neovim/neovim/pull/17336#issuecomment-1132085879). I already opened an issue in the neovim repo (https://github.com/neovim/neovim/issues/18645)

So far, until this winbar bug is fixed, we should use a piece of code like this:

_G.gps_location = function()
  local gps = require "nvim-gps"
  return gps.is_available() and gps.get_location() or ""
end

vim.opt.winbar = "%{%v:lua.gps_location()%}"

vim.api.nvim_create_autocmd ("CursorMoved", {
  pattern = '*',
  command = "set winbar=%{%v:lua.gps_location()%}",
})

rockyzhang24 avatar May 19 '22 19:05 rockyzhang24

Corner case, the winbar always shows even gps is not available.

Correct case image

Wrong case image

leaxoy avatar May 20 '22 04:05 leaxoy

@leaxoy Use my code above and it works well currently. Because winbar won't be updated when you move your cursor (it's a bug and the PR solving it will be merged soon), thus you need the autocmd to refresh it. When that mentioned issue gets fixed, you can remove the autocmd part.

rockyzhang24 avatar May 20 '22 04:05 rockyzhang24

@rockyzhang24 This causes Telescope to turn from looking like the first image to the second. Other than that, it works really great! image image

emuel-vassallo avatar May 20 '22 09:05 emuel-vassallo

@emuel-vassallo Using the autocmd is just a temporary workaround to solve the winbar update issue I mentioned above and it causes this telescope side effect. Waiting for the fix (https://github.com/neovim/neovim/pull/18646) to be merged, and then the autocmd will not be needed.

rockyzhang24 avatar May 20 '22 15:05 rockyzhang24

@emuel-vassallo Try to change the autocmd event to CursorHold to avoid it disrupting telescope windows.

rockyzhang24 avatar May 20 '22 16:05 rockyzhang24

@emuel-vassallo Try to change the autocmd event to CursorHold to avoid it disrupting telescope windows.

@rockyzhang24 That works, thank you!

emuel-vassallo avatar May 20 '22 18:05 emuel-vassallo

Will add instructions in README on how to setup in winbar 👍🏽

SmiteshP avatar May 22 '22 13:05 SmiteshP

@SmiteshP Great! You can wait until that PR fixing the update bug gets merged.

rockyzhang24 avatar May 22 '22 14:05 rockyzhang24

it's merged

max397574 avatar May 22 '22 18:05 max397574

Great. Then adding gps to winbar is the same with adding it to statusline. It's very handy. And this issue can be closed now.

rockyzhang24 avatar May 22 '22 20:05 rockyzhang24

Now that https://github.com/neovim/neovim/pull/18650 is merged, is it possible to add click support to each part of gps to jump to it?

nghialm269 avatar May 25 '22 12:05 nghialm269

perhaps open a new issue for that @nghialm269

max397574 avatar May 25 '22 13:05 max397574

https://github.com/fgheng/winbar.nvim

siduck avatar Jun 03 '22 09:06 siduck