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

@@ -69,4 +69,17 @@ token_limit: 3000
- Check `ChatGPT_Debug_Log` if `improved_debug` is on, or the Neovim messages if `debug` is on, for detailed info. - Check `ChatGPT_Debug_Log` if `improved_debug` is on, or the Neovim messages if `debug` is on, for detailed info.
- You can close the selection or prompt buffers at any time with commands like `:bd`, `:x`, or `:wq`. No need to rely on `:q`. - You can close the selection or prompt buffers at any time with commands like `:bd`, `:x`, or `:wq`. No need to rely on `:q`.
## Debug Commands
If `enable_debug_commands` is true, you can include commands like these in your YAML:
```yaml
commands:
- command: "list"
dir: "some/directory"
- command: "grep"
pattern: "searchString"
target: "path/to/file/or/directory"
```
The **list** command now uses the Linux `ls` command to list directory contents. The **grep** command searches for a given pattern in a file or all files in a directory.
Enjoy your improved, more flexible ChatGPT Neovim plugin with step-by-step support! Enjoy your improved, more flexible ChatGPT Neovim plugin with step-by-step support!

View File

@@ -75,7 +75,6 @@ local function get_estimate_fn()
end end
end end
-- Handle large prompts by splitting them if needed
local function handle_step_by_step_if_needed(prompt, estimate_fn) local function handle_step_by_step_if_needed(prompt, estimate_fn)
local conf = config.load() local conf = config.load()
local token_count = estimate_fn(prompt) local token_count = estimate_fn(prompt)
@@ -250,25 +249,6 @@ local function store_prompt_for_reference(prompt)
vim.cmd("buffer " .. bufnr) vim.cmd("buffer " .. bufnr)
end 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 function grep_in_file(search_string, filepath)
local content = read_file(filepath) local content = read_file(filepath)
if not content then if not content then
@@ -295,9 +275,15 @@ local function execute_debug_command(cmd)
end end
local command = cmd.command local command = cmd.command
if command == "list" then if command == "ls" then
local dir = cmd.dir or "." 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 elseif command == "grep" then
local pattern = cmd.pattern local pattern = cmd.pattern
local target = cmd.target local target = cmd.target
@@ -309,7 +295,15 @@ local function execute_debug_command(cmd)
return "Cannot grep: target path does not exist" return "Cannot grep: target path does not exist"
end end
if stat.type == "directory" then 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 = {} local results = {}
for _, f in ipairs(all_files) do for _, f in ipairs(all_files) do
local fstat = vim.loop.fs_stat(f) local fstat = vim.loop.fs_stat(f)
@@ -405,7 +399,7 @@ function M.run_chatgpt_command()
```yaml ```yaml
commands: commands:
- command: "list" - command: "ls"
dir: "some/directory" dir: "some/directory"
- command: "grep" - command: "grep"
@@ -413,6 +407,7 @@ function M.run_chatgpt_command()
target: "path/to/file/or/directory" 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. When these commands are present and enable_debug_commands is true, I'll execute them and return the results in the clipboard.
]]) ]])
end end
@@ -652,6 +647,7 @@ function M.run_chatgpt_current_buffer_command()
target: "path/to/file/or/directory" 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. When these commands are present and enable_debug_commands is true, I'll execute them and return the results in the clipboard.
]]) ]])
end end