feat: changed to and interactive workflow

This commit is contained in:
2024-12-11 00:06:28 +01:00
parent 8919795603
commit ce2b77b0ec
5 changed files with 109 additions and 80 deletions

View File

@@ -1,39 +1,13 @@
local M = {}
-- paste_code_from_clipboard:
-- The user copies a code block from the website (which includes a first line with the file path),
-- and then runs :ChatGPTPaste.
-- We retrieve the clipboard content, extract the first line as a path, and the rest as code.
function M.paste_code_from_clipboard()
local clipboard_content = vim.fn.getreg('+')
if clipboard_content == "" then
vim.api.nvim_err_writeln("Clipboard is empty. Copy the code block from the website first.")
return
end
local lines = {}
for line in clipboard_content:gmatch("[^\r\n]+") do
table.insert(lines, line)
end
if #lines == 0 then
vim.api.nvim_err_writeln("No content in clipboard to parse.")
return
end
local filepath = lines[1]
local code_lines = {}
for i = 2, #lines do
table.insert(code_lines, lines[i])
end
local code = table.concat(code_lines, "\n")
M.write_file(filepath, code)
print("Wrote file: " .. filepath)
-- Retrieves clipboard content and trims trailing whitespace/newlines
function M.get_clipboard_content()
local content = vim.fn.getreg('+')
content = content:gsub("%s+$", "")
return content
end
function M.write_file(filepath, content)
-- Create directories if needed
local dir = filepath:match("(.*/)")
if dir and dir ~= "" then
vim.fn.mkdir(dir, "p")
@@ -48,4 +22,8 @@ function M.write_file(filepath, content)
end
end
function M.finish()
print("All files processed. You can now continue working.")
end
return M