From 19ddbe700714919104d7df80f3e4e814af8f2eca Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Thu, 5 Oct 2023 09:35:35 +0200 Subject: [PATCH] Improve table; Add prompt window --- lua/easytables/init.lua | 28 ++++++++++++++--- lua/easytables/table.lua | 46 ++++++++++++++++++++++++++++ lua/easytables/tablebuilder.lua | 54 ++++++--------------------------- 3 files changed, 79 insertions(+), 49 deletions(-) create mode 100644 lua/easytables/table.lua diff --git a/lua/easytables/init.lua b/lua/easytables/init.lua index 861c812..ae08f0c 100644 --- a/lua/easytables/init.lua +++ b/lua/easytables/init.lua @@ -1,6 +1,7 @@ local Input = require("nui.input") local event = require("nui.utils.autocmd").event -local table = require("easytables.tablebuilder") +local table = require("easytables.table") +local table_builder = require("easytables.tablebuilder") local function create_win() end @@ -54,24 +55,41 @@ local function get_size() end local function a() - local own_table = table.create_new_table(6, 3) + local own_table = table:create(6, 3) local buffer = vim.api.nvim_create_buf(false, true) -- Center local width = 40 local height = 20 + local x = math.floor((vim.o.columns - width) / 2) local win = vim.api.nvim_open_win(buffer, true, { relative = "win", row = math.floor(((vim.o.lines - height) / 2) - 1), - col = math.floor((vim.o.columns - width) / 2), + col = x, width = width, height = height, style = "minimal", border = "rounded", - title = "New Table", + title = "", + title_pos = "center", }) - local representation = table.draw_representation(own_table) + vim.api.nvim_set_option_value('winhl', 'Normal:MyHighlight', { win = win }) + + + local new_win = vim.api.nvim_open_win(buffer, true, { + relative = "win", + row = math.floor(((vim.o.lines - height) / 2) - 1) + height + 1, + col = x - 1, + width = width, + height = 2, + style = "minimal", + border = "rounded", + title = "", + title_pos = "center", + }) + + local representation = table_builder.draw_representation(own_table) vim.api.nvim_buf_set_lines(buffer, 0, -1, false, representation) diff --git a/lua/easytables/table.lua b/lua/easytables/table.lua new file mode 100644 index 0000000..ff3e241 --- /dev/null +++ b/lua/easytables/table.lua @@ -0,0 +1,46 @@ +local M = {}; + +function M:create(cols, rows) + local table = {} + for i = 1, rows do + table[i] = {} + for j = 1, cols do + table[i][j] = "" + end + end + + self.table = table + + return self +end + +function M:insert(row, col, value) + self.table[row][col] = value +end + +function M:value_at(row, col) + return self.table[row][col] +end + +function M:get_largest_length() + local largest = #self.table[1][1] + for _, row in ipairs(self.table) do + for _, col in ipairs(row) do + if #col > largest then + largest = #col + end + end + end + + return largest +end + +function M:rows_amount() + return #self.table +end + +function M:cols_amount() + return #self.table[1] +end + +return M diff --git a/lua/easytables/tablebuilder.lua b/lua/easytables/tablebuilder.lua index 90f1c20..868cd39 100644 --- a/lua/easytables/tablebuilder.lua +++ b/lua/easytables/tablebuilder.lua @@ -1,22 +1,4 @@ -local table = {}; - -function table.create_new_table(width, height) - local table = {} - - for i = 1, height do - table[i] = {} - - for j = 1, width do - table[i][j] = "" - end - end - - return table -end - -local function edit_cell(table, x, y, value) - table[y][x] = value -end +local M = {}; DEFAULT_DRAW_REPRESENTATION_OPTIONS = { min_width = 3, @@ -34,22 +16,6 @@ DEFAULT_DRAW_REPRESENTATION_OPTIONS = { cross = "┼" } -local function find_largest_value_length(table) - local largest_value_length = #table[1][1] - - for i = 1, #table do - for j = 1, #table[i] do - local cell_width = #table[i][j] - - if cell_width > largest_value_length then - largest_value_length = cell_width - end - end - end - - return largest_value_length -end - function create_horizontal_line(width, cell_width, left, middle, right, middle_t) local string = "" @@ -107,18 +73,18 @@ function table.draw_representation(table, options) local vertical = options.vertical or DEFAULT_DRAW_REPRESENTATION_OPTIONS.vertical local representation = {} - local largest_length = find_largest_value_length(table) + 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[1], length, options) + local horizontal_divider = create_horizontal_divider(table:cols_amount(), length, options) - representation[#representation + 1] = create_horizontal_divider(#table[1], length, { variant = "top" }) + representation[#representation + 1] = create_horizontal_divider(table:cols_amount(), length, { variant = "top" }) - for i = 1, #table do + for i = 1, table:rows_amount() do local line = "" - for j = 1, #table[i] do - local cell = table[i][j] + for j = 1, table:cols_amount() do + local cell = table:value_at(i, j) local cell_width = #cell if cell_width < min_width then @@ -127,7 +93,7 @@ function table.draw_representation(table, options) cell = vertical .. cell - if j == #table[i] then + if j == table:cols_amount() then cell = cell .. vertical end @@ -136,12 +102,12 @@ function table.draw_representation(table, options) representation[#representation + 1] = line - if i ~= #table then + if i ~= table:rows_amount() then representation[#representation + 1] = horizontal_divider end end - representation[#representation + 1] = create_horizontal_divider(#table[1], length, { variant = "bottom" }) + representation[#representation + 1] = create_horizontal_divider(table:cols_amount(), length, { variant = "bottom" }) return representation end