From 57d87f2353874d4503dde09c85d29e20ff291c18 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 2 May 2022 07:21:39 -0700 Subject: [PATCH] little Lua repl on hitting ctrl-r --- main.lua | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/main.lua b/main.lua index a061093..eafcdd3 100644 --- a/main.lua +++ b/main.lua @@ -34,13 +34,42 @@ function love.keypressed(key, scancode, isrepeat) -- do nothing elseif key == 'lalt' or key == 'ralt' then -- do nothing + elseif key == 'lshift' or key == 'rshift' then + -- do nothing elseif love.keyboard.isDown('lctrl') or love.keyboard.isDown('rctrl') then - lines[#lines] = lines[#lines]..' aaa' + if key == 'r' then + lines[#lines+1] = eval(lines[#lines])[1] + lines[#lines+1] = '' + end else lines[#lines] = lines[#lines]..key end end +function eval(buf) + local f = load('return '..buf, 'REPL') + if f then + return call_gather(f) + end + local f, err = load(buf, 'REPL') + if f then + return call_gather(f) + else + return {err} + end +end + +-- based on https://github.com/hoelzro/lua-repl +function call_gather(f) + local success, results = gather_results(xpcall(f, function(...) return debug.traceback() end)) + return results +end + +function gather_results(success, ...) + local n = select('#', ...) + return success, { n = n, ... } +end + function love.keyreleased(key, scancode) end