Make repl work properly

This commit is contained in:
Jakub 2026-07-12 16:22:51 +08:00
parent 19b42db731
commit 21230efb5e

View file

@ -47,37 +47,40 @@ that the DOM and running scripts can be modified.
PROMPT specifies the user prompt." PROMPT specifies the user prompt."
(let* ((add-line (lambda (state text prompt?) (let* ((add-line (lambda (state text prompt?)
(state.lines.push (list text prompt?)))) (set! state.lines (cons `&(:text ,text :prompt ,prompt?) state.lines))))
(evaluate (lambda (state cmd) (evaluate (lambda (state cmd)
(add-line state (format "~a~a" prompt cmd) #t) (add-line state (format "~a~a" prompt cmd) #t)
(let ((res (eval str))) (let ((res (eval (with-input-from-string cmd read))))
(when res (when res
(add-line state res #f))))) (add-line state res #f)))))
(handle-key-down (lambda (state e) (handle-key-down (lambda (state e)
(case e.key (cond
(("Enter") ((equal? e.key "Enter")
(e.preventDefault) (e.preventDefault)
(let ((cmd state.input)) (let ((cmd state.input))
(set! state.input "") (set! state.input "")
(evaluate state cmd))))))) (evaluate state cmd)))))))
(mithril:comp (mithril:comp
(let ((state vnode.state)) (let ((state vnode.state))
(sxml (sxml
(.repl (div.repl
(.repl-output (div.repl-output
~(list->array ~(list->array
(map (map
(lambda (line) (lambda (line)
(m ".line" line.text)) (m ".line" line.text))
state.lines))) (reverse state.lines))))
(.repl-input (div.repl-input
(span.prompt ~prompt) (span.prompt ~prompt)
(input (@ (type "text") (input (@ (type "text")
(value state.input) (value state.input)
(oninput (lambda (e) (set! state.input e.target.value))) (oninput (lambda (e) (set! state.input e.target.value)))
(onkeydown handle-key-down) (onkeydown (lambda (e) (handle-key-down state e)))
(oncreate (lambda (vnode) (oncreate (lambda (vnode)
(set! state.inputRef vnode.dome) (set! state.inputRef vnode.dome)
(vnode.dom.focus))) (vnode.dom.focus)))
(onfocus (lambda () (object))) (onfocus (lambda () (object)))
(autofocus #t)))))))))) (autofocus #t)))))))
:oninit (lambda (vnode)
(set! vnode.state.lines '())
(set! vnode.state.input "")))))