Skip to content

add option to make width a percentage of "&columns" #473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Plug 'kyazdani42/nvim-tree.lua'

```vim
let g:nvim_tree_side = 'right' "left by default
let g:nvim_tree_width = 40 "30 by default
let g:nvim_tree_width = 40 "30 by default, can be width_in_columns or 'width_in_percent%'
let g:nvim_tree_ignore = [ '.git', 'node_modules', '.cache' ] "empty by default
let g:nvim_tree_gitignore = 1 "0 by default
let g:nvim_tree_auto_open = 1 "0 by default, opens the tree when typing `vim $DIR` or `vim`
Expand Down
5 changes: 4 additions & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ OPTIONS *nvim-tree-options*

|g:nvim_tree_width| *g:nvim_tree_width*

width of the window (default to 30)
width of the window, can be *width_in_columns* or *'width_in_percent%'*
>
let g:nvim_tree_width = 30 " indicates width of 30 columns
let g:nvim_tree_width = '30%' " indicates width of 30% of '&columns'

|g:nvim_tree_side| *g:nvim_tree_side*

Expand Down
13 changes: 11 additions & 2 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,21 @@ function M.focus(winnr, open_if_closed)
a.nvim_set_current_win(wnr)
end

local function get_width()
if type(M.View.width) == "number" then
return M.View.width
end
local width_as_number = tonumber(M.View.width:sub(0, -2))
local percent_as_decimal = width_as_number / 100
return math.floor(vim.o.columns * percent_as_decimal)
end

function M.resize()
if not a.nvim_win_is_valid(M.get_winnr()) then
return
end

a.nvim_win_set_width(M.get_winnr(), M.View.width)
a.nvim_win_set_width(M.get_winnr(), get_width())
end

local move_tbl = {
Expand All @@ -225,7 +234,7 @@ function M.open()
a.nvim_command("vsp")
local move_to = move_tbl[M.View.side]
a.nvim_command("wincmd "..move_to)
a.nvim_command("vertical resize "..M.View.width)
a.nvim_command("vertical resize "..get_width())
local winnr = a.nvim_get_current_win()
local tabpage = a.nvim_get_current_tabpage()
M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or {help = false}, {winnr = winnr})
Expand Down