diff --git a/lua/easytables/init.lua b/lua/easytables/init.lua index 086cc63..42ca71a 100644 --- a/lua/easytables/init.lua +++ b/lua/easytables/init.lua @@ -1,70 +1,43 @@ -local Input = require("nui.input") -local event = require("nui.utils.autocmd").event local table = require("easytables.table") local window = require("easytables.window") +local inputHelper = require("easytables.input") -local function create_win() +local function create_new_table(cols, rows) + local markdown_table = table:create(cols, rows) + + local win = window:create(markdown_table) + + win:show() + win:register_listeners() + win:draw_table() end -local function show_table_builder(rows, cols) - create_win() -end +local function setup() + vim.api.nvim_create_user_command( + "EasyTablesCreateNew", + function(opt) + local input = opt.args -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*)") + local success, result = pcall(function() return inputHelper.extract_column_info(input) end) - if cols == "" then - if create_singular == "x" then - cols = "1" - else - cols = rows - end + if not success then + error("Don't know how to interpret this message. Please use a format like 3x4 or 3x or 4 or x5") + return end - rows = tonumber(rows) - cols = tonumber(cols) + -- tuple do not seem to be working with pcall + local cols = result[1] + local rows = result[2] - show_table_builder(rows, cols) + create_new_table(cols, rows) end, - }) - - dialog_input:mount() - - dialog_input:on(event.BufLeave, function() - dialog_input:unmount() - end) -end - -local function a() - local own_table = table:create(6, 3) - - local window = window:create(own_table) - - window:show() - window:draw_table() - window:register_listeners() + { + nargs = 1, + desc = "Create a new markdown table using EasyTables" + } + ) end return { - a = a, - get_size = get_size, + setup = setup, } diff --git a/lua/easytables/input.lua b/lua/easytables/input.lua new file mode 100644 index 0000000..6cda316 --- /dev/null +++ b/lua/easytables/input.lua @@ -0,0 +1,26 @@ +local string = require("string") + +---Extracts the column info from the input +---@param raw_input string +---@return number, number +local function extract_column_info(raw_input) + local _, _, cols, create_singular, rows = string.find(raw_input, "(%d+)(x?)(%d*)") + + if rows == "" then + if create_singular == "x" then + rows = "1" + else + rows = cols + end + end + + local m = {} + m[1] = tonumber(cols) + m[2] = tonumber(rows) + + return m +end + +return { + extract_column_info = extract_column_info, +} diff --git a/lua/easytables/window.lua b/lua/easytables/window.lua index 7c20fee..1f557fc 100644 --- a/lua/easytables/window.lua +++ b/lua/easytables/window.lua @@ -21,7 +21,7 @@ function M:create(table, options) self.height = options.height or DEFAULT_OPTIONS.height self.min_value_width = options.min_value_width or DEFAULT_OPTIONS.min_value_width self.table = table - self.previous_buffer = vim.api.nvim_get_current_buf() + self.previous_window = vim.api.nvim_get_current_win() return self end @@ -48,7 +48,6 @@ function M:_open_preview_window() title_pos = "center", }) - vim.api.nvim_set_option_value("readonly", true, { win = self.preview_window }) -- Disable default highlight vim.api.nvim_set_option_value("winhighlight", "Normal:Normal", { win = self.preview_window }) @@ -148,8 +147,13 @@ function M:close() vim.api.nvim_win_close(self.preview_window, true) vim.api.nvim_win_close(self.prompt_window, true) + vim.api.nvim_set_current_win(self.previous_window) + self.preview_window = nil self.prompt_window = nil + self.preview_buffer = nil + self.prompt_buffer = nil + self.previous_window = nil end function M:register_listeners() @@ -320,21 +324,19 @@ function M:register_listeners() function() local markdown_table = export:export_table(self.table) + self:close() + vim.schedule(function() - vim.cmd("bprevious") + local cursor = vim.api.nvim_win_get_cursor(0) - vim.schedule(function() - local cursor = vim.api.nvim_win_get_cursor(0) - - vim.api.nvim_buf_set_text( - 0, - cursor[1] - 1, - cursor[2], - cursor[1] - 1, - cursor[2], - markdown_table - ) - end) + vim.api.nvim_buf_set_text( + 0, + cursor[1] - 1, + cursor[2], + cursor[1] - 1, + cursor[2], + markdown_table + ) end) end, {}