Skip to content

Commit 41cf92a

Browse files
committed
Don't pull this, I'm going to amend it
1 parent f0ace79 commit 41cf92a

File tree

9 files changed

+1022
-39
lines changed

9 files changed

+1022
-39
lines changed

lua/eca/api.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local Utils = require("eca.utils")
2+
local Logger = require("eca.logger")
23

34
-- Load nui.nvim components for floating windows
45
local Popup = require("nui.popup")
@@ -395,6 +396,48 @@ end
395396
local logs_popup = nil
396397

397398
function M.show_logs()
399+
-- Check if file logging is enabled
400+
if Logger.is_file_logging_enabled() then
401+
M._show_logs_in_buffer()
402+
else
403+
M._show_logs_in_popup()
404+
end
405+
end
406+
407+
--- Show logs in a regular Neovim buffer (when file logging is enabled)
408+
function M._show_logs_in_buffer()
409+
local log_path = Logger.get_log_path()
410+
411+
if not log_path then
412+
Utils.warn("File logging is enabled but no log path is available")
413+
return
414+
end
415+
416+
-- Check if log file exists
417+
if vim.fn.filereadable(log_path) ~= 1 then
418+
Utils.info("Log file does not exist yet: " .. log_path)
419+
return
420+
end
421+
422+
-- Open the log file in a new buffer
423+
vim.cmd("edit " .. vim.fn.fnameescape(log_path))
424+
425+
-- Set buffer options for log viewing
426+
vim.bo.readonly = true
427+
vim.bo.modifiable = false
428+
vim.bo.filetype = "log"
429+
vim.wo.wrap = false
430+
vim.wo.number = true
431+
vim.wo.cursorline = true
432+
433+
-- Jump to the end of the file to show latest logs
434+
vim.cmd("normal! G")
435+
436+
Utils.info("Opened ECA log file: " .. log_path)
437+
end
438+
439+
--- Show logs in a popup (when file logging is disabled)
440+
function M._show_logs_in_popup()
398441
local eca = require("eca")
399442

400443
if not eca.server then

lua/eca/commands.lua

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local Utils = require("eca.utils")
22

33
local M = {}
44

5+
--- Setup ECA commands
56
function M.setup()
67
-- Define ECA commands
78
vim.api.nvim_create_user_command("EcaChat", function(opts)
@@ -43,7 +44,7 @@ function M.setup()
4344

4445
vim.api.nvim_create_user_command("EcaAddSelection", function()
4546
-- Force exit visual mode and set marks
46-
vim.cmd('normal! \\<Esc>')
47+
vim.cmd("normal! \\<Esc>")
4748
vim.defer_fn(function()
4849
require("eca.api").add_selection_context()
4950
end, 50) -- Small delay to ensure marks are set
@@ -83,7 +84,7 @@ function M.setup()
8384
})
8485

8586
-- ===== Selected Code Commands =====
86-
87+
8788
vim.api.nvim_create_user_command("EcaShowSelection", function()
8889
require("eca.api").show_selected_code()
8990
end, {
@@ -97,7 +98,7 @@ function M.setup()
9798
})
9899

99100
-- ===== TODOs Commands =====
100-
101+
101102
vim.api.nvim_create_user_command("EcaAddTodo", function(opts)
102103
if opts.args and opts.args ~= "" then
103104
require("eca.api").add_todo(opts.args)
@@ -159,17 +160,17 @@ function M.setup()
159160
local eca = require("eca")
160161
local status = eca.server and eca.server:status() or "Not initialized"
161162
local status_icon = ""
162-
163+
163164
if status == "Running" then
164165
status_icon = ""
165166
elseif status == "Starting" then
166167
status_icon = ""
167168
elseif status == "Failed" then
168169
status_icon = ""
169170
end
170-
171+
171172
Utils.info("ECA Server Status: " .. status .. " " .. status_icon)
172-
173+
173174
-- Also show path info if available
174175
if eca.server and eca.server._path_finder then
175176
local config = require("eca.config")
@@ -183,10 +184,48 @@ function M.setup()
183184
desc = "Show ECA server status with details",
184185
})
185186

186-
vim.api.nvim_create_user_command("EcaLogs", function()
187-
require("eca.api").show_logs()
187+
vim.api.nvim_create_user_command("EcaLogs", function(opts)
188+
local Logger = require("eca.logger")
189+
local Api = require("eca.api")
190+
local subcommand = opts.args and opts.args:match("%S+") or "show"
191+
192+
if subcommand == "show" then
193+
Api.show_logs()
194+
elseif subcommand == "log_path" then
195+
local log_path = Logger.get_log_path()
196+
if log_path then
197+
Utils.info("ECA log file: " .. log_path, { nofity_only = true })
198+
else
199+
Utils.info("File logging is disabled")
200+
end
201+
elseif subcommand == "clear" then
202+
if Logger.clear_log() then
203+
Utils.info("ECA log file cleared")
204+
else
205+
Utils.warn("Failed to clear log file (file logging may be disabled)")
206+
end
207+
elseif subcommand == "stats" then
208+
local stats = Logger.get_log_stats()
209+
if stats then
210+
Utils.info(string.format("Log file: %s", stats.path))
211+
Utils.info(string.format("Size: %.2fMB (%d bytes)", stats.size_mb, stats.size))
212+
Utils.info(string.format("Last modified: %s", stats.modified))
213+
else
214+
Utils.info("No log file stats available (file logging may be disabled)")
215+
end
216+
else
217+
Utils.warn("Unknown EcaLogs subcommand: " .. subcommand)
218+
Utils.info("Available subcommands: show, log_path, clear, stats")
219+
end
188220
end, {
189-
desc = "Open ECA server logs in a new buffer",
221+
desc = "ECA logging commands (show, log_path, clear, stats)",
222+
nargs = "?",
223+
complete = function(ArgLead, CmdLine, CursorPos)
224+
local subcommands = { "show", "log_path", "clear", "stats" }
225+
return vim.tbl_filter(function(cmd)
226+
return cmd:match("^" .. ArgLead)
227+
end, subcommands)
228+
end,
190229
})
191230

192231
vim.api.nvim_create_user_command("EcaSend", function(opts)
@@ -213,23 +252,23 @@ function M.setup()
213252
vim.api.nvim_create_user_command("EcaRedownload", function()
214253
local eca = require("eca")
215254
local Utils = require("eca.utils")
216-
255+
217256
if eca.server and eca.server:is_running() then
218257
Utils.info("Stopping server before re-download...")
219258
eca.server:stop()
220259
end
221-
260+
222261
-- Remove existing version file to force re-download
223262
local cache_dir = Utils.get_cache_dir()
224263
local version_file = cache_dir .. "/eca-version"
225264
os.remove(version_file)
226-
265+
227266
-- Remove existing binary
228267
local server_binary = cache_dir .. "/eca"
229268
os.remove(server_binary)
230-
269+
231270
Utils.info("Removed cached ECA server. Will re-download on next start.")
232-
271+
233272
-- Restart server
234273
vim.defer_fn(function()
235274
if eca.server then
@@ -243,7 +282,7 @@ function M.setup()
243282
vim.api.nvim_create_user_command("EcaStopResponse", function()
244283
local eca = require("eca")
245284
local Utils = require("eca.utils")
246-
285+
247286
-- Force stop any ongoing streaming response
248287
if eca.sidebar then
249288
eca.sidebar:_finalize_streaming_response()
@@ -257,7 +296,7 @@ function M.setup()
257296

258297
vim.api.nvim_create_user_command("EcaFixTreesitter", function()
259298
local Utils = require("eca.utils")
260-
299+
261300
-- Emergency treesitter fix for chat buffer
262301
vim.schedule(function()
263302
local eca = require("eca")
@@ -266,15 +305,15 @@ function M.setup()
266305
if bufnr and vim.api.nvim_buf_is_valid(bufnr) then
267306
-- Disable all highlighting for this buffer
268307
pcall(vim.api.nvim_set_option_value, "syntax", "off", { buf = bufnr })
269-
308+
270309
-- Destroy treesitter highlighter if it exists
271310
pcall(function()
272311
if vim.treesitter.highlighter.active[bufnr] then
273312
vim.treesitter.highlighter.active[bufnr]:destroy()
274313
vim.treesitter.highlighter.active[bufnr] = nil
275314
end
276315
end)
277-
316+
278317
Utils.info("Disabled treesitter highlighting for ECA chat buffer")
279318
Utils.info("Buffer " .. bufnr .. " highlighting disabled")
280319
else

lua/eca/config.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ M._defaults = {
1010
server_args = "", -- Extra args for the eca start command
1111
---@type string
1212
usage_string_format = "{messageCost} / {sessionCost}",
13+
log = {
14+
level = "info", -- Log level as string: "trace", "debug", "info", "warn", "error"
15+
notify = true, -- Enable vim.notify logging
16+
file = false, -- false = no file logging, true = default XDG path, string = custom path
17+
},
1318
behaviour = {
1419
auto_set_keymaps = true,
1520
auto_focus_sidebar = true,

lua/eca/init.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ function M.setup(opts)
208208

209209
if M.did_setup then return end
210210

211+
-- Initialize logger with configuration
212+
require("eca.logger").setup(Config.options)
213+
211214
require("eca.highlights").setup()
212215
require("eca.commands").setup()
213216

0 commit comments

Comments
 (0)