diff --git a/lua/easytables/init.lua b/lua/easytables/init.lua index 54ea792..11a170b 100644 --- a/lua/easytables/init.lua +++ b/lua/easytables/init.lua @@ -55,6 +55,9 @@ 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) @@ -63,11 +66,6 @@ local function a() window:show() window:draw_table(own_table) window:register_listeners(own_table) - - print("size") - print(#"─") - print(#" ") - print(#"┌") end return { diff --git a/lua/easytables/table.lua b/lua/easytables/table.lua index 3307cbb..f5c9185 100644 --- a/lua/easytables/table.lua +++ b/lua/easytables/table.lua @@ -23,25 +23,15 @@ function M:value_at(row, col) end function M:get_largest_length_for_column( - col --[[ int ]] + col, --[[ int ]] + should_use_strwidth --[[ bool ]] ) -- int + should_use_strwidth = should_use_strwidth or false + 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 + largest = should_use_strwidth and vim.api.nvim_strwidth(row[col]) or #row[col] end end @@ -62,12 +52,13 @@ function M:get_largest_length() end function M:get_widths_for_columns( - min_width --[[ int ]] + min_width --[[ int ]], + should_use_strwidth --[[ bool ]] ) -- table local widths = {} for i = 1, #self.table[1] do - widths[i] = math.max(min_width, self:get_largest_length_for_column(i)) + widths[i] = math.max(min_width, self:get_largest_length_for_column(i, should_use_strwidth)) end return widths @@ -109,17 +100,73 @@ function M:move_highlight_to_next_cell() end function M:get_cell_positions(col, row, min_value_width) - local start_position = 1 + local length = #"│" + local start_position = 0 for i, cell in ipairs(self.table[row]) do if i == col then break end - start_position = start_position + math.max(min_value_width, #cell) + 1 + start_position = start_position + math.max(min_value_width, #cell) + length end - local end_position = start_position + math.max(min_value_width, #self.table[row][col]) + local end_position = math.max(length, start_position) + math.max(min_value_width, #self.table[row][col]) + length + + if col ~= 1 then + -- Add `length again because of the border left and right + end_position = end_position + length + end + + return start_position, end_position +end + +function M:get_horizontal_border_width( + col, -- [[ int ]] + row, -- [[ int ]] + min_value_width -- [[ int ]] +) + local length = #"─" + local start_position = 0 + local widths = self:get_widths_for_columns(min_value_width, true) + + for i, _ in ipairs(self.table[1]) do + if i == col then + break + end + + start_position = start_position + math.max(min_value_width, widths[i]) * length + + if row == 1 then + start_position = start_position + #"┬" + else + start_position = start_position + #"┼" + end + end + + local end_position = 0 + + if col == 1 then + end_position = #"┬" + else + end_position = #"┤" + end + + end_position = end_position + start_position + math.max(min_value_width, widths[col]) * length + + if row == 1 then + if col == 1 then + end_position = end_position + #"┬" + else + end_position = end_position + #"┐" + end + else + if col == 1 then + end_position = end_position + #"├" + else + end_position = end_position + #"┤" + end + end return start_position, end_position end diff --git a/lua/easytables/window.lua b/lua/easytables/window.lua index 3fba319..e4fae0c 100644 --- a/lua/easytables/window.lua +++ b/lua/easytables/window.lua @@ -87,21 +87,15 @@ function M:_draw_highlight(table) 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) - - print(cell_start, cell_end) - - --cell_start = 10 + (cell_start - 2) * 3 - - -- No idea why, but the table characters take up multiple characters per one characters allegedly - local table_cell_end = 10 + (cell_end - 2) * 3 + local border_start, border_end = table:get_horizontal_border_width(cell.col, cell.row, self.min_value_width) vim.api.nvim_buf_set_extmark( self.preview_buffer, vim.api.nvim_create_namespace("easytables"), row - 1, - cell_start, + border_start, { - end_col = table_cell_end, + end_col = border_end, hl_group = "NormalFloat", hl_mode = "combine", } @@ -110,9 +104,9 @@ function M:_draw_highlight(table) self.preview_buffer, vim.api.nvim_create_namespace("easytables"), row + 1, - 0, + border_start, { - end_col = 6, + end_col = border_end, hl_group = "NormalFloat", hl_mode = "combine", } @@ -121,10 +115,9 @@ function M:_draw_highlight(table) self.preview_buffer, vim.api.nvim_create_namespace("easytables"), row, - 0, + cell_start, { - -- +2 because 2 are missing and +4 for the table character - end_col = 3, + end_col = cell_end, hl_group = "NormalFloat", hl_mode = "combine", }