Skip to content

Commit

Permalink
feat: allow configuring window size with functions
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy committed Jul 25, 2023
1 parent 0d3b64f commit 867a698
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
19 changes: 12 additions & 7 deletions lua/edgy/animate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ function M.get_state(win)
or 1
M.state[win] = {
[long] = #edgebar.wins == 1 and bounds[long] or hidden_size,
[short] = #edgebar.wins == 1 and 1 or edgebar.size,
[short] = #edgebar.wins == 1 and 1
or (type(edgebar.size) == "function" and edgebar.size() or edgebar.size),
}
for _, w in ipairs(edgebar.wins) do
M.state[win][short] = math.max(M.state[win][short], M.state[w] and M.state[w][short] or 0)
Expand All @@ -43,21 +44,25 @@ function M.step(win, step)
local buf = vim.api.nvim_win_get_buf(win.win)
for _, key in ipairs({ "width", "height" }) do
local current = vim.api["nvim_win_get_" .. key](win.win)
local dim = win[key]
if dim and type(dim) == "function" then
dim = dim()
end
if vim.bo[buf].buftype == "terminal" then
state[key] = win[key]
state[key] = dim
else
if win[key] and state[key] ~= win[key] then
if state[key] > win[key] then
state[key] = math.max(state[key] - step, win[key])
if dim and state[key] ~= dim then
if state[key] > dim then
state[key] = math.max(state[key] - step, dim)
else
state[key] = math.min(state[key] + step, win[key])
state[key] = math.min(state[key] + step, dim)
end
end
end
if current ~= state[key] then
vim.api["nvim_win_set_" .. key](win.win, math.floor(state[key] + 0.5))
end
updated = updated or current ~= win[key]
updated = updated or current ~= dim
end
return updated
end
Expand Down
5 changes: 4 additions & 1 deletion lua/edgy/edgebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ M.__index = M
---@field width? number
---@field height? number

---@param size number
---@param size number | fun(): number
---@param max number
function M.size(size, max)
if type(size) == "function" then
size = size()
end
return math.max(size < 1 and math.floor(max * size) or size, 1)
end

Expand Down
3 changes: 3 additions & 0 deletions lua/edgy/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ function M:apply_size()
for _, key in ipairs({ "width", "height" }) do
local current = vim.api["nvim_win_get_" .. key](self.win)
local needed = self[key]
if type(needed) == "function" then
needed = needed()
end
if current ~= needed then
changes[key] = { current, needed }
vim.api["nvim_win_set_" .. key](self.win, needed)
Expand Down

0 comments on commit 867a698

Please sign in to comment.