From 16d0e0645af61fb644cfc3ebfc4a5932972911cf Mon Sep 17 00:00:00 2001 From: Dominik Polakovics Date: Sat, 4 Jan 2025 17:25:10 +0100 Subject: [PATCH] feat: use system ls command --- README.md | 13 ++++++++++++ lua/chatgpt_nvim/init.lua | 44 ++++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 6fdb680..dfb19be 100644 --- a/README.md +++ b/README.md @@ -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. - 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! diff --git a/lua/chatgpt_nvim/init.lua b/lua/chatgpt_nvim/init.lua index 3a83157..fd83eb6 100644 --- a/lua/chatgpt_nvim/init.lua +++ b/lua/chatgpt_nvim/init.lua @@ -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