Skip to content

Commit e472619

Browse files
sand4rtCopilot
andauthored
feat: dynamic main with (#4)
* feat: dynamic main with * fix: add nil fallback in get_main_width and fix width type annotation Agent-Logs-Url: https://github.com/sand4rt/zen.nvim/sessions/776df3ec-ea1f-48b9-96ac-c13025ddf517 Co-authored-by: sand4rt <17591696+sand4rt@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 31d0d45 commit e472619

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

‎lua/zen/init.lua‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@
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[]
1818
--- @field left { min_width: number; [number]: Integration[]}
1919

20+
local default_width = 148
2021
--- @type ConfigOptions
2122
local opts = {
22-
main = { width = 148 },
23+
main = { width = default_width },
2324
top = {},
2425
right = { min_width = 46 },
2526
bottom = {},
@@ -29,6 +30,17 @@ local state = {
2930
[vim.api.nvim_get_current_tabpage()] = { left = nil, right = nil },
3031
}
3132

33+
local function get_main_width()
34+
local width = opts.main and opts.main.width
35+
if type(width) == "function" then
36+
width = width()
37+
end
38+
if type(width) ~= "number" then
39+
return default_width
40+
end
41+
return width
42+
end
43+
3244
local function create_window(position)
3345
if position == "left" then
3446
vim.cmd("topleft vnew")
@@ -37,7 +49,7 @@ local function create_window(position)
3749
end
3850

3951
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))
52+
vim.api.nvim_win_set_width(win_id, math.floor((vim.o.columns - get_main_width()) / 2))
4153
vim.api.nvim_set_option_value("winfixwidth", true, { scope = "local", win = win_id })
4254
vim.api.nvim_set_option_value("winfixbuf", true, { scope = "local", win = win_id })
4355
vim.api.nvim_set_option_value("cursorline", false, { scope = "local", win = win_id })
@@ -64,6 +76,7 @@ local function is_filetype(target, filetype)
6476
return false
6577
end
6678

79+
---@return number
6780
local function get_side_buffer(position)
6881
local current_tabpage = vim.api.nvim_get_current_tabpage()
6982
if state[current_tabpage] and state[current_tabpage][position] then
@@ -209,7 +222,7 @@ local function adjust_top_bottom_window_hack(target_window, position)
209222
end
210223

211224
local function resize_side_buffers()
212-
local new_width = math.floor((vim.o.columns - opts.main.width) / 2)
225+
local new_width = math.floor((vim.o.columns - get_main_width()) / 2)
213226
local left = vim.api.nvim_win_is_valid(get_side_buffer("left"))
214227
if left then
215228
vim.api.nvim_win_set_width(get_side_buffer("left"), new_width)
@@ -250,7 +263,7 @@ local function setup(options)
250263
for _, integration in pairs(opts[position]) do
251264
---@diagnostic disable-next-line: undefined-field
252265
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))
266+
local new_width = math.max(opts[position].min_width, math.floor((vim.o.columns - get_main_width()) / 2))
254267
vim.api.nvim_win_set_width(buf_info[1].windows[1], new_width)
255268
return
256269
end
@@ -264,7 +277,7 @@ local function setup(options)
264277
vim.api.nvim_create_autocmd({ "VimEnter", "TabNew" }, {
265278
callback = function()
266279
-- disable when window is too small
267-
if vim.o.columns <= opts.main.width then
280+
if vim.o.columns <= get_main_width() then
268281
return
269282
end
270283

@@ -336,7 +349,7 @@ local function setup(options)
336349
end
337350

338351
-- close when window is too small
339-
if vim.o.columns <= opts.main.width then
352+
if vim.o.columns <= get_main_width() then
340353
close_side_buffer("left")
341354
close_side_buffer("right")
342355
return
@@ -363,7 +376,7 @@ local function setup(options)
363376
pattern = "*",
364377
callback = function(args)
365378
-- do not recreate when window is too small
366-
if vim.o.columns <= opts.main.width then
379+
if vim.o.columns <= get_main_width() then
367380
return
368381
end
369382
local win_id = tonumber(args.match)

0 commit comments

Comments
 (0)