about summary refs log tree commit diff stats
path: root/main.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-05-02 07:21:39 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-02 07:23:36 -0700
commit57d87f2353874d4503dde09c85d29e20ff291c18 (patch)
tree2eed1eb6adc5c00ef18fd14f8b467984ee7c5740 /main.lua
parenta501b5ea23c8b8b40e321454f81aedb141cd9cce (diff)
downloadview.love-57d87f2353874d4503dde09c85d29e20ff291c18.tar.gz
little Lua repl on hitting ctrl-r
Diffstat (limited to 'main.lua')
-rw-r--r--main.lua31
1 files changed, 30 insertions, 1 deletions
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