From 3a056a03f62429c4f601ed1fbcdac84298474330 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Thu, 5 Oct 2023 19:58:59 +0200 Subject: [PATCH] fix: Improvements --- lua/easytables/init.lua | 5 +++++ lua/easytables/table.lua | 34 ++++++++++++++++++++-------------- lua/easytables/window.lua | 22 ++++++++++++++++++---- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/lua/easytables/init.lua b/lua/easytables/init.lua index 8e9ad77..54ea792 100644 --- a/lua/easytables/init.lua +++ b/lua/easytables/init.lua @@ -63,6 +63,11 @@ 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 c01a04b..3307cbb 100644 --- a/lua/easytables/table.lua +++ b/lua/easytables/table.lua @@ -89,6 +89,25 @@ function M:get_highlighted_cell() return self.highlighted_cell end +-- Jumps to next cell in row. If there is no next cell, it jumps to the first cell in the next row. +function M:move_highlight_to_next_cell() + if self.highlighted_cell == nil then + return + end + + if self.highlighted_cell.col == self:cols_amount() then + self.highlighted_cell = { + col = 1, + row = self.highlighted_cell.row + 1, + } + else + self.highlighted_cell = { + col = self.highlighted_cell.col + 1, + row = self.highlighted_cell.row, + } + end +end + function M:get_cell_positions(col, row, min_value_width) local start_position = 1 @@ -97,7 +116,7 @@ function M:get_cell_positions(col, row, min_value_width) break end - start_position = start_position + #cell + 1 + start_position = start_position + math.max(min_value_width, #cell) + 1 end local end_position = start_position + math.max(min_value_width, #self.table[row][col]) @@ -105,17 +124,4 @@ function M:get_cell_positions(col, row, min_value_width) return start_position, end_position end -function M:get_highlighted_position() - if self.highlighted_cell == nil then - return nil - end - - local row = self.highlighted_cell.row - local col = self.highlighted_cell.col - - local cell_start, cell_end = self:get_cell_positions(row, col) - - return cell_start, cell_end -end - return M diff --git a/lua/easytables/window.lua b/lua/easytables/window.lua index 92b492a..3fba319 100644 --- a/lua/easytables/window.lua +++ b/lua/easytables/window.lua @@ -88,6 +88,10 @@ 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 @@ -106,9 +110,9 @@ function M:_draw_highlight(table) self.preview_buffer, vim.api.nvim_create_namespace("easytables"), row + 1, - cell_start, + 0, { - end_col = table_cell_end, + end_col = 6, hl_group = "NormalFloat", hl_mode = "combine", } @@ -117,10 +121,10 @@ function M:_draw_highlight(table) self.preview_buffer, vim.api.nvim_create_namespace("easytables"), row, - cell_start, + 0, { -- +2 because 2 are missing and +4 for the table character - end_col = cell_end + 2 + 3, + end_col = 3, hl_group = "NormalFloat", hl_mode = "combine", } @@ -148,6 +152,16 @@ function M:register_listeners(table) end) end, }) + + vim.api.nvim_buf_create_user_command( + self.prompt_buffer, + "JumpToNextCell", + function() + table:move_highlight_to_next_cell() + self:draw_table(table) + end, + {} + ) end return M