feat: change commands to accept args

This commit is contained in:
2025-01-05 17:11:13 +01:00
parent ca729140c3
commit f1cc371294
2 changed files with 97 additions and 63 deletions

View File

@@ -12,7 +12,6 @@ local function copy_to_clipboard(text)
vim.fn.setreg('+', text) vim.fn.setreg('+', text)
end end
-- Expecting 'conf' instead of loading config in parse_response:
local function parse_response(raw, conf) local function parse_response(raw, conf)
if not ok_yaml then if not ok_yaml then
vim.api.nvim_err_writeln("lyaml not available. Install with `luarocks install lyaml`.") vim.api.nvim_err_writeln("lyaml not available. Install with `luarocks install lyaml`.")
@@ -54,7 +53,6 @@ local function is_directory(path)
return stat and stat.type == "directory" return stat and stat.type == "directory"
end end
-- Expect 'conf' instead of loading config in handle_step_by_step_if_needed:
local function handle_step_by_step_if_needed(prompt, conf) local function handle_step_by_step_if_needed(prompt, conf)
local length = #prompt local length = #prompt
if not conf.enable_step_by_step or length <= (conf.prompt_char_limit or 8000) then if not conf.enable_step_by_step or length <= (conf.prompt_char_limit or 8000) then
@@ -221,6 +219,7 @@ local function store_prompt_for_reference(prompt, conf)
vim.cmd("buffer " .. bufnr) vim.cmd("buffer " .. bufnr)
end end
-- Updated to allow flags/args for 'ls' and 'grep'
local function execute_debug_command(cmd, conf) local function execute_debug_command(cmd, conf)
if type(cmd) ~= "table" or not cmd.command then if type(cmd) ~= "table" or not cmd.command then
return "Invalid command object." return "Invalid command object."
@@ -228,24 +227,41 @@ local function execute_debug_command(cmd, conf)
local command = cmd.command local command = cmd.command
if command == "ls" then if command == "ls" then
-- Accept optional flags/args
local dir = cmd.dir or "." local dir = cmd.dir or "."
local handle = io.popen("ls " .. dir) local args = cmd.args or {}
local cmd_str = "ls"
if #args > 0 then
cmd_str = cmd_str .. " " .. table.concat(args, " ")
end
cmd_str = cmd_str .. " " .. dir
local handle = io.popen(cmd_str)
if not handle then if not handle then
return "Failed to run ls command." return "Failed to run ls command."
end end
local result = handle:read("*a") or "" local result = handle:read("*a") or ""
handle:close() handle:close()
return "Listing files in: " .. dir .. "\n" .. result return "Listing files in: " .. dir .. "\n" .. result
elseif command == "grep" then elseif command == "grep" then
-- Accept optional flags/args
-- If the user wants to do something like:
-- args: ["-r", "somePattern", "someDir"]
-- we just pass them all to grep.
local args = cmd.args or {}
if #args == 0 then
-- fallback to old usage
local pattern = cmd.pattern local pattern = cmd.pattern
local target = cmd.target local target = cmd.target
if not pattern or not target then if not pattern or not target then
return "Usage for grep: {command='grep', pattern='<text>', target='<file_or_directory>'}" return "Usage for grep: {command='grep', args=['-r','pattern','target']} or {pattern='<text>', target='<path>'}"
end end
local stat = vim.loop.fs_stat(target) local stat = vim.loop.fs_stat(target)
if not stat then if not stat then
return "Cannot grep: target path does not exist" return "Cannot grep: target path does not exist"
end end
-- old logic remains for backward compatibility
if stat.type == "directory" then if stat.type == "directory" then
local handle = io.popen("ls -p " .. target .. " | grep -v /") local handle = io.popen("ls -p " .. target .. " | grep -v /")
if not handle then if not handle then
@@ -297,12 +313,23 @@ local function execute_debug_command(cmd, conf)
end end
return grep_in_file(pattern, target) return grep_in_file(pattern, target)
end end
else
-- new approach with flags/args
local cmd_str = "grep " .. table.concat(args, " ")
local handle = io.popen(cmd_str)
if not handle then
return "Failed to run grep command."
end
local result = handle:read("*a") or ""
handle:close()
return result
end
else else
return "Unknown command: " .. command return "Unknown command: " .. command
end end
end end
function M.run_chatgpt_command() local function run_chatgpt_command()
local conf = config.load() local conf = config.load()
ui.setup_ui(conf) ui.setup_ui(conf)
ui.debug_log("Running :ChatGPT command.") ui.debug_log("Running :ChatGPT command.")
@@ -398,7 +425,7 @@ function M.run_chatgpt_command()
vim.cmd("buffer " .. bufnr) vim.cmd("buffer " .. bufnr)
end end
function M.run_chatgpt_paste_command() local function run_chatgpt_paste_command()
local conf = config.load() local conf = config.load()
ui.setup_ui(conf) ui.setup_ui(conf)
ui.debug_log("Running :ChatGPTPaste command.") ui.debug_log("Running :ChatGPTPaste command.")
@@ -545,7 +572,7 @@ function M.run_chatgpt_paste_command()
end end
end end
function M.run_chatgpt_current_buffer_command() local function run_chatgpt_current_buffer_command()
local conf = config.load() local conf = config.load()
ui.setup_ui(conf) ui.setup_ui(conf)
ui.debug_log("Running :ChatGPTCurrentBuffer command.") ui.debug_log("Running :ChatGPTCurrentBuffer command.")
@@ -653,4 +680,9 @@ function M.run_chatgpt_current_buffer_command()
end end
end end
M.run_chatgpt_command = run_chatgpt_command
M.run_chatgpt_paste_command = run_chatgpt_paste_command
M.run_chatgpt_current_buffer_command = run_chatgpt_current_buffer_command
M.execute_debug_command = execute_debug_command
return M return M

View File

@@ -74,16 +74,18 @@ local M = {
```yaml ```yaml
commands: commands:
- command: "list" - command: "ls"
dir: "some/directory" args: ["-l", "path/to/directory"]
- command: "grep" - command: "grep"
pattern: "searchString" args: ["-r", "searchString", "path/to/file/or/directory"]
target: "path/to/file/or/directory"
``` ```
The "ls" command uses the system's 'ls' command to list directory contents. The "ls" command uses the system's 'ls' command to list directory contents. You can pass flags or additional arguments in `args`.
When these commands are present and enable_debug_commands is true, I'll execute them and return the results in the clipboard. The "grep" command searches for a given pattern in files or directories, again receiving flags or additional arguments in `args`.
If you omit `args` for grep, you can still use the older format with `pattern` and `target` for backward compatibility.
When these commands are present and `enable_debug_commands` is true, I'll execute them and return the results in the clipboard.
]] ]]
} }