I have lua in neovim call sgpt cli. i use vim.ui to get some user input for arguments. on argument is –chat that im not asking user for and wanted a clever solution here like a sort of memory of last one used. but way to specify if changes. any thoughts
To achieve this, you can implement a simple caching mechanism within your Lua script to remember the last --chat argument used. Here’s a basic approach:
-
Cache the Last Argument: Store the last used
--chatargument in a temporary file or a global variable within your Lua script. -
Prompt for Change: Use
vim.ui.inputto ask the user if they want to change the--chatargument. If they choose to change it, update the cache. -
Use the Cached Value: If the user doesn’t want to change it, use the cached value for the
--chatargument.
Here’s a simple example:
local cache_file = "/tmp/sgpt_last_chat.txt"
-- Function to read the last chat argument from cache
local function read_last_chat()
local file = io.open(cache_file, "r")
if file then
local last_chat = file:read("*a")
file:close()
return last_chat
end
return nil
end
-- Function to write the chat argument to cache
local function write_last_chat(chat)
local file = io.open(cache_file, "w")
file:write(chat)
file:close()
end
-- Get the last chat argument
local last_chat = read_last_chat()
-- Ask the user if they want to change the chat argument
vim.ui.input({ prompt = "Change chat argument? (y/n): " }, function(input)
if input == "y" then
vim.ui.input({ prompt = "Enter new chat argument: " }, function(new_chat)
if new_chat then
write_last_chat(new_chat)
-- Use new_chat as the --chat argument
end
end)
else
-- Use last_chat as the --chat argument
end
end)
This script will prompt the user to change the --chat argument and store the new value for future use. Adjust the file path and logic as needed for your specific use case.