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."
(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)
(add-line state (format "~a~a" prompt cmd) #t)
(let ((res (eval str)))
(let ((res (eval (with-input-from-string cmd read))))
(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)))))))
(cond
((equal? 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
(div.repl
(div.repl-output
~(list->array
(map
(lambda (line)
(m ".line" line.text))
state.lines)))
(.repl-input
(reverse state.lines))))
(div.repl-input
(span.prompt ~prompt)
(input (@ (type "text")
(value state.input)
(oninput (lambda (e) (set! state.input e.target.value)))
(onkeydown handle-key-down)
(onkeydown (lambda (e) (handle-key-down state e)))
(oncreate (lambda (vnode)
(set! state.inputRef vnode.dome)
(vnode.dom.focus)))
(onfocus (lambda () (object)))
(autofocus #t))))))))))
(autofocus #t)))))))
:oninit (lambda (vnode)
(set! vnode.state.lines '())
(set! vnode.state.input "")))))