mirror of
https://github.com/Myzel394/easytables.nvim.git
synced 2025-06-18 23:05:27 +02:00
fix: Correctly jump back to previous window on table export
This commit is contained in:
parent
34012f9096
commit
a3bcf40bff
@ -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(
|
||||||
|
"EasyTablesCreateNew",
|
||||||
|
function(opt)
|
||||||
|
local input = opt.args
|
||||||
|
|
||||||
|
local success, result = pcall(function() return inputHelper.extract_column_info(input) 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
|
end
|
||||||
|
|
||||||
local function get_size()
|
-- tuple do not seem to be working with pcall
|
||||||
local dialog_input = Input({
|
local cols = result[1]
|
||||||
position = "50%",
|
local rows = result[2]
|
||||||
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
|
create_new_table(cols, rows)
|
||||||
if create_singular == "x" then
|
|
||||||
cols = "1"
|
|
||||||
else
|
|
||||||
cols = rows
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
rows = tonumber(rows)
|
|
||||||
cols = tonumber(cols)
|
|
||||||
|
|
||||||
show_table_builder(rows, cols)
|
|
||||||
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
26
lua/easytables/input.lua
Normal 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,
|
||||||
|
}
|
@ -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,
|
||||||
{}
|
{}
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user