added history support to buffer:read() in terminal mode, do provide feedback

This commit is contained in:
Izaya 2020-06-20 17:24:10 +10:00
parent e3069f94a3
commit 0421034ff7
1 changed files with 14 additions and 1 deletions

View File

@ -259,7 +259,8 @@ function buffer:read(...)
else
-- ?????
io.write("\27[s\27[8m")
local pos, buffer = 1, ""
local pos, buffer, hIndex = 1, "", 0
self.history = self.history or {}
local function redraw()
io.write(string.format("\27[u%s \27[u\27[%iC",buffer,(#buffer-pos)+1))
end
@ -285,6 +286,16 @@ function buffer:read(...)
if pos <= #buffer then
pos = pos + 1
end
elseif char == "A" then -- up
hIndex = hIndex + 1
io.write("\27[u"..(" "):rep(buffer:len()+1))
buffer = self.history[1+#self.history-hIndex] or buffer
pos = 1
elseif char == "B" then -- down
hIndex = hIndex - 1
io.write("\27[u"..(" "):rep(buffer:len()+1))
buffer = self.history[1+#self.history-hIndex] or buffer
pos = 1
end
end
elseif char == "\8" then
@ -293,6 +304,8 @@ function buffer:read(...)
end
elseif char == "\13" or char == "\10" or char == "\n" then
io.write("\n")
self.history[#self.history+1] = buffer
if #self.history > (self.maxhistory or 16) then table.remove(self.history,1) end
if chop then buffer = buffer .. "\n" end
return buffer
else