Skip to content

Commit d311c22

Browse files
authored
add option to make width a percentage of "&columns" (#473)
1 parent 10e845e commit d311c22

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Plug 'kyazdani42/nvim-tree.lua'
1818

1919
```vim
2020
let g:nvim_tree_side = 'right' "left by default
21-
let g:nvim_tree_width = 40 "30 by default
21+
let g:nvim_tree_width = 40 "30 by default, can be width_in_columns or 'width_in_percent%'
2222
let g:nvim_tree_ignore = [ '.git', 'node_modules', '.cache' ] "empty by default
2323
let g:nvim_tree_gitignore = 1 "0 by default
2424
let g:nvim_tree_auto_open = 1 "0 by default, opens the tree when typing `vim $DIR` or `vim`

doc/nvim-tree-lua.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ OPTIONS *nvim-tree-options*
5454

5555
|g:nvim_tree_width| *g:nvim_tree_width*
5656

57-
width of the window (default to 30)
57+
width of the window, can be *width_in_columns* or *'width_in_percent%'*
58+
>
59+
let g:nvim_tree_width = 30 " indicates width of 30 columns
60+
let g:nvim_tree_width = '30%' " indicates width of 30% of '&columns'
5861
5962
|g:nvim_tree_side| *g:nvim_tree_side*
6063

lua/nvim-tree/view.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,21 @@ function M.focus(winnr, open_if_closed)
214214
a.nvim_set_current_win(wnr)
215215
end
216216

217+
local function get_width()
218+
if type(M.View.width) == "number" then
219+
return M.View.width
220+
end
221+
local width_as_number = tonumber(M.View.width:sub(0, -2))
222+
local percent_as_decimal = width_as_number / 100
223+
return math.floor(vim.o.columns * percent_as_decimal)
224+
end
225+
217226
function M.resize()
218227
if vim.g.nvim_tree_auto_resize == 0 or not a.nvim_win_is_valid(M.get_winnr()) then
219228
return
220229
end
221230

222-
a.nvim_win_set_width(M.get_winnr(), M.View.width)
231+
a.nvim_win_set_width(M.get_winnr(), get_width())
223232
end
224233

225234
local move_tbl = {
@@ -237,7 +246,7 @@ function M.open()
237246
a.nvim_command("vsp")
238247
local move_to = move_tbl[M.View.side]
239248
a.nvim_command("wincmd "..move_to)
240-
a.nvim_command("vertical resize "..M.View.width)
249+
a.nvim_command("vertical resize "..get_width())
241250
local winnr = a.nvim_get_current_win()
242251
local tabpage = a.nvim_get_current_tabpage()
243252
M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or {help = false}, {winnr = winnr})

0 commit comments

Comments
 (0)