kevinhwang91 / nvim-bqf

Better quickfix window in Neovim, polish old quickfix window.
BSD 3-Clause "New" or "Revised" License
1.69k stars 30 forks source link

feat(preview): support format_title config #113

Closed MunifTanjim closed 1 year ago

MunifTanjim commented 1 year ago
The current way of doing this is very hacky. ```lua require("bqf.preview.floatwin").generateTitle = function (self, bufnr, idx, size) local position = (" [%d/%d] "):format(idx, size) local buffer = ("buf %d: "):format(bufnr) local flags = vim.bo[bufnr].modified and " [+] " or " " local max_width = self.width - 4 - vim.fn.strwidth(buffer) - vim.fn.strwidth(position) - vim.fn.strwidth(flags) local fname = vim.fn.fnamemodify(vim.fn.bufname(bufnr), ":p:~:.") local attempt = 3 while max_width < vim.fn.strwidth(fname) do if attempt == -1 then fname = "" break elseif attempt == 0 then fname = vim.fn.fnamemodify(fname, ":t") else fname = vim.fn.pathshorten(fname, attempt * 2 - 1) end attempt = attempt - 1 end return ("%s%s%s%s"):format(position, buffer, fname, flags) end ```

This config will enable user to do it cleanly:

local function format_title(info)
  local bufnr = info.bufnr
  local position = (" [%d/%d] "):format(info.idx, info.size)
  local buffer = ("buf %d: "):format(bufnr)
  local flags = vim.bo[bufnr].modified and " [+] " or " "
  local max_width = info.max_width - vim.fn.strwidth(buffer) - vim.fn.strwidth(position) - vim.fn.strwidth(flags)

  local fname = vim.fn.fnamemodify(vim.fn.bufname(bufnr), ":p:~:.")

  local attempt = 3
  while max_width < vim.fn.strwidth(fname) do
    if attempt == -1 then
      fname = ""
      break
    elseif attempt == 0 then
      fname = vim.fn.fnamemodify(fname, ":t")
    else
      fname = vim.fn.pathshorten(fname, attempt * 2 - 1)
    end
    attempt = attempt - 1
  end

  return ("%s%s%s%s"):format(position, buffer, fname, flags)
end

require("bqf").setup({
  preview = {
    format_title = format_title,
  },
})

Outside current directory (under root):

image

Outside current directory (under home):

image

Inside current directory:

image

Truncations:

3:

image

2:

image

1:

image

0:

image

-1:

image
kevinhwang91 commented 1 year ago

Could you show me your customized title? I'm not sure why the users need it, current title has shown since this plugin release, and no one ask to customize it.

MunifTanjim commented 1 year ago

Could you show me your customized title?

It's mostly like the built-in one. But for files under the current directory, the current directory is stripped out. And the truncation has some additional steps:

This shows as much information as possible, as long as possible. Also, the built-in one has a hardcoded 15 width reserved, so sometimes title disappears even when there's room to show it.

I've added the screenshots in PR description.

kevinhwang91 commented 1 year ago

Thanks to contribute this plugin, but I think it's not worthy for this. As a maintainer, I don't want to expose unnecessary (few users use) API function to users. I hate breaking change and just want to easy to iterate modules in the future.

MunifTanjim commented 1 year ago

Not sure how giving user more customizability is unnecessary, or how this will lead to breaking change... but it's your call.