Improve table; Add prompt window

This commit is contained in:
Myzel394 2023-10-05 09:35:35 +02:00
parent 6a4ec05fff
commit 19ddbe7007
3 changed files with 79 additions and 49 deletions

View File

@ -1,6 +1,7 @@
local Input = require("nui.input") local Input = require("nui.input")
local event = require("nui.utils.autocmd").event 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() local function create_win()
end end
@ -54,24 +55,41 @@ local function get_size()
end end
local function a() 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) local buffer = vim.api.nvim_create_buf(false, true)
-- Center -- Center
local width = 40 local width = 40
local height = 20 local height = 20
local x = math.floor((vim.o.columns - width) / 2)
local win = vim.api.nvim_open_win(buffer, true, { local win = vim.api.nvim_open_win(buffer, true, {
relative = "win", relative = "win",
row = math.floor(((vim.o.lines - height) / 2) - 1), row = math.floor(((vim.o.lines - height) / 2) - 1),
col = math.floor((vim.o.columns - width) / 2), col = x,
width = width, width = width,
height = height, height = height,
style = "minimal", style = "minimal",
border = "rounded", 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) vim.api.nvim_buf_set_lines(buffer, 0, -1, false, representation)

46
lua/easytables/table.lua Normal file
View File

@ -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

View File

@ -1,22 +1,4 @@
local table = {}; local M = {};
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
DEFAULT_DRAW_REPRESENTATION_OPTIONS = { DEFAULT_DRAW_REPRESENTATION_OPTIONS = {
min_width = 3, min_width = 3,
@ -34,22 +16,6 @@ DEFAULT_DRAW_REPRESENTATION_OPTIONS = {
cross = "" 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) function create_horizontal_line(width, cell_width, left, middle, right, middle_t)
local string = "" local string = ""
@ -107,18 +73,18 @@ function table.draw_representation(table, options)
local vertical = options.vertical or DEFAULT_DRAW_REPRESENTATION_OPTIONS.vertical local vertical = options.vertical or DEFAULT_DRAW_REPRESENTATION_OPTIONS.vertical
local representation = {} 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 -- 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 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 = "" local line = ""
for j = 1, #table[i] do for j = 1, table:cols_amount() do
local cell = table[i][j] local cell = table:value_at(i, j)
local cell_width = #cell local cell_width = #cell
if cell_width < min_width then if cell_width < min_width then
@ -127,7 +93,7 @@ function table.draw_representation(table, options)
cell = vertical .. cell cell = vertical .. cell
if j == #table[i] then if j == table:cols_amount() then
cell = cell .. vertical cell = cell .. vertical
end end
@ -136,12 +102,12 @@ function table.draw_representation(table, options)
representation[#representation + 1] = line representation[#representation + 1] = line
if i ~= #table then if i ~= table:rows_amount() then
representation[#representation + 1] = horizontal_divider representation[#representation + 1] = horizontal_divider
end end
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 return representation
end end