Skip to content
This repository was archived by the owner on Jun 9, 2022. It is now read-only.

Add padding feature to ui.layout #3

Merged
merged 2 commits into from
Feb 5, 2021
Merged
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
64 changes: 46 additions & 18 deletions lua/ui/layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,39 @@ local function make_win(text, options)
end

-- TODO(smolck): Is `partition` an accurate/good name?
local function partition(max_height, max_width, starting_row, starting_col, input, is_horizontal)
local function partition(input, opts)
local top_padding = input.top or 0
local bottom_padding = input.bottom or 0
local right_padding = input.right or 0
local left_padding = input.left or 0

for i, win_id in ipairs(input) do
local row, col
if is_horizontal then
row = starting_row + (max_height * (i - 1))
col = starting_col
if opts.is_horizontal then
row = opts.starting_row + (opts.max_height * (i - 1))
col = opts.starting_col
else
row = starting_row
col = starting_col + (max_width * (i - 1))
row = opts.starting_row
col = opts.starting_col + (opts.max_width * (i - 1))
end
row = row + top_padding
col = col + left_padding

if type(win_id) == 'table' then
local horizontal = not is_horizontal
partition(
horizontal and math.floor(max_height / #win_id) or max_height,
horizontal and max_width or math.floor(max_width / #win_id),
row,
col,
win_id,
horizontal
)
local horizontal = not opts.is_horizontal
partition(win_id, {
max_height = horizontal and math.floor(opts.max_height / #win_id) or opts.max_height,
max_width = horizontal and opts.max_width or math.floor(opts.max_width / #win_id),
starting_row = row - top_padding,
starting_col = col - left_padding,
is_horizontal = horizontal,
})
else
vim.api.nvim_win_set_config(win_id, {
row = row,
col = col,
width = max_width,
height = max_height,
width = opts.max_width - left_padding - right_padding,
height = opts.max_height - top_padding - bottom_padding,
relative = 'win',
})
end
Expand All @@ -68,8 +74,30 @@ local function layout(input)
for i, tbl in ipairs(input) do
local col = max_width * (i - 1)
local max_height = math.floor(vim.o.lines / #tbl)
partition(max_height, max_width, 0, col, tbl, true, false)
partition(tbl, {
max_height = max_height,
max_width = max_width,
starting_row = 0,
starting_col = col, tbl,
is_horizontal = true,
})
end
end

--layout {
-- { make_win(),
-- { make_win('col left'), make_win('col right'), top = 5, left = 5, right = 5 },
-- top = 5,
-- left = 5,
-- right = 5
-- },
-- {
-- make_win(),
-- make_win(),
-- top = 5,
-- left = 5,
-- right = 5
-- },
--}

return layout