edtris/editor/repl.fnl

50 lines
1.4 KiB
Plaintext
Raw Normal View History

2021-03-29 05:03:43 +00:00
(local util (require :lib.util))
(local fennel (require :lib.fennel))
(local style (require :core.style))
(local lume (require :lib.lume))
(local repl {})
(fn repl.inspect [v view x y w]
(renderer.draw_text style.font (fv v) x y style.text)
(style.font:get_height))
(fn repl.inspector [{: vals} view x y]
(var h 0)
(each [i v (ipairs vals)]
(set h (+ h (repl.inspect v view x (+ y h) view.size.x))))
(+ h style.padding.y))
(fn repl.notify [listeners line]
(each [_ listener (ipairs listeners)]
(listener:append line)))
(fn repl.run [{: listeners}]
(local inspector (util.fn repl :inspector))
(fennel.repl {:readChunk coroutine.yield
:onValues #(repl.notify listeners {:draw inspector :vals $1})
:onError (fn [errType err luaSource] (repl.notify listeners {:draw inspector :vals [err]}))
:pp #$1
:env (lume.clone _G)}))
(fn repl.listen [{: listeners} listener]
(table.insert listeners listener))
(fn repl.unlisten [{: listeners} listener]
(lume.remove listeners listener))
(fn repl.submit [{: coro} chunk]
(coroutine.resume coro (.. chunk "\n")))
(fn repl.new []
(local result
{:listeners []
:listen (util.fn repl :listen)
:unlisten (util.fn repl :unlisten)
:submit (util.fn repl :submit)
:coro (coroutine.create repl.run)})
(coroutine.resume result.coro result)
result)
repl