hack a lua repl into bin/tty.lua

main
cinder 6 months ago
parent b046f8b4b5
commit 563c63a1c8

@ -20,9 +20,39 @@ gpu.setBackground(0x000000)
local w, h = gpu.getResolution() local w, h = gpu.getResolution()
gpu.fill(1, 1, w, h, ' ') gpu.fill(1, 1, w, h, ' ')
gpu.set(1, 1, string.format('%s // %s', _VERSION, _OSVERSION)) gpu.set(1, 1, string.format('%s // %s', _VERSION, _OSVERSION))
gpu.set(1, 2, 'lua>')
local cmd, x, y = '', 1, 2 local cmd, x, y = '', 6, 2
local env = setmetatable({}, {__index = _ENV}) --TODO pcall require() on __index
local function doEval(code)
local f, err = load('return ' .. code, '=expr', 't', env)
if not f then
f, err = load(code, '=repl', 't', env)
end
if not f then
gpu.setForeground(0xff0000)
gpu.set(x, y, tostring(err))
gpu.setForeground(0xffffff)
y = y + 1
return
end
local results = table.pack(pcall(f))
if results[1] then
for i = 2, results.n do
gpu.set(x, y, tostring(results[i]))
y = y + 1
end
else
gpu.setForeground(0xff0000)
gpu.set(x, y, tostring(results[2]))
gpu.setForeground(0xffffff)
y = y + 1
end
end
local function onKey(ch, code) local function onKey(ch, code)
gpu.bind(screen) gpu.bind(screen)
@ -34,11 +64,11 @@ local function onKey(ch, code)
x = x - widthLast x = x - widthLast
cmd = cmd:sub(1, -2) cmd = cmd:sub(1, -2)
elseif code == 28 then --enter elseif code == 28 then --enter
gpu.set(1, y + 1, '> ') x, y = 1, y + 1
gpu.set(3, y + 1, cmd) doEval(cmd)
cmd = '' x, cmd = 6, ''
x, y = 1, y + 2 gpu.set(1, y, 'lua>')
else elseif ch ~= 0 then
ch = unicode.char(ch) ch = unicode.char(ch)
gpu.set(x, y, ch) gpu.set(x, y, ch)
cmd = cmd .. ch cmd = cmd .. ch

Loading…
Cancel
Save