Skip to content

Commit

Permalink
feat(table): fit_w opt
Browse files Browse the repository at this point in the history
  • Loading branch information
siduck committed Dec 25, 2024
1 parent 0cdfa1d commit 3f9abc9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions lua/volt/ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ return {
graphs = require "volt.ui.graphs",
border = components.border,
hpad = components.hpad,
line_w = components.line_w,
}
11 changes: 7 additions & 4 deletions lua/volt/ui/table.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local get_column_widths = function(tb, w)
local fit_w = type(w) == "string"
local maxrow = #tb[1]
local sum = 0
local result = {}
Expand All @@ -16,17 +17,19 @@ local get_column_widths = function(tb, w)
sum = sum + maxlen
end

local remaining_space = w - sum
local remaining_space = fit_w and sum or w - sum
local ratio = math.floor(remaining_space / maxrow)

local col_widths = vim.tbl_map(function(x)
sum = sum + ratio
return x + ratio - 2
end, result)

local last_item = col_widths[#col_widths]
-- -1 cuz last col has right border too
col_widths[#col_widths] = last_item + (w - sum) - 1
if not fit_w then
local last_item = col_widths[#col_widths]
-- -1 cuz last col has right border too
col_widths[#col_widths] = last_item + (fit_w and sum or w - sum) - 1
end

return col_widths
end
Expand Down

0 comments on commit 3f9abc9

Please sign in to comment.