fix: Correctly jump back to previous window on table export

This commit is contained in:
Myzel394 2023-10-07 09:41:53 +02:00
parent 34012f9096
commit a3bcf40bff
3 changed files with 71 additions and 70 deletions

View File

@ -1,70 +1,43 @@
local Input = require("nui.input")
local event = require("nui.utils.autocmd").event
local table = require("easytables.table") local table = require("easytables.table")
local window = require("easytables.window") 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 end
local function show_table_builder(rows, cols) local function setup()
create_win() vim.api.nvim_create_user_command(
end "EasyTablesCreateNew",
function(opt)
local input = opt.args
local function get_size() local success, result = pcall(function() return inputHelper.extract_column_info(input) end)
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 not success then
if create_singular == "x" then error("Don't know how to interpret this message. Please use a format like 3x4 or 3x or 4 or x5")
cols = "1" return
else
cols = rows
end
end end
rows = tonumber(rows) -- tuple do not seem to be working with pcall
cols = tonumber(cols) local cols = result[1]
local rows = result[2]
show_table_builder(rows, cols) create_new_table(cols, rows)
end, end,
}) {
nargs = 1,
dialog_input:mount() desc = "Create a new markdown table using EasyTables"
}
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()
end end
return { return {
a = a, setup = setup,
get_size = get_size,
} }

26
lua/easytables/input.lua Normal file
View File

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

View File

@ -21,7 +21,7 @@ function M:create(table, options)
self.height = options.height or DEFAULT_OPTIONS.height self.height = options.height or DEFAULT_OPTIONS.height
self.min_value_width = options.min_value_width or DEFAULT_OPTIONS.min_value_width self.min_value_width = options.min_value_width or DEFAULT_OPTIONS.min_value_width
self.table = table self.table = table
self.previous_buffer = vim.api.nvim_get_current_buf() self.previous_window = vim.api.nvim_get_current_win()
return self return self
end end
@ -48,7 +48,6 @@ function M:_open_preview_window()
title_pos = "center", title_pos = "center",
}) })
vim.api.nvim_set_option_value("readonly", true, { win = self.preview_window })
-- Disable default highlight -- Disable default highlight
vim.api.nvim_set_option_value("winhighlight", "Normal:Normal", vim.api.nvim_set_option_value("winhighlight", "Normal:Normal",
{ win = self.preview_window }) { 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.preview_window, true)
vim.api.nvim_win_close(self.prompt_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.preview_window = nil
self.prompt_window = nil self.prompt_window = nil
self.preview_buffer = nil
self.prompt_buffer = nil
self.previous_window = nil
end end
function M:register_listeners() function M:register_listeners()
@ -320,8 +324,7 @@ function M:register_listeners()
function() function()
local markdown_table = export:export_table(self.table) local markdown_table = export:export_table(self.table)
vim.schedule(function() self:close()
vim.cmd("bprevious")
vim.schedule(function() vim.schedule(function()
local cursor = vim.api.nvim_win_get_cursor(0) local cursor = vim.api.nvim_win_get_cursor(0)
@ -335,7 +338,6 @@ function M:register_listeners()
markdown_table markdown_table
) )
end) end)
end)
end, end,
{} {}
) )