From 9c2b047cf09902c8fcc7e6b7997cb41fb503c3f5 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:08:00 +0200 Subject: [PATCH] Add jumping to different direction --- lua/easytables/table.lua | 95 +++++++++++++++++++++++++++++++++++++-- lua/easytables/window.lua | 50 +++++++++++++++++++++ 2 files changed, 141 insertions(+), 4 deletions(-) diff --git a/lua/easytables/table.lua b/lua/easytables/table.lua index f5c9185..089d9da 100644 --- a/lua/easytables/table.lua +++ b/lua/easytables/table.lua @@ -87,10 +87,18 @@ function M:move_highlight_to_next_cell() end if self.highlighted_cell.col == self:cols_amount() then - self.highlighted_cell = { - col = 1, - row = self.highlighted_cell.row + 1, - } + if self.highlighted_cell.row == self:rows_amount() then + -- Reset highlight to the first cell + self.highlighted_cell = { + col = 1, + row = 1, + } + else + self.highlighted_cell = { + col = 1, + row = self.highlighted_cell.row + 1, + } + end else self.highlighted_cell = { col = self.highlighted_cell.col + 1, @@ -99,6 +107,85 @@ function M:move_highlight_to_next_cell() end end +-- Jumps to previous cell in row. If there is no previous cell, it jumps to the last cell in the previous row. +function M:move_highlight_to_previous_cell() + if self.highlighted_cell == nil then + return + end + + if self.highlighted_cell.col == 1 then + if self.highlighted_cell.row == 1 then + -- Reset highlight to the last cell + self.highlighted_cell = { + col = self:cols_amount(), + row = self:rows_amount(), + } + else + self.highlighted_cell = { + col = self:cols_amount(), + row = self.highlighted_cell.row - 1, + } + end + else + self.highlighted_cell = { + col = self.highlighted_cell.col - 1, + row = self.highlighted_cell.row, + } + end +end + +-- Moves highlight to the right, jumps back to the first cell in the same row if it is already at the rightmost cell. +function M:move_highlight_right() + if self.highlighted_cell == nil then + return + end + + if self.highlighted_cell.col == self:cols_amount() then + self.highlighted_cell.col = 1 + else + self.highlighted_cell.col = self.highlighted_cell.col + 1 + end +end + +-- Moves highlight to the left, jumps back to the last cell in the same row if it is already at the leftmost cell. +function M:move_highlight_left() + if self.highlighted_cell == nil then + return + end + + if self.highlighted_cell.col == 1 then + self.highlighted_cell.col = self:cols_amount() + else + self.highlighted_cell.col = self.highlighted_cell.col - 1 + end +end + +-- Moves highlight to the top, jumps back to the last row if it is already at the topmost row. +function M:move_highlight_up() + if self.highlighted_cell == nil then + return + end + + if self.highlighted_cell.row == 1 then + self.highlighted_cell.row = self:rows_amount() + else + self.highlighted_cell.row = self.highlighted_cell.row - 1 + end +end + +-- Moves highlight to the bottom, jumps back to the first row if it is already at the bottommost row. +function M:move_highlight_down() + if self.highlighted_cell == nil then + return + end + + if self.highlighted_cell.row == self:rows_amount() then + self.highlighted_cell.row = 1 + else + self.highlighted_cell.row = self.highlighted_cell.row + 1 + end +end + function M:get_cell_positions(col, row, min_value_width) local length = #"│" local start_position = 0 diff --git a/lua/easytables/window.lua b/lua/easytables/window.lua index e4fae0c..bc2e23a 100644 --- a/lua/easytables/window.lua +++ b/lua/easytables/window.lua @@ -155,6 +155,56 @@ function M:register_listeners(table) end, {} ) + + vim.api.nvim_buf_create_user_command( + self.prompt_buffer, + "JumpToPreviousCell", + function() + table:move_highlight_to_previous_cell() + self:draw_table(table) + end, + {} + ) + + vim.api.nvim_buf_create_user_command( + self.prompt_buffer, + "JumpDown", + function() + table:move_highlight_down() + self:draw_table(table) + end, + {} + ) + + vim.api.nvim_buf_create_user_command( + self.prompt_buffer, + "JumpUp", + function() + table:move_highlight_up() + self:draw_table(table) + end, + {} + ) + + vim.api.nvim_buf_create_user_command( + self.prompt_buffer, + "JumpLeft", + function() + table:move_highlight_left() + self:draw_table(table) + end, + {} + ) + + vim.api.nvim_buf_create_user_command( + self.prompt_buffer, + "JumpRight", + function() + table:move_highlight_right() + self:draw_table(table) + end, + {} + ) end return M