main
Micke Nordin 5 days ago
parent 2afce0278e
commit a72f85ce15

@ -66,21 +66,22 @@ telescope_knot.zones = function(opts)
local record = _filter_records(entry, records) local record = _filter_records(entry, records)
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
local buffer = vim.api.nvim_create_buf(false, true) local buffer = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(buffer, entry.value)
vim.api.nvim_buf_set_lines(buffer, 0, 0, false, record) vim.api.nvim_buf_set_lines(buffer, 0, 0, false, record)
local width = vim.api.nvim_win_get_width(0) local width = vim.api.nvim_win_get_width(0)
local height = vim.api.nvim_win_get_height(0) - 10 local height = vim.api.nvim_win_get_height(0) - 10
local _ = vim.api.nvim_open_win(buffer, true, local win = vim.api.nvim_open_win(buffer, true,
{ relative = 'win', bufpos = { 0, 0 }, width = width, height = height, border = 'rounded' }) { relative = 'win', bufpos = { 0, 0 }, width = width, height = height, border = 'rounded' })
vim.api.nvim_set_keymap("n", "q", "<cmd>bd<CR>", {}) vim.api.nvim_buf_set_keymap(buffer, "c", "w",
vim.api.nvim_buf_set_keymap(buffer, "c", "w", "<NOP>", {}) "lua vim.api.nvim_exec_autocmds('User', { pattern = 'KnotWrite' })<CR>", {})
vim.api.nvim_create_autocmd({ "BufWinLeave" }, { vim.api.nvim_create_autocmd({ "User" }, {
buffer = buffer, buffer = buffer,
callback = function(_) callback = function(event)
if event.match == "KnotWrite" then
local new = vim.api.nvim_buf_get_lines(buffer, 0, -1, false) local new = vim.api.nvim_buf_get_lines(buffer, 0, -1, false)
if new ~= record then if new ~= record then
local cleanold = {} local cleanold = {}
local changed = {} local updated = {}
local did_update = false
for _, v in ipairs(record) do for _, v in ipairs(record) do
if v ~= nil and v ~= "" then if v ~= nil and v ~= "" then
local key = v:match("%w+"):lower() local key = v:match("%w+"):lower()
@ -93,20 +94,23 @@ telescope_knot.zones = function(opts)
local key = v:match("%w+"):lower() local key = v:match("%w+"):lower()
cleannew[key] = v:gsub("%w+%:%s", "") cleannew[key] = v:gsub("%w+%:%s", "")
if cleanold[key] ~= cleannew[key] and (key ~= "url" or key ~= "zone") then if cleanold[key] ~= cleannew[key] and (key ~= "url" or key ~= "zone") then
table.insert(changed, " -a " .. key .. "=" .. cleannew[key]) table.insert(updated, " -a " .. key .. "=" .. cleannew[key])
did_update = true
end end
end end
end end
if changed ~= {} then if did_update then
local command = "knotctl update -z" .. cleanold.zone .. local command = "knotctl update -z" .. cleanold.zone ..
" -t" .. cleanold.ttl .. " -t" .. cleanold.ttl ..
" -n" .. cleanold.name .. " -n" .. cleanold.name ..
" -r" .. cleanold.rtype .. " -r" .. cleanold.rtype ..
" -d" .. cleanold.data .. " -d" .. cleanold.data ..
table.concat(changed, " ") table.concat(updated, " ")
local choice = vim.fn.confirm("Update record?", "&Yes\n&No", 2) local choice = vim.fn.confirm("Update record and close buffer?", "&Yes\n&No", 2)
if choice == 1 then if choice == 1 then
log.info(vim.fn.system(command)) log.info(vim.fn.system(command))
vim.api.nvim_buf_delete(buffer, {})
end
end end
end end
end end

Loading…
Cancel
Save