Last active
December 6, 2024 20:30
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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, { | |
split='above', | |
height=20, | |
}) | |
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, { | |
split='above', | |
height=20, | |
}) | |
end | |
else | |
print('no stdin') | |
end | |
end | |
-- :'<,'>Duckdb | |
-- :%Duckdb | |
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