Fix highlighting & table width

This commit is contained in:
Myzel394 2023-10-06 20:02:04 +02:00
parent 9c2b047cf0
commit a6d41bc2c1
4 changed files with 21 additions and 12 deletions

View File

@ -55,9 +55,6 @@ local function get_size()
end
local function a()
print("size")
print(#"┐┐")
print(vim.api.nvim_strwidth("┐┐"))
local own_table = table:create(6, 3)
own_table:highlight_cell(1, 1)

View File

@ -14,7 +14,7 @@ function M:create(cols, rows)
return self
end
function M:insert(row, col, value)
function M:insert(col, row, value)
self.table[row][col] = value
end
@ -28,7 +28,7 @@ function M:get_largest_length_for_column(
) -- int
should_use_strwidth = should_use_strwidth or false
local largest = #self.table[1][col]
local largest = 0
for _, row in ipairs(self.table) do
if #row[col] > largest then
largest = should_use_strwidth and vim.api.nvim_strwidth(row[col]) or #row[col]
@ -186,19 +186,19 @@ function M:move_highlight_down()
end
end
function M:get_cell_positions(col, row, min_value_width)
function M:get_cell_positions(col, row, widths)
local length = #""
local start_position = 0
for i, cell in ipairs(self.table[row]) do
for i, _ in ipairs(self.table[row]) do
if i == col then
break
end
start_position = start_position + math.max(min_value_width, #cell) + length
start_position = start_position + widths[i] + length
end
local end_position = math.max(length, start_position) + math.max(min_value_width, #self.table[row][col]) + length
local end_position = math.max(length, start_position) + widths[col] + length
if col ~= 1 then
-- Add `length again because of the border left and right

View File

@ -89,14 +89,15 @@ function table.draw_representation(table, options)
for j = 1, table:cols_amount() do
local length = column_widths[j]
local cell = table:value_at(i, j)
local cell_width = #cell
local cell_width = vim.api.nvim_strwidth(cell)
if cell_width < min_width then
if cell_width < length then
cell = cell .. string.rep(filler, length - cell_width)
end
cell = vertical .. cell
-- Add most right vertical divider
if j == table:cols_amount() then
cell = cell .. vertical
end

View File

@ -78,6 +78,10 @@ function M:show()
self:_open_prompt_window()
end
function M:_reset_prompt()
vim.api.nvim_buf_set_lines(self.prompt_buffer, 0, -1, false, { "" })
end
function M:_draw_highlight(table)
local cell = table:get_highlighted_cell()
@ -86,7 +90,8 @@ function M:_draw_highlight(table)
end
local row = 1 + math.max(0, cell.row - 1) * 2
local cell_start, cell_end = table:get_cell_positions(cell.col, cell.row, self.min_value_width)
local widths = table:get_widths_for_columns(self.min_value_width)
local cell_start, cell_end = table:get_cell_positions(cell.col, cell.row, widths)
local border_start, border_end = table:get_horizontal_border_width(cell.col, cell.row, self.min_value_width)
vim.api.nvim_buf_set_extmark(
@ -152,6 +157,7 @@ function M:register_listeners(table)
function()
table:move_highlight_to_next_cell()
self:draw_table(table)
self:_reset_prompt()
end,
{}
)
@ -162,6 +168,7 @@ function M:register_listeners(table)
function()
table:move_highlight_to_previous_cell()
self:draw_table(table)
self:_reset_prompt()
end,
{}
)
@ -172,6 +179,7 @@ function M:register_listeners(table)
function()
table:move_highlight_down()
self:draw_table(table)
self:_reset_prompt()
end,
{}
)
@ -182,6 +190,7 @@ function M:register_listeners(table)
function()
table:move_highlight_up()
self:draw_table(table)
self:_reset_prompt()
end,
{}
)
@ -192,6 +201,7 @@ function M:register_listeners(table)
function()
table:move_highlight_left()
self:draw_table(table)
self:_reset_prompt()
end,
{}
)
@ -202,6 +212,7 @@ function M:register_listeners(table)
function()
table:move_highlight_right()
self:draw_table(table)
self:_reset_prompt()
end,
{}
)