feat: change commands to accept args
This commit is contained in:
@@ -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,81 +227,109 @@ 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
|
||||||
local pattern = cmd.pattern
|
-- Accept optional flags/args
|
||||||
local target = cmd.target
|
-- If the user wants to do something like:
|
||||||
if not pattern or not target then
|
-- args: ["-r", "somePattern", "someDir"]
|
||||||
return "Usage for grep: {command='grep', pattern='<text>', target='<file_or_directory>'}"
|
-- we just pass them all to grep.
|
||||||
end
|
local args = cmd.args or {}
|
||||||
local stat = vim.loop.fs_stat(target)
|
if #args == 0 then
|
||||||
if not stat then
|
-- fallback to old usage
|
||||||
return "Cannot grep: target path does not exist"
|
local pattern = cmd.pattern
|
||||||
end
|
local target = cmd.target
|
||||||
if stat.type == "directory" then
|
if not pattern or not target then
|
||||||
local handle = io.popen("ls -p " .. target .. " | grep -v /")
|
return "Usage for grep: {command='grep', args=['-r','pattern','target']} or {pattern='<text>', target='<path>'}"
|
||||||
if not handle then
|
|
||||||
return "Failed to read directory contents for grep."
|
|
||||||
end
|
end
|
||||||
local all_files = {}
|
local stat = vim.loop.fs_stat(target)
|
||||||
for file in handle:read("*a"):gmatch("[^\n]+") do
|
if not stat then
|
||||||
table.insert(all_files, target .. "/" .. file)
|
return "Cannot grep: target path does not exist"
|
||||||
end
|
end
|
||||||
handle:close()
|
-- old logic remains for backward compatibility
|
||||||
local results = {}
|
if stat.type == "directory" then
|
||||||
local function grep_in_file(search_string, filepath)
|
local handle = io.popen("ls -p " .. target .. " | grep -v /")
|
||||||
local content = read_file(filepath)
|
if not handle then
|
||||||
if not content then
|
return "Failed to read directory contents for grep."
|
||||||
return "Could not read file: " .. filepath
|
|
||||||
end
|
end
|
||||||
local lines = {}
|
local all_files = {}
|
||||||
local line_num = 0
|
for file in handle:read("*a"):gmatch("[^\n]+") do
|
||||||
for line in content:gmatch("([^\n]*)\n?") do
|
table.insert(all_files, target .. "/" .. file)
|
||||||
line_num = line_num + 1
|
end
|
||||||
if line:find(search_string, 1, true) then
|
handle:close()
|
||||||
table.insert(lines, filepath .. ":" .. line_num .. ":" .. line)
|
local results = {}
|
||||||
|
local function grep_in_file(search_string, filepath)
|
||||||
|
local content = read_file(filepath)
|
||||||
|
if not content then
|
||||||
|
return "Could not read file: " .. filepath
|
||||||
|
end
|
||||||
|
local lines = {}
|
||||||
|
local line_num = 0
|
||||||
|
for line in content:gmatch("([^\n]*)\n?") do
|
||||||
|
line_num = line_num + 1
|
||||||
|
if line:find(search_string, 1, true) then
|
||||||
|
table.insert(lines, filepath .. ":" .. line_num .. ":" .. line)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return (#lines == 0) and ("No matches in " .. filepath) or table.concat(lines, "\n")
|
||||||
|
end
|
||||||
|
for _, f in ipairs(all_files) do
|
||||||
|
local fstat = vim.loop.fs_stat(f)
|
||||||
|
if fstat and fstat.type == "file" then
|
||||||
|
table.insert(results, grep_in_file(pattern, f))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return (#lines == 0) and ("No matches in " .. filepath) or table.concat(lines, "\n")
|
return table.concat(results, "\n")
|
||||||
end
|
else
|
||||||
for _, f in ipairs(all_files) do
|
local function grep_in_file(search_string, filepath)
|
||||||
local fstat = vim.loop.fs_stat(f)
|
local content = read_file(filepath)
|
||||||
if fstat and fstat.type == "file" then
|
if not content then
|
||||||
table.insert(results, grep_in_file(pattern, f))
|
return "Could not read file: " .. filepath
|
||||||
|
end
|
||||||
|
local lines = {}
|
||||||
|
local line_num = 0
|
||||||
|
for line in content:gmatch("([^\n]*)\n?") do
|
||||||
|
line_num = line_num + 1
|
||||||
|
if line:find(search_string, 1, true) then
|
||||||
|
table.insert(lines, filepath .. ":" .. line_num .. ":" .. line)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return (#lines == 0) and ("No matches in " .. filepath) or table.concat(lines, "\n")
|
||||||
end
|
end
|
||||||
|
return grep_in_file(pattern, target)
|
||||||
end
|
end
|
||||||
return table.concat(results, "\n")
|
|
||||||
else
|
else
|
||||||
local function grep_in_file(search_string, filepath)
|
-- new approach with flags/args
|
||||||
local content = read_file(filepath)
|
local cmd_str = "grep " .. table.concat(args, " ")
|
||||||
if not content then
|
local handle = io.popen(cmd_str)
|
||||||
return "Could not read file: " .. filepath
|
if not handle then
|
||||||
end
|
return "Failed to run grep command."
|
||||||
local lines = {}
|
|
||||||
local line_num = 0
|
|
||||||
for line in content:gmatch("([^\n]*)\n?") do
|
|
||||||
line_num = line_num + 1
|
|
||||||
if line:find(search_string, 1, true) then
|
|
||||||
table.insert(lines, filepath .. ":" .. line_num .. ":" .. line)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return (#lines == 0) and ("No matches in " .. filepath) or table.concat(lines, "\n")
|
|
||||||
end
|
end
|
||||||
return grep_in_file(pattern, target)
|
local result = handle:read("*a") or ""
|
||||||
|
handle:close()
|
||||||
|
return result
|
||||||
end
|
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
|
||||||
@@ -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.
|
||||||
]]
|
]]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user