First attempt at a simple repl
This commit is contained in:
parent
9ab75e3e3e
commit
19b42db731
1 changed files with 45 additions and 0 deletions
45
mithril.scm
45
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))))))))))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue