From fab605ff21b90e6b4b042f528800a95631f7b918 Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Tue, 24 Mar 2020 17:45:29 +1100 Subject: [PATCH] greatly improved the terminal emulator, with colours ([...m) and status support --- module/vt-task.lua | 2 +- module/vt100.lua | 54 +++++++++++++++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/module/vt-task.lua b/module/vt-task.lua index 6f61c5c..ded07d4 100644 --- a/module/vt-task.lua +++ b/module/vt-task.lua @@ -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) return r 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 diff --git a/module/vt100.lua b/module/vt100.lua index 3522fdc..ed18996 100644 --- a/module/vt100.lua +++ b/module/vt100.lua @@ -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 + local colours = {0x0,0xFF0000,0x00FF00,0xFFFF00,0x0000FF,0xFF00FF,0x00B6FF,0xFFFFFF} local mx, my = gpu.maxResolution() local cx, cy = 1, 1 local pc = " " @@ -7,16 +8,18 @@ function vt100emu(gpu) -- takes GPU component proxy *gpu* and returns a function local lw = true local sx, sy = 1,1 local cs = "" + local bg, fg = 0, 0xFFFFFF -- setup gpu.setResolution(mx,my) gpu.fill(1,1,mx,my," ") function termwrite(s) + local rs = "" s=s:gsub("\8","\27[D") pc = gpu.get(cx,cy) - gpu.setForeground(0xFFFFFF) - gpu.setBackground(0) + gpu.setForeground(fg) + gpu.setBackground(bg) gpu.set(cx,cy,pc) for i = 1, s:len() do 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.fill(1,1,mx,1," ") mode = "n" + else + mode = "n" end elseif mode == "v" then + mode = "n" if cc == "s" then -- save cursor sx, sy = cx, cy - mode = "n" elseif cc == "u" then -- restore cursor cx, cy = sx, sy - mode = "n" 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" cx, cy = tonumber(tx), tonumber(ty) - mode = "n" elseif cc == "A" then -- cursor up cy = cy - (tonumber(cs) or 1) - mode = "n" elseif cc == "B" then -- cursor down cy = cy + (tonumber(cs) or 1) - mode = "n" elseif cc == "C" then -- cursor right cx = cx + (tonumber(cs) or 1) - mode = "n" elseif cc == "D" then -- cursor left cx = cx - (tonumber(cs) or 1) - mode = "n" elseif cc == "h" and lc == "7" then -- enable line wrap lw = true elseif cc == "l" and lc == "7" then -- disable line wrap 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 + gpu.setForeground(fg) + gpu.setBackground(bg) + else + cs = cs .. cc + if cc:match("[%d;]") then + mode = "v" + end end - cs = cs .. cc end if cx > mx and lw then @@ -94,11 +117,12 @@ function vt100emu(gpu) -- takes GPU component proxy *gpu* and returns a function lc = cc end pc = gpu.get(cx,cy) - gpu.setForeground(0) - gpu.setBackground(0xFFFFFF) + gpu.setForeground(bg) + gpu.setBackground(fg) gpu.set(cx,cy,pc) - gpu.setForeground(0xFFFFFF) - gpu.setBackground(0) + gpu.setForeground(fg) + gpu.setBackground(bg) + return rs end return termwrite