greatly improved the terminal emulator, with colours (<ESC>[...m) and status support

This commit is contained in:
Izaya 2020-03-24 17:45:29 +11:00
parent 0993182839
commit fab605ff21
2 changed files with 40 additions and 16 deletions

View File

@ -34,6 +34,6 @@ function vtemu(gpua,scra) -- creates a process to handle the GPU and screen addr
r, buf = buf:sub(1,n), buf:sub(n+1) r, buf = buf:sub(1,n), buf:sub(n+1)
return r return r
end end
return bread, write, function() io.write("\27[2J\27[H") end return bread, function(d) buf=buf..write(d) end, function() io.write("\27[2J\27[H") end
end end
end end

View File

@ -1,4 +1,5 @@
function vt100emu(gpu) -- takes GPU component proxy *gpu* and returns a function to write to it in a manner like an ANSI terminal function vt100emu(gpu) -- 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() local mx, my = gpu.maxResolution()
local cx, cy = 1, 1 local cx, cy = 1, 1
local pc = " " local pc = " "
@ -7,16 +8,18 @@ function vt100emu(gpu) -- takes GPU component proxy *gpu* and returns a function
local lw = true local lw = true
local sx, sy = 1,1 local sx, sy = 1,1
local cs = "" local cs = ""
local bg, fg = 0, 0xFFFFFF
-- setup -- setup
gpu.setResolution(mx,my) gpu.setResolution(mx,my)
gpu.fill(1,1,mx,my," ") gpu.fill(1,1,mx,my," ")
function termwrite(s) function termwrite(s)
local rs = ""
s=s:gsub("\8","\27[D") s=s:gsub("\8","\27[D")
pc = gpu.get(cx,cy) pc = gpu.get(cx,cy)
gpu.setForeground(0xFFFFFF) gpu.setForeground(fg)
gpu.setBackground(0) gpu.setBackground(bg)
gpu.set(cx,cy,pc) gpu.set(cx,cy,pc)
for i = 1, s:len() do for i = 1, s:len() do
local cc = s:sub(i,i) local cc = s:sub(i,i)
@ -46,38 +49,58 @@ function vt100emu(gpu) -- takes GPU component proxy *gpu* and returns a function
gpu.copy(1,1,mx,my-1,0,1) gpu.copy(1,1,mx,my-1,0,1)
gpu.fill(1,1,mx,1," ") gpu.fill(1,1,mx,1," ")
mode = "n" mode = "n"
else
mode = "n"
end end
elseif mode == "v" then elseif mode == "v" then
mode = "n"
if cc == "s" then -- save cursor if cc == "s" then -- save cursor
sx, sy = cx, cy sx, sy = cx, cy
mode = "n"
elseif cc == "u" then -- restore cursor elseif cc == "u" then -- restore cursor
cx, cy = sx, sy cx, cy = sx, sy
mode = "n"
elseif cc == "H" then -- cursor home or to elseif cc == "H" then -- cursor home or to
local tx, ty = cs:match("(.-);(.-)") local tx, ty = cs:match("(%d+);(%d+)")
tx, ty = tx or "1", ty or "1" tx, ty = tx or "1", ty or "1"
cx, cy = tonumber(tx), tonumber(ty) cx, cy = tonumber(tx), tonumber(ty)
mode = "n"
elseif cc == "A" then -- cursor up elseif cc == "A" then -- cursor up
cy = cy - (tonumber(cs) or 1) cy = cy - (tonumber(cs) or 1)
mode = "n"
elseif cc == "B" then -- cursor down elseif cc == "B" then -- cursor down
cy = cy + (tonumber(cs) or 1) cy = cy + (tonumber(cs) or 1)
mode = "n"
elseif cc == "C" then -- cursor right elseif cc == "C" then -- cursor right
cx = cx + (tonumber(cs) or 1) cx = cx + (tonumber(cs) or 1)
mode = "n"
elseif cc == "D" then -- cursor left elseif cc == "D" then -- cursor left
cx = cx - (tonumber(cs) or 1) cx = cx - (tonumber(cs) or 1)
mode = "n"
elseif cc == "h" and lc == "7" then -- enable line wrap elseif cc == "h" and lc == "7" then -- enable line wrap
lw = true lw = true
elseif cc == "l" and lc == "7" then -- disable line wrap elseif cc == "l" and lc == "7" then -- disable line wrap
lw = false lw = false
elseif cc == "c" then
rs = string.format("%s\27[%d;%d0c",rs,mx,my)
elseif cc == "n" and lc == "6" then
rs = string.format("%s\27[%d;%dR",rs,cx,cy)
elseif cc == "m" then
for num in cs:gmatch("%d+") do
num=tonumber(num)
if num == 0 then
fg,bg = 0xFFFFFF,0
elseif num == 7 then
local nfg,nbg = bg, fg
fg, bg = nfg, nbg
elseif num > 29 and num < 38 then
fg = colours[num-29]
elseif num > 39 and num < 48 then
bg = colours[num-39]
end end
end
gpu.setForeground(fg)
gpu.setBackground(bg)
else
cs = cs .. cc cs = cs .. cc
if cc:match("[%d;]") then
mode = "v"
end
end
end end
if cx > mx and lw then if cx > mx and lw then
@ -94,11 +117,12 @@ function vt100emu(gpu) -- takes GPU component proxy *gpu* and returns a function
lc = cc lc = cc
end end
pc = gpu.get(cx,cy) pc = gpu.get(cx,cy)
gpu.setForeground(0) gpu.setForeground(bg)
gpu.setBackground(0xFFFFFF) gpu.setBackground(fg)
gpu.set(cx,cy,pc) gpu.set(cx,cy,pc)
gpu.setForeground(0xFFFFFF) gpu.setForeground(fg)
gpu.setBackground(0) gpu.setBackground(bg)
return rs
end end
return termwrite return termwrite