Skip to content

Commit a48e4a0

Browse files
committed
feat(history): don't write to history file if last prompt is the same
1 parent ce73d36 commit a48e4a0

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

lua/opencode/history.lua

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ local function get_history_file()
1515
end
1616

1717
M.write = function(prompt)
18-
local file = io.open(get_history_file().filename, "a")
18+
local history = M.read()
19+
if #history > 0 and history[1] == prompt then
20+
return
21+
end
22+
23+
local file = io.open(get_history_file().filename, 'a')
1924
if file then
2025
-- Escape any newlines in the prompt
21-
local escaped_prompt = prompt:gsub("\n", "\\n")
22-
file:write(escaped_prompt .. "\n")
26+
local escaped_prompt = prompt:gsub('\n', '\\n')
27+
file:write(escaped_prompt .. '\n')
2328
file:close()
2429
-- Invalidate cache when writing new history
2530
cached_history = nil
@@ -33,16 +38,16 @@ M.read = function()
3338
end
3439

3540
local line_by_index = {}
36-
local file = io.open(get_history_file().filename, "r")
41+
local file = io.open(get_history_file().filename, 'r')
3742

3843
if file then
3944
local lines = {}
4045

4146
-- Read all non-empty lines
4247
for line in file:lines() do
43-
if line:gsub("%s", "") ~= "" then
48+
if line:gsub('%s', '') ~= '' then
4449
-- Unescape any escaped newlines
45-
local unescaped_line = line:gsub("\\n", "\n")
50+
local unescaped_line = line:gsub('\\n', '\n')
4651
table.insert(lines, unescaped_line)
4752
end
4853
end

0 commit comments

Comments
 (0)