Skip to content

Commit de7d39f

Browse files
committed
feat: dynamic main with
1 parent 31d0d45 commit de7d39f

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

lua/zen/init.lua

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
--- @field filetype Filetype
55

66
--- @class Config
7-
--- @field main? { width?: number }
7+
--- @field main? { width?: number | fun(): number; }
88
--- @field top? Integration[]
99
--- @field right? { min_width?: number; [number]: Integration[]}
1010
--- @field bottom? Integration[]
1111
--- @field left? { min_width?: number; [number]: Integration[]}
1212

1313
--- @class ConfigOptions
14-
--- @field main { width: number }
14+
--- @field main { width: number | fun(): number; }
1515
--- @field top Integration[]
1616
--- @field right { min_width: number; [number]: Integration[]}
1717
--- @field bottom Integration[]
@@ -29,6 +29,16 @@ local state = {
2929
[vim.api.nvim_get_current_tabpage()] = { left = nil, right = nil },
3030
}
3131

32+
--- Resolves the configured main width.
33+
---@return number
34+
local function get_main_width()
35+
local width = opts.main.width
36+
if type(width) == "function" then
37+
return width()
38+
end
39+
return width
40+
end
41+
3242
local function create_window(position)
3343
if position == "left" then
3444
vim.cmd("topleft vnew")
@@ -37,7 +47,7 @@ local function create_window(position)
3747
end
3848

3949
local win_id = vim.api.nvim_get_current_win()
40-
vim.api.nvim_win_set_width(win_id, math.floor((vim.o.columns - opts.main.width) / 2))
50+
vim.api.nvim_win_set_width(win_id, math.floor((vim.o.columns - get_main_width()) / 2))
4151
vim.api.nvim_set_option_value("winfixwidth", true, { scope = "local", win = win_id })
4252
vim.api.nvim_set_option_value("winfixbuf", true, { scope = "local", win = win_id })
4353
vim.api.nvim_set_option_value("cursorline", false, { scope = "local", win = win_id })
@@ -64,6 +74,7 @@ local function is_filetype(target, filetype)
6474
return false
6575
end
6676

77+
---@return number
6778
local function get_side_buffer(position)
6879
local current_tabpage = vim.api.nvim_get_current_tabpage()
6980
if state[current_tabpage] and state[current_tabpage][position] then
@@ -209,7 +220,7 @@ local function adjust_top_bottom_window_hack(target_window, position)
209220
end
210221

211222
local function resize_side_buffers()
212-
local new_width = math.floor((vim.o.columns - opts.main.width) / 2)
223+
local new_width = math.floor((vim.o.columns - get_main_width()) / 2)
213224
local left = vim.api.nvim_win_is_valid(get_side_buffer("left"))
214225
if left then
215226
vim.api.nvim_win_set_width(get_side_buffer("left"), new_width)
@@ -250,7 +261,7 @@ local function setup(options)
250261
for _, integration in pairs(opts[position]) do
251262
---@diagnostic disable-next-line: undefined-field
252263
if type(integration) == "table" and integration.filetype == filetype then
253-
local new_width = math.max(opts[position].min_width, math.floor((vim.o.columns - opts.main.width) / 2))
264+
local new_width = math.max(opts[position].min_width, math.floor((vim.o.columns - get_main_width()) / 2))
254265
vim.api.nvim_win_set_width(buf_info[1].windows[1], new_width)
255266
return
256267
end
@@ -264,7 +275,7 @@ local function setup(options)
264275
vim.api.nvim_create_autocmd({ "VimEnter", "TabNew" }, {
265276
callback = function()
266277
-- disable when window is too small
267-
if vim.o.columns <= opts.main.width then
278+
if vim.o.columns <= get_main_width() then
268279
return
269280
end
270281

@@ -336,7 +347,7 @@ local function setup(options)
336347
end
337348

338349
-- close when window is too small
339-
if vim.o.columns <= opts.main.width then
350+
if vim.o.columns <= get_main_width() then
340351
close_side_buffer("left")
341352
close_side_buffer("right")
342353
return
@@ -363,7 +374,7 @@ local function setup(options)
363374
pattern = "*",
364375
callback = function(args)
365376
-- do not recreate when window is too small
366-
if vim.o.columns <= opts.main.width then
377+
if vim.o.columns <= get_main_width() then
367378
return
368379
end
369380
local win_id = tonumber(args.match)

0 commit comments

Comments
 (0)