feat: use system ls command

This commit is contained in:
2025-01-04 17:25:10 +01:00
parent 0dc93c1d78
commit 16d0e0645a
2 changed files with 33 additions and 24 deletions

View File

@@ -75,7 +75,6 @@ local function get_estimate_fn()
end
end
-- Handle large prompts by splitting them if needed
local function handle_step_by_step_if_needed(prompt, estimate_fn)
local conf = config.load()
local token_count = estimate_fn(prompt)
@@ -250,25 +249,6 @@ local function store_prompt_for_reference(prompt)
vim.cmd("buffer " .. bufnr)
end
local function list_files_in_dir(dir)
local handle = vim.loop.fs_scandir(dir)
local entries = {}
if handle then
while true do
local name, t = vim.loop.fs_scandir_next(handle)
if not name then
break
end
if t == "directory" then
table.insert(entries, dir .. "/" .. name .. "/")
else
table.insert(entries, dir .. "/" .. name)
end
end
end
return entries
end
local function grep_in_file(search_string, filepath)
local content = read_file(filepath)
if not content then
@@ -295,9 +275,15 @@ local function execute_debug_command(cmd)
end
local command = cmd.command
if command == "list" then
if command == "ls" then
local dir = cmd.dir or "."
return "Listing files in: " .. dir .. "\n" .. table.concat(list_files_in_dir(dir), "\n")
local handle = io.popen("ls " .. dir)
if not handle then
return "Failed to run ls command."
end
local result = handle:read("*a") or ""
handle:close()
return "Listing files in: " .. dir .. "\n" .. result
elseif command == "grep" then
local pattern = cmd.pattern
local target = cmd.target
@@ -309,7 +295,15 @@ local function execute_debug_command(cmd)
return "Cannot grep: target path does not exist"
end
if stat.type == "directory" then
local all_files = list_files_in_dir(target)
local handle = io.popen("ls -p " .. target .. " | grep -v /")
if not handle then
return "Failed to read directory contents for grep."
end
local all_files = {}
for file in handle:read("*a"):gmatch("[^\n]+") do
table.insert(all_files, target .. "/" .. file)
end
handle:close()
local results = {}
for _, f in ipairs(all_files) do
local fstat = vim.loop.fs_stat(f)
@@ -405,7 +399,7 @@ function M.run_chatgpt_command()
```yaml
commands:
- command: "list"
- command: "ls"
dir: "some/directory"
- command: "grep"
@@ -413,6 +407,7 @@ function M.run_chatgpt_command()
target: "path/to/file/or/directory"
```
The "ls" command uses the system's 'ls' command to list directory contents.
When these commands are present and enable_debug_commands is true, I'll execute them and return the results in the clipboard.
]])
end
@@ -652,6 +647,7 @@ function M.run_chatgpt_current_buffer_command()
target: "path/to/file/or/directory"
```
The "list" command uses the system's 'ls' command to list directory contents.
When these commands are present and enable_debug_commands is true, I'll execute them and return the results in the clipboard.
]])
end