diff --git a/src/bin/tty.lua b/src/bin/tty.lua index 62b9a2b..d0ab94b 100644 --- a/src/bin/tty.lua +++ b/src/bin/tty.lua @@ -20,9 +20,39 @@ gpu.setBackground(0x000000) local w, h = gpu.getResolution() gpu.fill(1, 1, w, h, ' ') 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) gpu.bind(screen) @@ -34,11 +64,11 @@ local function onKey(ch, code) x = x - widthLast cmd = cmd:sub(1, -2) elseif code == 28 then --enter - gpu.set(1, y + 1, '> ') - gpu.set(3, y + 1, cmd) - cmd = '' - x, y = 1, y + 2 - else + x, y = 1, y + 1 + doEval(cmd) + x, cmd = 6, '' + gpu.set(1, y, 'lua>') + elseif ch ~= 0 then ch = unicode.char(ch) gpu.set(x, y, ch) cmd = cmd .. ch