OC-PsychOS/modules/drivers/fastty.lua
2017-10-13 04:22:36 +11:00

80 lines
1.6 KiB
Lua

_G.term_hack = false
function tty(gA,sA,sI,mx,my)
spawn("fastty: "..gA:sub(1,8)..","..sA:sub(1,8)..","..sI,function()
_G.nlog = ""
local sb,lb,slb = {},{},{}
local cx, cy = 1, 1
local sI = sI or os.getenv("sI")
local gpu = component.proxy(gA)
gpu.bind(sA)
local sx, sy = gpu.maxResolution()
sx, sy = mx or sx, my or sy
gpu.fill(1,1,sx,sy," ")
local function wl(s)
s=tostring(s) or ""
for i = 1,unicode.len(s) do
local c = ""
c=unicode.sub(s,i,i)
if c == "\f" then
for i = 1, sy do
sb[i] = nil
end
cx,cy=1,1
elseif c == "\n" then
cx,cy=1,cy+1
elseif c == "\t" then
repeat
cx=cx+1
until cx%8 == 0
elseif c == "\127" then
cx=cx-1
if cx<1 then cx=1 end
sb[cy] = sb[cy]:sub(1,cx-1).." "..sb[cy]:sub(cx+2)
else
sb[cy] = sb[cy] or ""
while cx > sb[cy]:len() do
sb[cy]=sb[cy] .. " "
end
sb[cy] = sb[cy]:sub(1,cx-1)..c..sb[cy]:sub(cx)
if sb[cy]:len() > sx then
sb[cy] = sb[cy]:sub(1,sx)
end
cx=cx+unicode.charWidth(c)
if cx > sx then
cx,cy = 1, cy+1
end
end
end
end
local function rd()
while #sb > sy do
table.remove(sb,1)
cy=cy-1
end
for i = 1, sy do
if sb[i] and sb[i] ~= lb[i] then
lb[i] = sb[i]
local cs = sb[i]
--nlog = nlog .. tostring(i) .. " " .. tostring(cs) .. "\n"
while cs:len() < sx do
cs=cs.." "
end
gpu.set(1,i,cs)
if term_hack then
gpu.set(1,i,cs)
end
end
end
end
while true do
_,si,str = event.pull("display")
if si == sI then
wl(str)
rd()
gpu.set(cx,cy,"")
end
end
end,{sI=sI})
end