main
Micke Nordin 5 days ago
parent 2afce0278e
commit a72f85ce15

@ -66,47 +66,51 @@ 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)
local new = vim.api.nvim_buf_get_lines(buffer, 0, -1, false) if event.match == "KnotWrite" then
if new ~= record then local new = vim.api.nvim_buf_get_lines(buffer, 0, -1, false)
local cleanold = {} if new ~= record then
local changed = {} local cleanold = {}
for _, v in ipairs(record) do local updated = {}
if v ~= nil and v ~= "" then local did_update = false
local key = v:match("%w+"):lower() for _, v in ipairs(record) do
cleanold[key] = v:gsub("%w+%:%s", "") if v ~= nil and v ~= "" then
local key = v:match("%w+"):lower()
cleanold[key] = v:gsub("%w+%:%s", "")
end
end end
end local cleannew = {}
local cleannew = {} for _, v in ipairs(new) do
for _, v in ipairs(new) 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() 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(updated, " -a " .. key .. "=" .. cleannew[key])
table.insert(changed, " -a " .. key .. "=" .. cleannew[key]) did_update = true
end
end end
end end
end if did_update then
if changed ~= {} 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(updated, " ")
table.concat(changed, " ") local choice = vim.fn.confirm("Update record and close buffer?", "&Yes\n&No", 2)
local choice = vim.fn.confirm("Update record?", "&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