commit 6a4ec05fff1d437893b09cecc2987b229d28b8e3 Author: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Wed Oct 4 21:40:29 2023 +0200 feat: Initial project diff --git a/lua/.luarc.json b/lua/.luarc.json new file mode 100644 index 0000000..0251122 --- /dev/null +++ b/lua/.luarc.json @@ -0,0 +1,37 @@ +{ + "workspace.library": [ + "/home/myzel394/.local/share/nvim/site/pack/packer/start/neodev.nvim/types/stable", + "/usr/share/nvim/runtime/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/ChatGPT.nvim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/LuaSnip/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/auto-save.nvim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/cmp-nvim-lsp/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/copilot.vim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/gitsigns.nvim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/harpoon/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/leap.nvim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/lsp-zero.nvim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/mason-lspconfig.nvim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/mason-null-ls.nvim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/mason.nvim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/neo-tree.nvim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/neodev.nvim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/nui.nvim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/null-ls.nvim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/nvim-cmp/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/nvim-colorizer.lua/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/nvim-lspconfig/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/nvim-surround/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/nvim-treesitter/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/nvim-ts-autotag/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/nvim-web-devicons/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/nvim-window-picker/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/packer.nvim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/plenary.nvim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/react-extract.nvim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/tailwindcss-colorizer-cmp.nvim/lua", + "/home/myzel394/.local/share/nvim/site/pack/packer/start/telescope.nvim/lua", + "/home/myzel394/.config/nvim/custom_plugins/lua", + "${3rd}/luv/library" + ] +} \ No newline at end of file diff --git a/lua/easytables/init.lua b/lua/easytables/init.lua new file mode 100644 index 0000000..861c812 --- /dev/null +++ b/lua/easytables/init.lua @@ -0,0 +1,90 @@ +local Input = require("nui.input") +local event = require("nui.utils.autocmd").event +local table = require("easytables.tablebuilder") + +local function create_win() +end + +local function show_table_builder(rows, cols) + create_win() +end + +local function get_size() + local dialog_input = Input({ + position = "50%", + size = { + width = 60, + }, + border = { + style = "single", + text = { + top = "[What's the size of your table?]", + top_align = "center", + }, + }, + win_options = { + winhighlight = "Normal:Normal,FloatBorder:Normal", + }, + }, { + prompt = "> ", + default_value = "3x3", + on_submit = function(value) + _, _, rows, create_singular, cols = string.find(value, "(%d+)(x?)(%d*)") + + if cols == "" then + if create_singular == "x" then + cols = "1" + else + cols = rows + end + end + + rows = tonumber(rows) + cols = tonumber(cols) + + show_table_builder(rows, cols) + end, + }) + + dialog_input:mount() + + dialog_input:on(event.BufLeave, function() + dialog_input:unmount() + end) +end + +local function a() + local own_table = table.create_new_table(6, 3) + local buffer = vim.api.nvim_create_buf(false, true) + + -- Center + local width = 40 + local height = 20 + 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), + width = width, + height = height, + style = "minimal", + border = "rounded", + title = "New Table", + }) + + local representation = table.draw_representation(own_table) + + vim.api.nvim_buf_set_lines(buffer, 0, -1, false, representation) + + vim.api.nvim_buf_attach(buffer, false, { + on_lines = function(_, handle, _, firstline, lastline, new_lastline) + local new_text = vim.api.nvim_buf_get_lines(handle, firstline, new_lastline, true) + + print(new_text[1]) + end, + }) +end + +return { + a = a, + get_size = get_size, +} diff --git a/lua/easytables/tablebuilder.lua b/lua/easytables/tablebuilder.lua new file mode 100644 index 0000000..90f1c20 --- /dev/null +++ b/lua/easytables/tablebuilder.lua @@ -0,0 +1,159 @@ +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 + +DEFAULT_DRAW_REPRESENTATION_OPTIONS = { + min_width = 3, + filler = " ", + top_left = "┌", + top_right = "┐", + bottom_left = "└", + bottom_right = "┘", + horizontal = "─", + vertical = "│", + left_t = "├", + right_t = "┤", + top_t = "┬", + bottom_t = "┴", + 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 = "" + + for i = 1, width do + if i == 1 then + string = string .. left + elseif i == width then + string = string .. right + elseif (i - 1) % (cell_width + 1) == 0 then + string = string .. middle_t + else + string = string .. middle + end + end + + return string +end + +-- Creates a horizontal divider like this: +-- `create_horizontal_divider(5, 5, {variant = "top"})`: +-- ┌─────┬─────┬─────┬─────┬─────┐ +-- `create_horizontal_divider(5, 5, {variant = "between"})`: +-- ├─────┼─────┼─────┼─────┼─────┤ +-- `create_horizontal_divider(5, 5, {variant = "bottom"})`: +-- └─────┴─────┴─────┴─────┴─────┘ +function create_horizontal_divider(table_width, cell_width, options) + local options = options or {} + local top_left = options.top_left or DEFAULT_DRAW_REPRESENTATION_OPTIONS.top_left + local top_right = options.top_right or DEFAULT_DRAW_REPRESENTATION_OPTIONS.top_right + local bottom_left = options.bottom_left or DEFAULT_DRAW_REPRESENTATION_OPTIONS.bottom_left + local bottom_right = options.bottom_right or DEFAULT_DRAW_REPRESENTATION_OPTIONS.bottom_right + local horizontal = options.horizontal or DEFAULT_DRAW_REPRESENTATION_OPTIONS.horizontal + local left_t = options.left_t or DEFAULT_DRAW_REPRESENTATION_OPTIONS.left_t + local right_t = options.right_t or DEFAULT_DRAW_REPRESENTATION_OPTIONS.right_t + local top_t = options.top_t or DEFAULT_DRAW_REPRESENTATION_OPTIONS.top_t + local bottom_t = options.bottom_t or DEFAULT_DRAW_REPRESENTATION_OPTIONS.bottom_t + local cross = options.cross or DEFAULT_DRAW_REPRESENTATION_OPTIONS.cross + local variant = options.variant or "between" + + local full_width = table_width * cell_width + table_width + 1 + + if variant == "top" then + return create_horizontal_line(full_width, cell_width, top_left, horizontal, top_right, top_t) + elseif variant == "between" then + return create_horizontal_line(full_width, cell_width, left_t, horizontal, right_t, cross) + elseif variant == "bottom" then + return create_horizontal_line(full_width, cell_width, bottom_left, horizontal, bottom_right, bottom_t) + end +end + +function table.draw_representation(table, options) + local options = options or {} + local min_width = options.min_width or DEFAULT_DRAW_REPRESENTATION_OPTIONS.min_width + local filler = options.filler or DEFAULT_DRAW_REPRESENTATION_OPTIONS.filler + local vertical = options.vertical or DEFAULT_DRAW_REPRESENTATION_OPTIONS.vertical + + local representation = {} + local largest_length = find_largest_value_length(table) + -- 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) + + representation[#representation + 1] = create_horizontal_divider(#table[1], length, { variant = "top" }) + + for i = 1, #table do + local line = "" + for j = 1, #table[i] do + local cell = table[i][j] + local cell_width = #cell + + if cell_width < min_width then + cell = cell .. string.rep(filler, length - cell_width) + end + + cell = vertical .. cell + + if j == #table[i] then + cell = cell .. vertical + end + + line = line .. cell + end + + representation[#representation + 1] = line + + if i ~= #table then + representation[#representation + 1] = horizontal_divider + end + end + + representation[#representation + 1] = create_horizontal_divider(#table[1], length, { variant = "bottom" }) + + return representation +end + +function table.from_representation(representation, options) + local opts = options or {} + + local table = {} + + for i = 1, #representation do + local character = representation[i] + end +end + +return table