Skip to content

Instantly share code, notes, and snippets.

@treatmesubj
Last active September 4, 2024 21:13
Show Gist options
  • Save treatmesubj/15fd50041efabf2d882b1ff8bdba4e4b to your computer and use it in GitHub Desktop.
Save treatmesubj/15fd50041efabf2d882b1ff8bdba4e4b to your computer and use it in GitHub Desktop.
execute visually selected SQL with duckdb in neovim and open csv results in new pane
-- inspired by
-- https://gist.github.com/Leenuus/7a2ea47b88bfe16430b42e4e48122718
-- https://gist.github.com/romainl/eae0a260ab9c135390c30cd370c20cd7
local function duckdb(args)
local range = args.range
local line1 = args.line1 - 1
local line2 = args.line2
line2 = line1 == line2 and line1 + 1 or line2
local stdin = vim.api.nvim_buf_get_lines(0, line1, line2, false)
if range ~= 0 then
local output = vim.system({'bash', '-c', 'duckdb -csv'}, {text=true, stdin=stdin}):wait()
-- print(vim.inspect(output))
local new_buf = vim.api.nvim_create_buf(true, false)
if output.code == 0 then
vim.api.nvim_set_option_value("ft", "csv", { buf = new_buf})
vim.api.nvim_buf_set_lines(new_buf, 0, -1, false, vim.fn.split(output.stdout, "\n"))
local win = vim.api.nvim_open_win(new_buf, true, {vertical = false,})
vim.cmd "CSVInit" -- chrisbra/csv.vim
else
vim.api.nvim_buf_set_lines(new_buf, 0, -1, false,
vim.fn.split(tostring(output.code) .. ": " .. output.stderr, "\n")
)
local win = vim.api.nvim_open_win(new_buf, true, {vertical = false,})
end
else
print('no stdin')
end
end
vim.api.nvim_create_user_command("Duckdb", duckdb, {
range = true,
bang = true,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment