From 19b42db731b8e5f2f38c5eaafcfc40fd8a9f9ba4 Mon Sep 17 00:00:00 2001 From: Jakub Date: Sun, 12 Jul 2026 15:55:04 +0800 Subject: [PATCH] First attempt at a simple repl --- mithril.scm | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/mithril.scm b/mithril.scm index c2cd72f..ac171e2 100644 --- a/mithril.scm +++ b/mithril.scm @@ -36,3 +36,48 @@ Call: (mithril:comp ~s ~s)" :view (lambda (vnode) (,@view)) ,@args)) + +(define (mithril:repl prompt) + "(mithril:repl prompt preamble) + +Returns a REPL Mithril component that allows evaluating +custom LIPS code. The environment used by this REPL is +the same as the application execution environment, meaning +that the DOM and running scripts can be modified. + +PROMPT specifies the user prompt." + (let* ((add-line (lambda (state text prompt?) + (state.lines.push (list text prompt?)))) + (evaluate (lambda (state cmd) + (add-line state (format "~a~a" prompt cmd) #t) + (let ((res (eval str))) + (when res + (add-line state res #f))))) + (handle-key-down (lambda (state e) + (case e.key + (("Enter") + (e.preventDefault) + (let ((cmd state.input)) + (set! state.input "") + (evaluate state cmd))))))) + (mithril:comp + (let ((state vnode.state)) + (sxml + (.repl + (.repl-output + ~(list->array + (map + (lambda (line) + (m ".line" line.text)) + state.lines))) + (.repl-input + (span.prompt ~prompt) + (input (@ (type "text") + (value state.input) + (oninput (lambda (e) (set! state.input e.target.value))) + (onkeydown handle-key-down) + (oncreate (lambda (vnode) + (set! state.inputRef vnode.dome) + (vnode.dom.focus))) + (onfocus (lambda () (object))) + (autofocus #t))))))))))