mirror of
https://github.com/Myzel394/easytables.nvim.git
synced 2025-06-18 06:55:25 +02:00
fix: Only change cell size per column/row
This commit is contained in:
parent
8273e5020b
commit
158f120c57
@ -22,6 +22,32 @@ function M:value_at(row, col)
|
||||
return self.table[row][col]
|
||||
end
|
||||
|
||||
function M:get_largest_length_for_column(
|
||||
col --[[ int ]]
|
||||
) -- int
|
||||
local largest = #self.table[1][col]
|
||||
for _, row in ipairs(self.table) do
|
||||
if #row[col] > largest then
|
||||
largest = #row[col]
|
||||
end
|
||||
end
|
||||
|
||||
return largest
|
||||
end
|
||||
|
||||
function M:get_largest_length_for_row(
|
||||
row --[[ int ]]
|
||||
) -- int
|
||||
local largest = #self.table[row][1]
|
||||
for _, col in ipairs(self.table[row]) do
|
||||
if #col > largest then
|
||||
largest = #col
|
||||
end
|
||||
end
|
||||
|
||||
return largest
|
||||
end
|
||||
|
||||
function M:get_largest_length()
|
||||
local largest = #self.table[1][1]
|
||||
for _, row in ipairs(self.table) do
|
||||
@ -35,6 +61,18 @@ function M:get_largest_length()
|
||||
return largest
|
||||
end
|
||||
|
||||
function M:get_widths_for_columns(
|
||||
min_width --[[ int ]]
|
||||
) -- table
|
||||
local widths = {}
|
||||
|
||||
for i = 1, #self.table[1] do
|
||||
widths[i] = math.max(min_width, self:get_largest_length_for_column(i))
|
||||
end
|
||||
|
||||
return widths
|
||||
end
|
||||
|
||||
function M:rows_amount()
|
||||
return #self.table
|
||||
end
|
||||
|
@ -16,21 +16,21 @@ DEFAULT_DRAW_REPRESENTATION_OPTIONS = {
|
||||
cross = "┼"
|
||||
}
|
||||
|
||||
function create_horizontal_line(width, cell_width, left, middle, right, middle_t)
|
||||
function create_horizontal_line(cell_widths, left, middle, right, middle_t)
|
||||
local string = ""
|
||||
|
||||
for i = 1, width do
|
||||
if i == 1 then
|
||||
string = string .. left
|
||||
elseif i == width then
|
||||
string = string .. right
|
||||
elseif (i - 1) % (cell_width + 1) == 0 then
|
||||
string = string .. left
|
||||
|
||||
for i, width in ipairs(cell_widths) do
|
||||
string = string .. string.rep(middle, width)
|
||||
|
||||
if i ~= #cell_widths then
|
||||
string = string .. middle_t
|
||||
else
|
||||
string = string .. middle
|
||||
end
|
||||
end
|
||||
|
||||
string = string .. right
|
||||
|
||||
return string
|
||||
end
|
||||
|
||||
@ -41,7 +41,10 @@ end
|
||||
-- ├─────┼─────┼─────┼─────┼─────┤
|
||||
-- `create_horizontal_divider(5, 5, {variant = "bottom"})`:
|
||||
-- └─────┴─────┴─────┴─────┴─────┘
|
||||
function create_horizontal_divider(table_width, cell_width, options)
|
||||
function create_horizontal_divider(
|
||||
table,
|
||||
options -- [[ table ]] -- optional
|
||||
)
|
||||
local options = options or {}
|
||||
local top_left = options.top_left or DEFAULT_DRAW_REPRESENTATION_OPTIONS.top_left
|
||||
local top_right = options.top_right or DEFAULT_DRAW_REPRESENTATION_OPTIONS.top_right
|
||||
@ -53,16 +56,17 @@ function create_horizontal_divider(table_width, cell_width, options)
|
||||
local top_t = options.top_t or DEFAULT_DRAW_REPRESENTATION_OPTIONS.top_t
|
||||
local bottom_t = options.bottom_t or DEFAULT_DRAW_REPRESENTATION_OPTIONS.bottom_t
|
||||
local cross = options.cross or DEFAULT_DRAW_REPRESENTATION_OPTIONS.cross
|
||||
local min_width = options.min_width or DEFAULT_DRAW_REPRESENTATION_OPTIONS.min_width
|
||||
local variant = options.variant or "between"
|
||||
|
||||
local full_width = table_width * cell_width + table_width + 1
|
||||
local widths = table:get_widths_for_columns(min_width)
|
||||
|
||||
if variant == "top" then
|
||||
return create_horizontal_line(full_width, cell_width, top_left, horizontal, top_right, top_t)
|
||||
return create_horizontal_line(widths, top_left, horizontal, top_right, top_t)
|
||||
elseif variant == "between" then
|
||||
return create_horizontal_line(full_width, cell_width, left_t, horizontal, right_t, cross)
|
||||
return create_horizontal_line(widths, left_t, horizontal, right_t, cross)
|
||||
elseif variant == "bottom" then
|
||||
return create_horizontal_line(full_width, cell_width, bottom_left, horizontal, bottom_right, bottom_t)
|
||||
return create_horizontal_line(widths, bottom_left, horizontal, bottom_right, bottom_t)
|
||||
end
|
||||
end
|
||||
|
||||
@ -73,17 +77,17 @@ function table.draw_representation(table, options)
|
||||
local vertical = options.vertical or DEFAULT_DRAW_REPRESENTATION_OPTIONS.vertical
|
||||
|
||||
local representation = {}
|
||||
local largest_length = table:get_largest_length()
|
||||
-- If length is shorter than min_width, then add filler to the end of the string
|
||||
local length = largest_length < min_width and min_width or largest_length
|
||||
|
||||
local horizontal_divider = create_horizontal_divider(table:cols_amount(), length, options)
|
||||
local horizontal_divider = create_horizontal_divider(table, options)
|
||||
|
||||
representation[#representation + 1] = create_horizontal_divider(table:cols_amount(), length, { variant = "top" })
|
||||
representation[#representation + 1] = create_horizontal_divider(table, { variant = "top" })
|
||||
|
||||
local column_widths = table:get_widths_for_columns(min_width)
|
||||
|
||||
for i = 1, table:rows_amount() do
|
||||
local line = ""
|
||||
for j = 1, table:cols_amount() do
|
||||
local length = column_widths[j]
|
||||
local cell = table:value_at(i, j)
|
||||
local cell_width = #cell
|
||||
|
||||
@ -107,7 +111,7 @@ function table.draw_representation(table, options)
|
||||
end
|
||||
end
|
||||
|
||||
representation[#representation + 1] = create_horizontal_divider(table:cols_amount(), length, { variant = "bottom" })
|
||||
representation[#representation + 1] = create_horizontal_divider(table, { variant = "bottom" })
|
||||
|
||||
return representation
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user