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 = {} 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 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 colours = {0x0,0xFF0000,0x00FF00,0xFFFF00,0x0000FF,0xFF00FF,0x00B6FF,0xFFFFFF}
local mx, my = gpu.maxResolution() local mx, my = gpu.maxResolution()
@ -106,7 +115,7 @@ function vtansi.vtemu(gpu) -- table -- function -- takes GPU component proxy *gp
elseif cc == "m" then elseif cc == "m" then
for _,num in ipairs(tA) do for _,num in ipairs(tA) do
if num == 0 then if num == 0 then
fg,bg,ec,lb = 0xFFFFFF,0,true,true fg,bg,ec,lb = 0xFFFFFF,0,false,true
elseif num == 7 then elseif num == 7 then
local nfg,nbg = bg, fg local nfg,nbg = bg, fg
fg, bg = nfg, nbg 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 for k,v in ipairs(component.invoke(scra,"getKeyboards")) do
kba[v]=true kba[v]=true
end end
local buf, lbuf, echo = "", true, true local buf, lbuf, echo = "", false, false
os.spawn(function() dprint(pcall(function() os.spawn(function()
while true do 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 ty == "key_down" and kba[ka] then
if ch == 13 then ch = 10 end local outs
if ch == 8 and lbuf then if ch > 0 then
if buf:len() > 0 then outs = string.char(ch)
if echo then write("\8 \8") end end
buf = buf:sub(1,-2) outs = vtansi.sequences[kc] or outs
end if outs then
elseif ch > 0 then if echo then write(outs) end
if echo then write(string.char(ch)) end buf=buf..outs
buf=buf..string.char(ch)
end end
end 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 function bread(n)
local r coroutine.yield()
if lbuf then local r = buf
while not buf:find("\n") do buf = ""
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
return r return r
end end
local function bwrite(d) local function bwrite(d)

View File

@ -234,26 +234,82 @@ function buffer:read(...)
end end
local function readLine(chop) local function readLine(chop)
local start = 1 if not self.mode.t then
while true do local start = 1
local l = self.bufferRead:find("\n", start, true) while true do
if l then local l = self.bufferRead:find("\n", start, true)
local result = self.bufferRead:sub(1, l + (chop and -1 or 0)) if l then
self.bufferRead = self.bufferRead:sub(l + 1) local result = self.bufferRead:sub(1, l + (chop and -1 or 0))
return result self.bufferRead = self.bufferRead:sub(l + 1)
else return result
start = #self.bufferRead else
local result, reason = readChunk() start = #self.bufferRead
if not result then local result, reason = readChunk()
if reason then if not result then
return nil, reason if reason then
else -- eof return nil, reason
local result = #self.bufferRead > 0 and self.bufferRead or nil else -- eof
self.bufferRead = "" local result = #self.bufferRead > 0 and self.bufferRead or nil
return result self.bufferRead = ""
return result
end
end end
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
end end