moved all readline analogs into the buffer module

This commit is contained in:
Izaya 2020-06-11 12:56:07 +10:00
parent 1cc220d38e
commit f95124996c
2 changed files with 98 additions and 43 deletions

View File

@ -1,4 +1,13 @@
local vtansi = {}
vtansi.sequences = {
[28] = "\n", -- newline
[200] = "\27[A", -- up
[201] = "\27[5~", -- page up
[203] = "\27[D", -- left
[205] = "\27[C", -- right
[208] = "\27[B", -- down
[209] = "\27[6~" -- page down
}
function vtansi.vtemu(gpu) -- table -- function -- takes GPU component proxy *gpu* and returns a function to write to it in a manner like an ANSI terminal
local colours = {0x0,0xFF0000,0x00FF00,0xFFFF00,0x0000FF,0xFF00FF,0x00B6FF,0xFFFFFF}
local mx, my = gpu.maxResolution()
@ -106,7 +115,7 @@ function vtansi.vtemu(gpu) -- table -- function -- takes GPU component proxy *gp
elseif cc == "m" then
for _,num in ipairs(tA) do
if num == 0 then
fg,bg,ec,lb = 0xFFFFFF,0,true,true
fg,bg,ec,lb = 0xFFFFFF,0,false,true
elseif num == 7 then
local nfg,nbg = bg, fg
fg, bg = nfg, nbg
@ -150,37 +159,27 @@ function vtansi.vtsession(gpua,scra) -- string string -- table -- creates a proc
for k,v in ipairs(component.invoke(scra,"getKeyboards")) do
kba[v]=true
end
local buf, lbuf, echo = "", true, true
os.spawn(function() dprint(pcall(function()
local buf, lbuf, echo = "", false, false
os.spawn(function()
while true do
local ty,ka,ch = coroutine.yield()
local ty,ka,ch,kc = coroutine.yield()
if ty == "key_down" and kba[ka] then
if ch == 13 then ch = 10 end
if ch == 8 and lbuf then
if buf:len() > 0 then
if echo then write("\8 \8") end
buf = buf:sub(1,-2)
end
elseif ch > 0 then
if echo then write(string.char(ch)) end
buf=buf..string.char(ch)
local outs
if ch > 0 then
outs = string.char(ch)
end
outs = vtansi.sequences[kc] or outs
if outs then
if echo then write(outs) end
buf=buf..outs
end
end
end
end)) end,string.format("ttyd[%s:%s]",gpua:sub(1,8),scra:sub(1,8)))
end,string.format("ttyd[%s:%s]",gpua:sub(1,8),scra:sub(1,8)))
local function bread(n)
local r
if lbuf then
while not buf:find("\n") do
coroutine.yield()
end
local n = buf:find("\n")
r, buf = buf:sub(1,n), buf:sub(n+1)
else
r = buf
buf = ""
coroutine.yield()
end
coroutine.yield()
local r = buf
buf = ""
return r
end
local function bwrite(d)

View File

@ -234,26 +234,82 @@ function buffer:read(...)
end
local function readLine(chop)
local start = 1
while true do
local l = self.bufferRead:find("\n", start, true)
if l then
local result = self.bufferRead:sub(1, l + (chop and -1 or 0))
self.bufferRead = self.bufferRead:sub(l + 1)
return result
else
start = #self.bufferRead
local result, reason = readChunk()
if not result then
if reason then
return nil, reason
else -- eof
local result = #self.bufferRead > 0 and self.bufferRead or nil
self.bufferRead = ""
return result
if not self.mode.t then
local start = 1
while true do
local l = self.bufferRead:find("\n", start, true)
if l then
local result = self.bufferRead:sub(1, l + (chop and -1 or 0))
self.bufferRead = self.bufferRead:sub(l + 1)
return result
else
start = #self.bufferRead
local result, reason = readChunk()
if not result then
if reason then
return nil, reason
else -- eof
local result = #self.bufferRead > 0 and self.bufferRead or nil
self.bufferRead = ""
return result
end
end
end
end
else
-- ?????
io.write("\27[s\27[8m")
local pos, buffer = 1, ""
local function redraw()
io.write("\27[u")
io.write(buffer.." ")
io.write("\27[u")
io.write(buffer:sub(1,(#buffer-pos)+1))
end
while true do
syslog("top of readline loop")
char = readBytesOrChars(1)
if char == "\27" then
if readBytesOrChars(1) == "[" then
syslog("escape code")
local args = {""}
repeat
char = readBytesOrChars(1)
--[[
if char:match("%d") then
args[#args] = args[#args]..char
else
args[#args] = tonumber(args[#args])
args[#args+1] = ""
end
]]
until not char:match("[%d;]")
if char == "C" then -- right
if pos > 1 then
syslog("moving right")
pos = pos - 1
end
elseif char == "D" then -- left
if pos <= #buffer then
syslog("moving left")
pos = pos + 1
end
end
end
elseif char == "\8" then
if #buffer > 0 and pos <= #buffer then
buffer = buffer:sub(1, (#buffer - pos)) .. buffer:sub((#buffer - pos) + 2)
end
elseif char == "\13" or char == "\10" or char == "\n" then
io.write("\n")
if chop then buffer = buffer .. "\n" end
return buffer
else
syslog("char: "..tostring(string.byte(char)))
buffer = buffer:sub(1, (#buffer - pos) + 1) .. char .. buffer:sub((#buffer - pos) + 2)
end
redraw()
end
end
end