Fork me on GitHub
#babashka
<
2019-12-14
>
sogaiu01:12:42

is there some analog of clojure.main/repl that one can use in a babashka script?

sogaiu01:12:42

i'm trying to replicate some version of george jahad's debug repl -- here is a simplified version by technomancy: https://github.com/technomancy/limit-break/blob/master/src/limit/break.clj

borkdude08:12:56

Nice:

$ clj
Clojure 1.10.1
user=> (clojure.main/repl)
user=> (+ 1 2 3)
6
user=> nil ;; exit inner REPL with ^D
user=> ^D ;; exit outer REPL with ^D 

borkdude08:12:13

I'll make an issue for it

sogaiu12:12:48

@borkdude thanks! here is my first unsuccessful attempt: https://github.com/borkdude/babashka/pull/155

borkdude12:12:07

Good start. One reason why it doesn't work is because in sci clojure.core/*in* is bound to a sci var, whereas in your function it's bound to the real clojure.core/*in* probably

sogaiu16:12:50

not understanding very well...trying to piece things together: in babashka's main.clj, there is:

[(start-repl! ctx #(read-next *in*)) 0]
and
(defn start-repl! [ctx read-next]
  (let [ctx (update ctx :bindings assoc
                    (with-meta '*in*
                      {:sci/deref! true})
                    (read-next))]
    (repl/start-repl! ctx)))
and
read-next (fn [*in*]
                    (if (pipe-signal-received?)
                      ::EOF
                      (if stream?
                        (if shell-in (or (read-line) ::EOF)
                            (read-edn))
                        (delay (cond shell-in
                                     (shell-seq *in*)
                                     edn-in
                                     (edn-seq *in*)
                                     :else
                                     (edn/read *in*))))))
so the *in* being used at bb's repl is not the vanilla *in*. is that along the lines of your hint?

borkdude16:12:07

yes, although later I realized I might have not said the right thing, but it's worth digging deeper

borkdude16:12:33

the repl function you are passing is now running with the default options, that might be why it doesn't work

borkdude16:12:08

but I have no clue beyond my guess

sogaiu16:12:20

by options you mean :prompt, :eval, and friends?

sogaiu16:12:37

will examine more closely, thanks

sogaiu19:12:23

i took an unclean approach to see if i could get things working. i exposed: * ctx that babashka.main/start-repl! sets up as @main/gctx * babashka.impl.repl/start-repl! function as main/start-repl! then did this:

$ lein bb
Babashka v0.0.42-SNAPSHOT REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.

user=> (main/start-repl! @main/gctx)
Babashka v0.0.42-SNAPSHOT REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.

user=> :hi
:hi
user=> :repl/quit
nil
user=> 
so may be that gives some hints about what to do