nvim-tree / nvim-tree.lua

A file explorer tree for neovim written in lua

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature Request] Use a function to set up window width

queensferryme opened this issue · comments

This is what I am doing currently. I want the nvim-tree's window width to be adaptive, and percentage is not working greatly -- e.g. 20% is too much on a larger screen and too narrow on a smaller screen.

vim.cmd [[autocmd VimResized * lua require'nvim-tree.view'.View.width=NvimTreeWidth()]]
function NvimTreeWidth()
    local winwidth = vim.fn.winwidth(0)
    if winwidth <= 100 then
        return 30
    elseif winwidth <= 200 then
        return 40
    else
        return 50
    end
end

require'nvim-tree'.setup {
    --- ...
    view = {
        width = NvimTreeWidth(),
    },
}

I am not sure if this is technically feasible, but it would be nice to support passing a custom function to set up width like:

require'nvim-tree'.setup {
    --- ...
    view = {
        width = function()
            --- ...
        end,
    },
}

BTW, nvim-tree.lua is really wonderful. Thank you for your work!

This shouldn't be too hard I believe?

https://github.com/kyazdani42/nvim-tree.lua/blob/fd3969ec986d739d9ab01b8ec5de051c45b475d2/lua/nvim-tree/view.lua#L247-L249

We can simply do something like:

if type(size) == "number" then 
   return size 
elseif type(size) == "function" then
  return size()
end

I would like to submit a PR implementing this if you @kyazdani42 find this proposal acceptable :)

commented

go for it :)

go for it :)

Thanks! PTAL #848 👈

I using the following function to set up the window width,

width = function()
  local winwidth = vim.fn.winwidth(0)
  if winwidth <= 100 then
    return 30
  elseif winwidth <= 200 then
    return 40
  else
    return 50
  end
end,

but, it seems that the value of winwidth will always be 40 in any case, so the function always returns 30.

commented

winwidth will hardly be over 100 because it's the size of the window you pass as parameter, if you wish to get the width of neovim, you should do use vim.go.columns to get the number of columns.

Thanks a lot.