Hi all 👋
Is it possible to bind *out*
to something that would allow me to capture it as a string? I'd like to capture the output of planck.core/load-string
and I assume the output from that is going to *out*
(but maybe not?).
@mike858 If the forms being evaluated write to out, then you can capture any such output using with-out-str
.
For example
(with-out-str (planck.core/load-string "(pr :hi)"))
evaluates to
":hi"
I'm probably going about this the wrong way but what I want to do is to send a string to a socket REPL, have it be evaluated and then have the output (including error messages) saved into a string I can send back on the wire. A socket REPL will output to the wire at the moment but I'm messing about with a plugin for Neovim that would send a buffer of ClojureScript to the socket REPL and there are two problems: (1) an unclosed form will return nothing because the REPL is waiting for the closing parenthesis; (2) there's no way to tell when the output received back on the socket in Neovim is the 'end'. My idea was to wrap the code as a string, unwrap it on the REPL, load it in, capture the output, send that back as a string. I can then have the Neovim plugin wait until it gets the 'end' of the string and know I'm at the end of the output. Hopefully that made sense.
Basically what I want to do is be able to send a buffer to a Planck socket REPL and have it be evaluated (with some kind of response to indicate whether it was successful or unsuccessful). There are other solutions for REPL development in Vim but they're all based on nREPL.
One thing to consider is that output could be printed asynchronously. For example
(js/setTimeout #(prn :hi) 5000)
Yeah, I tend to use conventional REPL support in Cursive, with Tubular.
Not perfect, but, it works.
Yeah. I do my development via a Raspberry Pi on my iPhone :(
That's part of why I got very excited by Planck :D
It took about 15 hours but successfully compiled Planck on Raspbian on Friday! The --fast
option only takes a couple of hours.
So, you have Planck running on the Pi?
Yeah!
And then, how is your iPhone related? It is your terminal?
Works great :D I do have the very slow initial function evaluation that someone complained about on GitHub.
I meant to write a reply to that.
I have two young kids and my wife's been ill so I don't have much time to program except on the train to and from work (I work as an in-house lawyer). I use Blink on the iPhone to connect to my Pi at home via Mosh (connection is to fickle for SSH). I then program in Vim.
Interesting. For testing out simple pure functions, you could use Replete as well.
Yeah, I have that too! :)
But it's not too much fun trying to program in the little input field on the standard iOS keyboard. Blink has its own custom keys (TAB, CTRL, etc).
Hah. Before all of this existed, I used to use my iPhone to SSH into my Linux box and execute forms in a Clojure REPL. 🙂
So, I see why you can't use Tubular 🙂
Oh, and your suggestion for with-out-str
works for capturing printed output but the output from load-string
didn't get captured.
I guess load-string
itself returns the value of the last form it evaluates...
I've also tried:
(import '[goog.string StringBuffer])
(def sb (StringBuffer.))
(binding [*out* (StringBufferWriter. sb)]
(planck.core/load-string "(+ 1 2)"))
That's true.
It's really the warnings and error messages that I need as well. You can get the error from *e
but I'm not sure where the warning message goes.
Maybe with-out-str
would work for that. Something to try :)
If you want the return value as a string, you could (pr-str (planck.core/load-string "(+ 1 2)"))
?
Hmmm, no, that didn't work either.
pr-str
is helpful for the return value :D
Thanks! I'll have to think more about how to get those warnings.
I bet you could set!
*print-err-fn*
to something that would capture errors
Ooh. Thanks :)
I'll have a look at that!
Oh, and thanks for Planck. It's terrific :D
Glad you are finding value in it 🙂
Hmmm. Perhaps this is the point at which I call it quits but for some reason set!
refuses to actually set the new value.
(defn eg []
((set! *print-err-fn* *print-fn*)
(str *print-err-fn*)))
; => #'cljs.user/eg
(eg)
; => function PLANCK_PRINT_ERR_FN() {
; => [native code]
; => }
Whoops. That's not true. Ignore the above.
Hmmm... following further investigation, I think there's a limitation in REPLs with respect to errors. I can rebind *print-err-fn*
to a different function but I can't avoid errors being printed out to the console. However, this isn't purely a Planck thing. I've tried something similar with clj -m cljs.main --repl-env node
and the result is the same. Presumably there's something about the way errors are handled by the REPL code that won't allow errors to be redirected.