feat: add debug logging

This commit is contained in:
2024-12-14 12:48:16 +01:00
parent 314a65a203
commit e97aa81d8f
5 changed files with 104 additions and 52 deletions

View File

@@ -1,6 +1,8 @@
local M = {}
local uv = vim.loop
local config = require('chatgpt_nvim.config')
local function ensure_dir(path)
local st = uv.fs_stat(path)
if st and st.type == 'directory' then
@@ -15,10 +17,16 @@ local function ensure_dir(path)
end
function M.get_clipboard_content()
return vim.fn.getreg('+')
local conf = config.load()
local content = vim.fn.getreg('+')
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:handler] Clipboard content length: " .. #content .. "\n")
end
return content
end
function M.write_file(filepath, content)
local conf = config.load()
local dir = filepath:match("(.*)/")
if dir and dir ~= "" then
ensure_dir(dir)
@@ -26,21 +34,38 @@ function M.write_file(filepath, content)
local fd = uv.fs_open(filepath, "w", 438)
if not fd then
vim.api.nvim_err_writeln("Could not open file for writing: " .. filepath)
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:handler] Failed to open file for writing: " .. filepath .. "\n")
end
return
end
uv.fs_write(fd, content, -1)
uv.fs_close(fd)
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:handler] Successfully wrote file: " .. filepath .. "\n")
end
end
function M.delete_file(filepath)
local conf = config.load()
local st = uv.fs_stat(filepath)
if st then
local success, err = uv.fs_unlink(filepath)
if not success then
vim.api.nvim_err_writeln("Could not delete file: " .. filepath .. " - " .. (err or "unknown error"))
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:handler] Failed to delete file: " .. filepath .. "\n")
end
else
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:handler] Deleted file: " .. filepath .. "\n")
end
end
else
vim.api.nvim_err_writeln("File not found, cannot delete: " .. filepath)
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:handler] File not found for deletion: " .. filepath .. "\n")
end
end
end